日期:2014-05-17  浏览次数:20676 次

帮我看一个servletjsp的问题

登录的servlet代码

Java code
public class Login extends HttpServlet{

    public void init(ServletConfig config) throws ServletException{
            super.init(config);
    }
    
    public void service(HttpServletRequest req,HttpServletResponse resp) throws IOException{
        
        HttpSession session=req.getSession(true);
        PrintWriter out = resp.getWriter();
        String username=req.getParameter("username");
        String pwd=req.getParameter("pwd");
        String sql="select * from userinfo where username=? and pwd=?";
        
        Connection conn=db.getConnection();
        
        try{
            PreparedStatement pstmt=conn.prepareStatement(sql);
            
            pstmt.setString(1,username);
            pstmt.setString(2,pwd);
            
            ResultSet rs=pstmt.executeQuery();
            Boolean m=rs.next();
            if(m==true){
                session.setAttribute(username, rs.getString("username"));
                session.setAttribute(pwd,rs.getString("pwd"));
                
                resp.sendRedirect("index.jsp");
            }else{
                out.println("<SCRIPT LANGUAGE=javascript>");
                out.println("alert('用户名或密码错误!');");
                out.println("window.location.href='default.jsp'; ");
                out.println("</script>");
            }
            pstmt.close();
              conn.close();
        }
        catch(SQLException e){
            e.printStackTrace();
        } 
        
    }
}


查询数据的servlet
Java code
public class gl extends HttpServlet{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public void init(ServletConfig config) throws ServletException{
            super.init(config);
    }
    
    public void service(HttpServletRequest req,HttpServletResponse resp) throws IOException{
        
        HttpSession session=req.getSession(true);
        PrintWriter out = resp.getWriter();
        String username=null;
        String lastip=null;
        String sql="select username,lastip from log";
        
        Connection conn=db.getConnection();
        
        try{
            PreparedStatement pstmt=conn.prepareStatement(sql);
            ResultSet rs=pstmt.executeQuery(sql);
            Boolean m=rs.next();
            if(m==true){
                session.setAttribute(username,rs.getString("username"));
                session.setAttribute(lastip,rs.getString("lastip"));
                System.out.println(username);
            }else{
                out.println("<SCRIPT LANGUAGE=javascript>");
                out.println("alert('用户名或密码错误!');");
                out.println("window.location.href='default.jsp'; ");
                out.println("</script>");
            }
            pstmt.close();
              conn.close();
        }
        catch(SQLException e){
            e.printStackTrace();
        } 
        
    }
}



登录的jsp页面

Java code
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'default.jsp' starting page&l