JSP页面按表格展示数据库中的一张表的内容_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3624 | 回复: 0   主题: JSP页面按表格展示数据库中的一张表的内容        上一篇   下一篇 
feng.gao
注册用户
等级:中士
经验:239
发帖:15
精华:0
注册:1970-1-1
状态:离线
发送短消息息给feng.gao 加好友    发送短消息息给feng.gao 发消息
发表于: IP:您无权察看 2017-5-12 17:36:49 | [全部帖] [楼主帖] 楼主


jsp展示数据

<%@page import="com.landing.bean.Product"%>

<%@page import="java.util.List"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<table border="1" cellpadding="10" cellspacing="0">

<tr>

<th>Id</th>

<th>Name</th>

<th>Price</th>

<th>Location</th>

</tr>

<%

List<Product> products = (List<Product>)request.getAttribute("products");

for(Product product : products) {

%>

<tr>

<td><%= product.getId() %> </td>

<td><%= product.getName() %> </td>

<td><%= product.getPrice() %> </td>

<td><%= product.getLocation() %> </td>

<%

}

%>

</tr>

</table>

</body>

</html>


通过Servlet获取数据源查询数据库

package com.landing.bean;


import java.io.IOException;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.Hashtable;

import java.util.List;


import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.sql.DataSource;


public class ProductServlet extends HttpServlet{


@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

System.out.println(getAllProducts());

req.setAttribute("products", getAllProducts());

req.getRequestDispatcher("/products.jsp").forward(req, resp);

}


@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

doGet(req, resp);

}


public List<Product> getAllProducts() {

List<Product> products = new ArrayList<Product>();

Hashtable env=new Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");

        env.put(Context.PROVIDER_URL, "t3://localhost:7001");

        

        Context ctx = null;

        DataSource ds = null;

        Connection conn = null;

        Statement statement = null;

        ResultSet rs = null;

try {

ctx = new InitialContext(env);

ds = (DataSource) ctx.lookup("jdbc");

conn = ds.getConnection();

String sql = "select * from product";

statement = conn.createStatement();

rs = statement.executeQuery(sql);

while(rs.next()) {

String id = rs.getString("id");

String name = rs.getString("name");

String price = rs.getString("price");

String location = rs.getString("location");

Product product = new Product(id,name,price,location);

products.add(product);

}

} catch (NamingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally{

if(rs != null) {

try {

rs.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if(statement != null) {

try {

statement.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if(conn != null) {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if(ctx != null) {

try {

ctx.close();

} catch (NamingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return products;

}

}

Product 对应数据库表

package com.landing.bean;


public class Product {

private String id;

private String name;

private String price;

private String location;

public Product(String id, String name, String price, String location) {

super();

this.id = id;

this.name = name;

this.price = price;

this.location = location;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPrice() {

return price;

}

public void setPrice(String price) {

this.price = price;

}

public String getLocation() {

return location;

}

public void setLocation(String location) {

this.location = location;

}

}


web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

  <display-name>weblogic_demo</display-name>

  

  <servlet>

  <servlet-name>productList</servlet-name>

  <servlet-class>com.landing.bean.ProductServlet</servlet-class>

  </servlet>

  <servlet-mapping>

  <servlet-name>productList</servlet-name>

  <url-pattern>/productList</url-pattern>

  </servlet-mapping>

  

</web-app>


image.png

image.png





赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论