简体   繁体   English

无法使用jstl将对象arraylist从servlet发送或传递给jsp

[英]unable to send or pass object arraylist from servlet to jsp using jstl

Hey guys I m trying to show list of users from the database table by passing arraylist object from servlet to jsp but unable to do so. 嘿伙计们我试图通过将arraylist对象从servlet传递给jsp来显示数据库表中的用户列表但是无法这样做。

1.> Login.java 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 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.> successful.jsp 3.> 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>

Here, I m unable to pass the object arraylist for list of users from servlet to jsp. 在这里,我无法将用户列表的对象arraylist从servlet传递给jsp。 There no run time or compile time error. 没有运行时或编译时错误。 I m able to show list of users by writing query in jstl sql tag but unable to do so in java servlet code. 我可以通过在jstl sql标签中编写查询来显示用户列表,但是在java servlet代码中无法这样做。 :( :(

In web.xml also everything is fine and database connectivity is also properly done. 在web.xml中,一切都很好,数据库连接也正常完成。 Then where m I going wrong. 然后我哪里错了。

I checked various catch's and tutorial's but unable to do so. 我检查了各种catch和教程,但无法这样做。

Please Help. 请帮忙。

You have a serious misconception. 你有一个严重的误解。 The servlet is only called when the form is submitted. 只有在提交表单时才会调用servlet。 So you can't expect, in the JSP, to have access to a list that should have been created and stored in the request, since the servlet doing that has never been called. 因此,您无法在JSP中访问应该已创建并存储在请求中的列表,因为从未调用过该servlet。

Your servlet never forwards to the JSP. 您的servlet永远不会转发到JSP。 All it does is send the content type of the response to text/html. 它只是将响应的内容类型发送到text / html。

Moreover, you're storing the same list in a request attribute at every iteration of the loop, instead of doing it just once, when the loop has ended: 此外,当循环结束时,您在循环的每次迭代中将相同的列表存储在请求属性中,而不是仅执行一次:

while(rs.next()) {
    ...
}
request.setAttribute("ual", al);

Here's what you should have: 这是你应该拥有的:

  1. A ShowUsers servlet. 一个ShowUsers servlet。 This servlet should have a doGet() method which gets the users, stores them in a request attribute, then forwards to a showUsers.jsp page 这个servlet应该有一个doGet()方法,它获取用户,将它们存储在一个请求属性中,然后转发到showUsers.jsp页面
  2. The showUsers.jsp should have one form per user (and not a single form with all the users inside). showUsers.jsp每个用户应该有一个表单(而不是包含所有用户的单个表单)。 Each form should have the ID of the user to delete in a hidden input field, and a submit button to delete the user. 每个表单应具有要在隐藏输入字段中删除的用户的ID,以及用于删除用户的提交按钮。 The form should have the path of the DeleteUser servlet. 表单应该具有DeleteUser servlet的路径。
  3. The DeleteUser servlet should have a doPost() method, which should take the ID of the user to delete as request parameter, delete this user from the database, and redirect to the ShowUsers servlet. DeleteUser servlet应该有一个doPost()方法,该方法应该将用户的ID作为请求参数删除,从数据库中删除该用户,并重定向到ShowUsers servlet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM