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

泪湿孤枕 提交于 2020-01-07 06:13:24

问题


my code is

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

}

the url pattern for this servlet is /loginServlet. i wrote this code so that if user is logged in and makes a get request by hitting enter on the url then the request must be forwarded to doPost() or else if the user is not logged in then he get to the login page

but the problem is that its always returning to the login page that means request.getSession(false) always returning null

how can i solve this problem

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();
    }
 }
}

in the doPost() i m creating the session for the first time


回答1:


I believe the issue is that when you call doPost() directly from your doGet() method, the request parameters name, commun and password are not available. Hence, the authenticating SQL query fails and your else block executes.

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

This is effectively the same as your doGet()'s if block and hence gives an impression that your session is null.

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

So to fix the issue, if the session exists, redirect the user immediately instead of trying to execute doPost() to authenticate them again. The presence of name attribute can further assure that the user has been authenticated before.

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


来源:https://stackoverflow.com/questions/32046303/httpsession-session-request-getsessionfalse-ruturning-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!