当我向数据库添加内容时,如何显示我的.jsp中的元素。 这是我的empservlet或我的代码的servlet
//empServlet
package emppackage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class empServlet extends HttpServlet {
String forward;
//empDAO myempDAO;
private static final long serialVersionUID = 1L;
//private empDAO disempdao;
public empServlet() {
super();
// myempDAO= new empDAO();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
empServices empserv= new empServices();
empserv.empadd(request.getParameter("eFname"),request.getParameter("eLname"),request.getParameter("eNameRes"),request.getParameter("eSerials"),request.getParameter("eJrss"),request.getParameter("eBand"),request.getParameter("eAcct"),request.getParameter("ePMPs"),request.getParameter("esJRSS"),request.getParameter("eOpenSeatDesc"),request.getParameter("eReqSkills"),request.getParameter("eReqBand"),request.getParameter("eDreject"),request.getParameter("eRreject"),request.getParameter("eDetActPlan"),request.getParameter("eDataComplete"),request.getParameter("eStat"));
//doGet(request, response);
}
}
这是我的员工dao,它位于arraylist
//empDAO
public List<empGetSet> getAllEmp()
{
List<empGetSet> empdetails = new ArrayList<empGetSet>();
try {
Connection conn = getConnection();
String showsql = "SELECT *FROM csemp;";
PreparedStatement psread= conn.prepareStatement(showsql);
ResultSet rsread = psread.executeQuery();
while (rsread.next())
{
empGetSet readgetset = new empGetSet();
readgetset.setFname(rsread.getString(1));
readgetset.setLname(rsread.getString(2));
readgetset.setNameRes(rsread.getString(3));
readgetset.setSerials(rsread.getString(4));
readgetset.setJrss(rsread.getString(5));
readgetset.setBand(rsread.getString(6));
readgetset.setAcct(rsread.getString(7));
readgetset.setPMPs(rsread.getString(8));
readgetset.setsJRSS(rsread.getString(9));
readgetset.setOpenSeatDesc(rsread.getString(10));
readgetset.setReqSkills(rsread.getString(11));
readgetset.setReqBand(rsread.getString(12));
readgetset.setDreject(rsread.getString(13));
readgetset.setsRreject(rsread.getString(14));
readgetset.setDetActPlan(rsread.getString(15));
readgetset.setDataComplete(rsread.getString(16));
readgetset.setStat(rsread.getString(17));
empdetails.add(readgetset);
psread.close();
conn.close();
}
}
这是我创建的jsp
<%@ page language="java"
contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
import = "thisPackage.GetSet"
import = "thisPackage.LoginServlet"
import = "thisPackage.LogoutServlet"
%>
<%
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.setHeader("Pragma", "no-cache");
%>
<!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>Hi welcome</title>
</head>
<body>
<center>
<h3>Hello </h3>
</center>
<form action ="LogoutServlet" method="post">
<div align="right"><input type = "submit" value= "Logout"></div>
</form>
<table border =1 cellpadding="18">
<thead>
<tr>
<th> First Name</th>
<th> Last Name</th>
<th> Name of Resource (LN ID Format)</th>
<th> Serial Number</th>
<th> JRSS</th>
<th> Band</th>
<th> Account (Proposed)</th>
<th> PMP Seat</th>
<th> Seat JRSS</th>
<th> Open Seat Description</th>
<th> Required Skills</th>
<th> Required Band low/high</th>
<th> Date of Rejection</th>
<th>Reason for Rejection</th>
<th>Detailed Action Plan</th>
<th>Target Date of Completion</th>
<th>Status(Ongoing/Closed)</th>
<th colspan=2>Do This</th>
</tr>
</thead>
</table>
<a href ="addDetails.jsp">Add Details</a>
</body>
</html>
我怎样才能让它显示出来,我找到了一些资源,但我无法以某种方式将它放入我的项目中,因为它不符合我编写代码的方式。
答案 0 :(得分:1)
在servlet中,您可以将request属性设置为DAO查询结果对象。然后循环遍历jsp页面中的数据网格。
Servlet代码&gt;&gt;
protected void listEmployees(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<empGetSet> empdetails = employeeDAO.getAllEmp(); //or call employeeService layer which calls employeDAO
request.setAttribute("empdetails", empdetails);
//dispatcher forward code
}
JSP代码&gt;&gt;
<c:forEach var="emp" items="${empdetails}">
<tr>
<td><c:out value="${emp.id}" /></td>
<td><c:out value="${emp.firstName}" /></td>
<td><c:out value="${emp.lastName}" /></td>
<!--- etc etc data -->
</tr>
</c:forEach>
注意:请更正你的DAO,即将结束PreparedStatement
&amp;结果集读取的while循环内的Connection
。把它从rsread.next()
的while循环中取出来。如果您使用的是Java 1.7及更高版本,我总是建议使用try with resources,更清洁和更好。更安全的代码。
<强>更新强>
您有两种JSP操作映射选项:
<a href="/list">Show List</a> <!-- this will call doGet() method -->
<form action="list" method="post">
<input type="submit" value="Show List" /> <!-- this will call doPost() method -->
</form>
servlet端url映射有两个选项:
注释(在Servlet类上)
@WebServlet("/employees")
public class EmpServlet extends HttpServlet {
/* code */
}
配置(在web.xml中)
<servlet>
<servlet-name>EmpServlet</servlet-name>
<servlet-class>com.test.EmpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EmpServlet</servlet-name>
<url-pattern>/employees</url-pattern>
</servlet-mapping>
最后你的servlet代码如下所示:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response); //if form action chosen
}
// if using doGet() / href option
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getServletPath();
//you can implement switch case here if having more possible actions/links on the page
if(action.equals("/list")){
List<empGetSet> empdetails = employeeDAO.getAllEmp(); //or call employeeService layer which calls employeDAO
request.setAttribute("empdetails", empdetails);
RequestDispatcher dispatcher = request.getRequestDispatcher("Employees.jsp");
dispatcher.forward(request, response);
}
}