首页 文章

HttpSession session = request.getSession(false); ruturning null

提问于
浏览
0

我的代码是

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

    HttpSession session=request.getSession(false);

    if(session==null){

        response.sendRedirect(request.getContextPath());
    }else{

        doPost(request,response);
    }

}

这个servlet的url模式是/ loginServlet . 我写了这段代码,以便如果用户登录并通过点击输入在url上发出get请求,那么请求必须转发到doPost(),否则如果用户没有登录,那么他会进入登录页面

但问题是它总是返回到登录页面,这意味着request.getSession(false)总是返回null

我怎么解决这个问题

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String name=request.getParameter("name");
    String commun=request.getParameter("commun");
    String password=request.getParameter("password");
      Connection conn = null;
      Statement  statement = null;


    try{
    Context initContext = new InitialContext();
    Context envContext = (Context) initContext.lookup("java:comp/env");
    DataSource ds = (DataSource) envContext.lookup("jdbc/community");
     conn = ds.getConnection();

      statement = conn.createStatement();
      String sql = "select memberragas from windowragas where memberragas='"+name+"' and liquid_key='"+commun+"' and chabhiragas='"+password+"'";
      ResultSet rs =statement.executeQuery(sql);
     if(rs.next()){
         System.out.println("welcome "+name+"");
         HttpSession session=request.getSession();
         session.setAttribute("name", name);
         session.setAttribute("commun", commun);
         RequestDispatcher rd = request.getRequestDispatcher("/homey");
         rd.forward(request, response);


     }else{
         System.out.println("either ur username or password or community was wrong");
         response.sendRedirect(request.getContextPath());
     }
    }
 catch (NamingException ex) {
    System.err.println(ex);
} catch (SQLException ex) {
    System.err.println(ex);
}finally {

    try {
       if (statement != null) statement.close();
       if (conn != null) conn.close();  // return to pool
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
 }
}

在doPost()我第一次创建会话

1 回答

  • 0

    我相信问题是,当您直接从 doGet() 方法调用 doPost() 时,请求参数 namecommunpassword 不可用 . 因此,身份验证SQL查询失败并执行 else 块 .

    } else {
         System.out.println("either ur username or password or community was wrong");
         response.sendRedirect(request.getContextPath());
     }
    

    这与你的 doGet()if 块实际上是相同的,因此给人的印象是 sessionnull .

    if (session == null) {
        response.sendRedirect(request.getContextPath());
    }
    

    因此,要解决此问题,如果 session 存在,请立即重定向用户,而不是尝试执行 doPost() 以再次对其进行身份验证 . name 属性的存在可以进一步确保用户之前已经过身份验证 .

    if (session != null && session.getAttribute("name") != null) {
        RequestDispatcher rd = request.getRequestDispatcher("/homey");
        rd.forward(request, response);
    } else {
        response.sendRedirect(request.getContextPath());
    }
    

相关问题