HttpSession session=request.getSession(false);车削零 [英] HttpSession session=request.getSession(false); ruturning null

查看:55
本文介绍了HttpSession session=request.getSession(false);车削零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是

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 上按回车键发出获取请求,那么请求必须转发到 doPost()否则,如果用户未登录,则他将进入登录页面

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

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

but the problem is that its always returning to the login page that means request.getSession(false) always returning 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() 我第一次创建会话

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

推荐答案

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

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

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

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

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

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

这篇关于HttpSession session=request.getSession(false);车削零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆