登录到应用程序并在jsp页面上显示时从数据库中检索数据

时间:2015-11-09 06:45:17

标签: java jsp servlets

我有一个简单的登录页面。记录时,它通过javabean dao和servlets ...一切都很好......但随着登录,我想检索数据并通过使用会话将数据保存到应用程序中。所以我可以在jsp标题上显示用户名和usertype。对于所有页面。下面是我的logindao和servlet页面,请帮帮我。

LoginDao:

public class LoginDao { 

public static String validate(String name, String pass) {  

    String Usertype="";
    PreparedStatement pst = null;  
    ResultSet rs = null;
    try{  
            Connection conn=ConnectionProvider.getConn(); 

        pst = conn.prepareStatement("select * from employee where Emp_id=? and Pwd=?");  
        pst.setString(1, name);  
        pst.setString(2, pass); 

        rs = pst.executeQuery();  
        if(rs.next()){
                    Usertype=rs.getString("Usertype");

                }  

    } catch (Exception e) {  
        System.out.println(e);  
    } 
    finally { 
        if (pst != null) {  
            try {  
                pst.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
        if (rs != null) {  
            try {  
                rs.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
    return Usertype;  
}  
}  

LoginServlet:

public class LoginServlet extends HttpServlet{  

    private static final long serialVersionUID = 1L;  

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)    
            throws ServletException, IOException {    


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

        String n=request.getParameter("Emp_id");    
        String p=request.getParameter("Pwd");   
        String Usertype=""; 
        Usertype=LoginDao.validate(n, p);


         if (Usertype.trim()!="" && Usertype.length()>0) {
            HttpSession session = request.getSession(true); // The session will be set only if there is a valid login 
            session.setAttribute("name", n);
            session.setAttribute("Usertype",Usertype);  
            RequestDispatcher rd=request.getRequestDispatcher("/daywise.jsp");    
            rd.forward(request,response);    
        }    
        else{    
            out.print("<p style=\"color:red\">Sorry Employee ID or password error</p>");    
            RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");    
            rd.include(request,response);  

        }    

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

}

Bean类用户:

public class User {
private String Emp_id;
private String Pwd;
private String Emp_name;
private String Designation;
private String Emailid;
private String Phone;
private String Usertype;
// getter and setter methods}

resultg jsp page:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
   Employee name:<input type="text" value="<%=session.getAttribute("Emp_name")%>">
   Employee Id:<input type="text" value="<%=session.getAttribute("Emp_id")%>">
   Designation:<input type="text" value="<%=session.getAttribute("Designation")%>">
   User Type:<input type="text" value="<%=session.getAttribute("Usertype")%>">

</body>
</html>

1 个答案:

答案 0 :(得分:1)

请试试

request.getSession().setAttribute("name", n);

我正在使用它。

Github LoginController