无法使用servlet和jstl从表中选择完整列表

时间:2014-03-27 09:32:30

标签: java jsp servlets jstl

嘿伙计们我正在尝试从数据库中检索表中的完整用户列表,但遗憾的是无法执行此操作。这是代码

1>的 Deleteuser.java

package roseindia.net;

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 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;

 /**
 * Servlet implementation class Deleteuser
 */
 @WebServlet("/Deleteuser")
 public class Deleteuser extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public Deleteuser() {
    super();
    // TODO Auto-generated constructor stub
}

 //String msg=" ";
    String fname=" ";
    String mname=" ";
    String lname=" ";
    String uname=" ";
    String emailid=" ";
    String mobno=" ";
    String address=" ";
    //String password1=" ";
//String password2=" ";
    String usertype=" ";
    String id=" ";

    public void setFname(String fname) {
    this.fname = fname;
    }
    public void setMname(String mname) {
    this.mname = mname;
    }
    public void setLname(String lname) {
    this.lname = lname;
    }
    public void setUname(String uname) {
    this.uname = uname;
    }
    public void setEmailid(String emailid) {
    this.emailid = emailid;
    }
    public void setMobno(String mobno) {
    this.mobno = mobno;
    }
    public void setAddress(String address) {
    this.address = address;
    }
public void setUsertype(String usertype) {
    this.usertype = usertype;
    }
public void setId(String id) {
    this.id = id;
    }

    /**
     * @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)
     */

    public static List<Deleteuser> list() throws Exception {
    Connection conn=null;
    String url="jdbc:mysql://localhost:3306/";
    String dbName="userlogindb";
    String driver="com.mysql.jdbc.Driver";
    String dbUserName="root";
    String dbPassword="root";

    //List<String> dataList = new ArrayList<String>(); 
    List<Deleteuser> users = new ArrayList<Deleteuser>();

    try{
    Class.forName(driver).newInstance();
    conn = DriverManager.getConnection(url+dbName,dbUserName,dbPassword);
    String strQuery="select * from registerutable";
    System.out.println("The sql is " +strQuery); 
    Statement st= conn.createStatement();
    ResultSet rs= st.executeQuery(strQuery);

    while(rs.next())
    {   
        Deleteuser user = new Deleteuser();
        user.setFname(rs.getString("fname"));
        user.setMname(rs.getString("mname"));
        user.setLname(rs.getString("lname"));
            user.setUname(rs.getString("uname"));
        user.setEmailid(rs.getString("emailid"));
        user.setMobno(rs.getString("mobno"));
        user.setAddress(rs.getString("address"));
        user.setUsertype(rs.getString("usertype"));
        user.setId(rs.getString("id"));
        users.add(user);    
}
rs.close();
st.close();
}
catch(Exception e)
{
    e.printStackTrace();
}
return users;
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    System.out.println("We are in the doPost method of Deleteuser");

    response.setContentType("text/html");
    PrintWriter out=response.getWriter();


    try{
    List<Deleteuser> users = Deleteuser.list();
    request.setAttribute("users",users);
    }
    catch(Exception e)
    {
    System.out.println(e);
    }

      //Disptching request

          RequestDispatcher dispatcher =  request.getRequestDispatcher("successful.jsp");

      if (dispatcher != null){

          dispatcher.forward(request, response);

          }

      //out.println("<font size='6' color=red>" + msg + "</font>");

}

  }

2&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">
<!--<%@page language="java" import="java.util.*" %>-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Successful</title>
</head>
<body>
<form name="deleteform" action="Deleteuser" 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>Email id</th>
<th>Mobile No</th>
<th>Address</th>
<th>Usertype</th>
<th>ID</th>
</tr> 

<c:forEach items="${users}" var="user">
<tr>
<td><c:out value="${user.fname}" /></td>
<td><c:out value="${user.mname}" /></td>
<td><c:out value="${user.lname}" /></td>
<td><c:out value="${user.uname}" /></td>
<td><c:out value="${user.emailid}" /></td>
<td><c:out value="${user.mobno}" /></td>
<td><c:out value="${user.address}" /></td>
<td><c:out value="${user.usertype}" /></td>
<td><c:out value="${user.id}" /></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>

这里,我无法从数据库表中显示用户列表。我也没有收到任何错误。

严重的道歉我还没有使用过MVC,因为我首先想要只使用一个java程序。

我添加了所有必要的jar文件。

如果我遗漏任何东西或没有遵循正确的技术/标准,请告诉我。

2 个答案:

答案 0 :(得分:0)

您尚未编写任何getter方法。

EL语句${user.fname}就像致电user.getFname()而你没有getter方法。

getter中的所有实例变量定义Deleteuser.java个方法。

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 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 getUsertype() {
    return usertype;
}
public void setUsertype(String usertype) {
    this.usertype = usertype;
}
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}

答案 1 :(得分:0)

要使用EL语法来获取对象 Pseudo Getter 的值,应该实现。

因此为所有元素定义getter。

${user.fname} implicitly calls user.getFname()