我想通过servlet在我的jsp页面中显示对象列表。如何从servlet显示我的jsp中的对象列表

时间:2018-09-05 20:33:52

标签: mysql jsp servlets

数据库连接资料

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtils {

private static Connection connection = null;
private static PreparedStatement preparedStatement = null;
private static ResultSet resultSet = null;

public static Connection getConnection(String shopingkart) {
String url = "jdbc:mysql://localhost:3306/" + shopingkart;
String user = "root";
String pwd = "root1234";

try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, user, pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}

public static PreparedStatement getPreparedStatement(String sql) {
try {
if (connection != null) {
preparedStatement = connection.prepareStatement(sql,
java.sql.Statement.RETURN_GENERATED_KEYS);
}
} catch (SQLException e) {
e.printStackTrace();
}
return preparedStatement;
}

public static void closeResources(ResultSet rs, PreparedStatement pst,
Connection conn) {
try {
if (rs != null)
rs.close();
if (pst != null)
pst.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

}`

实用程序类     这是我的util类。在这里,我已经完成了用于从数据库获取数据的数据库操作。

package com.sushovan.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sushovan.bin.Product;

public class Allutills {

Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

public List<Product> getAllProducts() {

Product product = null;
List<Product> pList = new ArrayList<Product>();
String sql = "select * from Product";
connection = DBUtils.getConnection("shopingkart");
preparedStatement = DBUtils.getPreparedStatement(sql);

try {
resultSet = preparedStatement.executeQuery();
if (resultSet != null) {
while (resultSet.next()) {
product = new Product(resultSet.getInt(1),
resultSet.getString(2), resultSet.getInt(3));

pList.add(product);
}
}
} catch (SQLException e) {

e.printStackTrace();
}
return pList;

}

}

Servlet类 这是我的servlet类,即控制器类。

package com.sushovan.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sushovan.bin.Product;
import com.sushovan.utils.Allutills;

@WebServlet("/productServlet")
public class ProductServlet extends HttpServlet {

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

Allutills allutills = new Allutills();

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

allProList = allutills.getAllProducts();

HttpSession session = request.getSession(true);
if (session != null) {
session.setAttribute("allProList", allProList);
}
RequestDispatcher dispatcher = request
.getRequestDispatcher("showProduct.jsp");

dispatcher.forward(request, response);
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}

.jsp页面 这是我的视图页面,我试图在其中显示对象列表

<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.sushovan.bin.Product"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div align="center" style="border: medium;">
<table>
<tr>
<td><input type="text" value="Id" disabled="disabled">
<td><input type="text" value="Product Name" disabled="disabled">
<td><input type="text" value="Product Price"
disabled="disabled">
</tr>
</table>
</div>

<%


ArrayList<Product> allproList=(ArrayList<Product>) 
request.getAttribute("allProList");


for(Product product : allproList){
out.println(product.getPid());
out.println(product.getPname());
out.println(product.getPprice());
}
%>

</body>
</html>
  

此产品类       包com.sushovan.bin;

public class Product {

private int pid;
private String pname;
private int pprice;


/***********Getters And Setters*****************/
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getPprice() {
return pprice;
}
public void setPprice(int pprice) {
this.pprice = pprice;
}
/**********************Getter And Setters********************/

/******************Constuctors******************/
public Product(int pid, String pname, int pprice) {
super();
this.pid = pid;
this.pname = pname;
this.pprice = pprice;
}
public Product() {
super();    }
}
  

新编辑的Jsp 我在这里没有使用我的Util类。我已经完成了jsp页面中的所有操作

`<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.sushovan.bin.Product"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="com.sushovan.utils.DBUtils"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>


<%-- <% --%>


// ArrayList<Product> allProList=(ArrayList<Product>) 
request.getAttribute("allProList");

<%-- %> --%>

<!--        <table> -->
<!--                 <tr> -->
<!--                     <th>pid</th> -->
<!--                     <th>pname</th> -->
<!--                     <th>price</th> -->
<!--                 </tr> -->
<%--                 <c:forEach var="product" items="${allProList}"> --%>
<!--                     <tr> -->
<%--                         <td><c:out value="${product.pid}" /></td> -- 
%>
<%--                         <td><c:out value="${product.pname}" /></td> - 
-%>
<%--                         <td><c:out value="${product.price}" /></td> - 
-%>
<!--                     </tr> -->
<%--                 </c:forEach> --%>
<!--           </table> -->


<%
Connection connection= null;
PreparedStatement preparedStatement= null;
ResultSet resultSet= null;
String sql="select * from Product";
//CuisineBin cuisineBin= new CuisineBin();

connection= DBUtils.getConnection("shopingkart");
preparedStatement= connection.prepareStatement(sql);
resultSet= preparedStatement.executeQuery();
%>

<table align="left">
<tr>
<td><input type="text" value="Product Id" class="text" size="30px">
<td><input type="text" value="Product Name" class="text" size="30px">
<td><input type="text" value="Product Rate" class="text" size="30px">
<td><input type="text" value="Buy" class="text" size="30px">
</tr>
</table>
<%
while(resultSet.next()){
int id= resultSet.getInt(1);
String name= resultSet.getString(2);
int price= resultSet.getInt(3);

%>
<table align="left">

<tr>
<td><input type="text" id="pid" name="pid" value= "<%= resultSet.getInt(1)  
%>" size="30px">
<td><input type="text" id="pid" name="pid" value=" 
<%=resultSet.getString(2) %>" size="30px">
<td><input type="text" id="pid" name="pid" value="<%=resultSet.getInt(3) 
%>" size="30px">
<td><input type="submit" value="Buy Now" width="30px" size="30px">
</tr>   

</table>
<%} %>


</body>
</html>`

0 个答案:

没有答案