如果else语句不能包含页面,则jsp中的会话不起作用

时间:2016-03-04 04:05:58

标签: jsp session

在索引页面上,jsp没有检查会话是否存在,并继续到下一页,刷新后显示会话属性的同一页面。

实际上在mycode中如果循环不起作用是什么问题

请帮忙。

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>login Page</title>
    </head>
    <body>
        <h1>index page</h1>
        <jsp:include page="/check" />       
    </body>
</html>

Servlet代码:

package reg;

import java.io.IOException;
import java.io.PrintWriter;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;

public class check extends HttpServlet {  

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    response.setContentType("text/html");  
    PrintWriter out=response.getWriter();  
    //request.getRequestDispatcher("link.html").forward(request, response);  
    out.print("hello check servlet<br>");
    HttpSession nsession=request.getSession(false);

    if(nsession == null) {  
        String name=(String)nsession.getAttribute("name");
        out.print("Hello, "+name+" Welcome to Profile");
        request.getRequestDispatcher("/adminhome.jsp").include(request, response);
    } else {    
        out.print("Please login first");
        request.getRequestDispatcher("/login.jsp").include(request, response);
    }
    out.close();  
    }  
} 

1 个答案:

答案 0 :(得分:0)

我也不知道问题是什么。如果你还没有在别处创建会话,它应该返回null。

但请尝试:

  • 取消选中在整个重新部署期间保留会话,并查看是否存在 帮助。 (对我没有帮助)
  • 或者只是按照不同的方式编码:
private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html");  
    PrintWriter out = response.getWriter();
    out.print("<h2>hello check servlet</h2><hr>");
    HttpSession nsession = request.getSession();
    String name = (String) nsession.getAttribute("name");

    if(name != null) { 
        out.print("Hello, " + name + " Welcome to Profile");
        request.getRequestDispatcher("/adminhome.jsp").include(request, response);
    } else {
        out.print("Please login first!");
        request.getRequestDispatcher("/login.jsp").include(request, response);
    }
    out.close();  
}

除此之外, 你有另一个处理登录的servlet,如果成功设置了会话吗? 像这样:

private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String email = request.getParameter("email");
    String password = request.getParameter("password");

    try {
        // Do database related stuff here... (to check if user exists)
        if(validUser())
            request.getSession().setAttribute("username", name); // Set the name of the user to the session
    }

您还应该检查这些以获取有关会话的更多信息, java session management http://www.tutorialspoint.com/servlets/servlets-session-tracking.htm