嘿伙计们我试图通过将arraylist对象从servlet传递给jsp来显示数据库表中的用户列表但是无法这样做。
1> Login.java
package com.pms.servlet;
public class Login {
int id;
String fname;
String mname;
String lname;
String uname;
String utype;
String emailid;
String mobno;
String address;
String password1;
String password2;
public int getUid()
{
return id;
}
public void setUid(int id)
{
this.id=id;
}
public String getFname()
{
return fname;
}
public void setFname(String fname)
{
this.fname=fname;
}
public String getMname()
{
return mname;
}
public void setMname(String mname)
{
this.mname=mname;
}
public String getLname()
{
return lname;
}
public void setLname(String lname)
{
this.lname=lname;
}
public String getUname()
{
return uname;
}
public void setUname(String uname)
{
this.uname=uname;
}
public String getUtype()
{
return utype;
}
public void setUtype(String utype)
{
this.utype=utype;
}
public String getEmailid()
{
return emailid;
}
public void setEmailid(String emailid)
{
this.emailid=emailid;
}
public String getMobno()
{
return mobno;
}
public void setMobno(String mobno)
{
this.mobno=mobno;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address=address;
}
public String getPassword1()
{
return password1;
}
public void setPassword1(String password1)
{
this.password1=password1;
}
public String getPassword2()
{
return password2;
}
public void setPassword2(String password2)
{
this.password2=password2;
}
}
2> Delete.java
package com.pms.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
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 javax.swing.text.html.HTMLDocument.Iterator;
/**
* Servlet implementation class Delete
*/
@WebServlet("/Delete")
public class Delete extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Delete() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Connection conn=null;
String url="jdbc:mysql://localhost:3306/";
String dbName="projectmanagement";
String driver="com.mysql.jdbc.Driver";
String dbUserName="root";
String dbPassword="root";
try{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,dbUserName,dbPassword);
String strQuery="select * from user";
System.out.println("The sql is " +strQuery);
Statement st= conn.createStatement();
ResultSet rs= st.executeQuery(strQuery);
ArrayList<Login> al=new ArrayList<Login>();
while(rs.next())
{
Login l=new Login();
//l.setUid(rs.getInt("uid"));
l.setFname(rs.getString("fname"));
l.setMname(rs.getString("mname"));
l.setLname(rs.getString("lname"));
l.setUname(rs.getString("uname"));
l.setUtype(rs.getString("utype"));
l.setEmailid(rs.getString("emailid"));
l.setMobno(rs.getString("mobno"));
l.setAddress(rs.getString("address"));
System.out.println(l);
System.out.println(al);
al.add(l);
request.setAttribute("ual",al);
System.out.println(al);
}
rs.close();
st.close();
}
catch(Exception e)
{
e.printStackTrace();
}
response.setContentType("text/html");
}
}
3&GT; successful.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<form name="delete-user" action="Delete" method="post">
<p><b><i>List of user details:</i></b></p>
<table border="1">
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>User Name</th>
<th>Usertype</th>
<th>Email id</th>
<th>Mobile No</th>
<th>Address</th>
<th>Delete User</th>
</tr>
<c:forEach items="${ual}" var="u">
<tr>
<td><c:out value="${u.fname}"></c:out></td>
<td><c:out value="${u.mname}"></c:out></td>
<td><c:out value="${u.lname}"></c:out></td>
<td><c:out value="${u.uname}"></c:out></td>
<td><c:out value="${u.utype}"></c:out></td>
<td><c:out value="${u.emailid}"></c:out></td>
<td><c:out value="${u.mobno}"></c:out></td>
<td><c:out value="${u.address}"></c:out></td>
<td><input type="button" name="delete" value="Delete User"/> </td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
这里,我无法将用户列表的对象arraylist从servlet传递给jsp。没有运行时或编译时错误。我可以通过在jstl sql标签中编写查询来显示用户列表,但是在java servlet代码中无法这样做。 :(
在web.xml中,一切都很好,数据库连接也正常完成。然后我哪里出错了。
我检查了各种捕获和教程,但无法这样做。
请帮助。
答案 0 :(得分:2)
你有一个严重的误解。只有在提交表单时才会调用servlet。因此,您无法在JSP中期望访问应该已创建并存储在请求中的列表,因为从未调用过这样做的servlet。
您的servlet永远不会转发到JSP。它只是将响应的内容类型发送到text / html。
此外,您在循环的每次迭代中将相同的列表存储在请求属性中,而不是在循环结束时仅执行一次:
while(rs.next()) {
...
}
request.setAttribute("ual", al);
这是你应该拥有的: