Servlets / JSP:后退按钮问题。退出后按回按钮说"确认表格重新提交"但刷新该页面会让我回到

时间:2015-06-29 19:08:01

标签: java jsp servlets button back

的Login.jsp

<form action="LoginServlet" method="post">
    <label>Email:</label>
    <input type="text" name="email"/>
    <label>Password:</label>
    <input type="password" name="pass"/>
    <input type="submit" value="Login"/>        
</form>

LoginServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("Boom");

    String email = request.getParameter("email");
    String pass = request.getParameter("pass");

    RequestDispatcher dispatcher = getServletConfig().getServletContext().getRequestDispatcher("/view/Success.jsp");

    AuthenticationService authService = new AuthenticationServiceImplementation();

    boolean isAuthenticated = authService.authenticate(email, pass);

    if(isAuthenticated) {
        HttpSession session = request.getSession();
        session.setAttribute("user", email);
        request.setAttribute("user", email);
        dispatcher.forward(request, response);
    }
}

的success.jsp

<a href="/Sample_app1/LogoutServlet">Logout</a>

<% String user = (String)request.getAttribute("user"); %>
<% if(user != null && session.getAttribute("user").equals("abc@abc.com")) { %>
        <p><%=user %> is logged in.</p>
<% } %>

LogoutServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    HttpSession session = request.getSession();
    session.removeAttribute("user");
    session.invalidate();
    RequestDispatcher dispatcher = getServletConfig().getServletContext().getRequestDispatcher("/view/Login.jsp");
    dispatcher.forward(request, response);
}

我在这里做错了什么?我不能让这个为我的生活工作。它只是简单的代码。刷新页面后如何重新发送用户名和密码,即使会话无效,也可以点击重新发送?可能是我对某些概念缺失了。请解释一下!

这是防止缓存的代码:

<% response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setDateHeader("Expires", 0); %>

我已将上述内容放在Success.jsp

0 个答案:

没有答案