2016-05-30 62 views
0

當我從index.jsp登錄時,我可以看到確認信息說登錄成功,但如果我按F5按鈕,則不考慮上一個會話並創建新會話...所以我必須重複登錄如果我按F5 ...爲什麼如果我按下F5,servlet會話結束?

我該如何使會話持久?

的index.jsp:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<%@ page session="true"%> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title> Practica3 </title> 
<link rel="stylesheet" type="text/css" href="css/structure.css" /> 
<script type="text/javascript" src="jquery/jquery-1.7.1.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $(".menu").click(function(event) { 
     $('#content').load('Content',{content: $(this).attr('id')}); 
      }); 
}); 
</script> 
</head> 

<body> 

    <!-- Begin Wrapper --> 
    <div id="wrapper"> 

     <!-- Begin Header --> 
     <div id="header"> 

       This is the Header   

     </div> 
     <!-- End Header --> 

     <!-- Begin Navigation --> 
     <div id="navigation"> 

     <jsp:include page="menu3.jsp" /> 

     </div> 
     <!-- End Navigation --> 

     <!-- Begin Faux Columns --> 
     <div id="faux"> 

       <!-- Begin Left Column --> 
       <div id="leftcolumn"> 



       </div> 
       <!-- End Left Column --> 

       <!-- Begin Content Column --> 
       <div id="content"> 

       <jsp:include page="login.jsp" /> 

       </div> 
       <!-- End Content Column --> 



       <!-- Begin Right Column --> 
       <div id="rightcolumn"> 




       </div> 
       <!-- End Right Column --> 

     </div>  
     <!-- End Faux Columns --> 

     <!-- Begin Footer --> 
     <div id="footer"> 

       This is the Footer  

     </div> 
     <!-- End Footer --> 

    </div> 
    <!-- End Wrapper --> 
</body> 
</html> 

引入了menu.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8" session="false"%> 

<script type="text/javascript"> 
$(document).ready(function() { 
    $(".menu").click(function(event) { 
     $('#content').load('Content',{content: $(this).attr('id')}); 
     }); 
}); 
</script> 


<%HttpSession session = request.getSession(true); 
System.out.println("cargamos menu.jsp"); 
System.out.println("  (menu.jsp)Sesion:"+session); 
System.out.println("Sesion(user):"+session.getAttribute("user")); 
if ((session != null) && (session.getAttribute("user")!=null)) { 

%> 
<table> 
<tr> 
<td> <a class="menu" id="logout.jsp" href=#> Logout </a> </td> 
</tr> 
</table>  
<% } 
else {%> 
<table> 
<tr> 
<td> <a class="menu" id="form.jsp" href=#> Registration </a> </td> 
<td> <a class="menu" id="login.jsp" href=#> Login </a> </td> 
</tr> 
</table> 
<%}; %> 

的login.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<%@ page import="models.BeanLogin" %> 


<script type="text/javascript" src="jquery/jquery-1.7.1.js"></script> 
<script type="text/javascript" src="jquery/jquery.validate.js"></script> 
<script> 
$(document).ready(function(){ 
    $("#registerForm").validate({ 
     submitHandler: function(form) { 
      $('#content').load('logincontroller',$("#registerForm").serialize()); 
     } 
    }); 
} 

); 
</script> 
</head> 
<body> 

<% 
BeanLogin login = null; 
if (request.getAttribute("login")!=null) { 
    login = (BeanLogin)request.getAttribute("login"); 
} 
else { 
    login = new BeanLogin(); 
} 
%> 

<form id=registerForm action="/Practica3/logincontroller" method="POST"> 
<table> 
<tr> 
<td> User id </td> 
<td> <input type="text" name="user" value="<%=login.getUser() %>" id="user" class="required" minlength="5"/> </td> 
<% 
    if (login.getError()[0] == 1) { 
     %> 
     <td class="error"> Invalid username or password. </td> 
     <% 
    } 
%> 
</tr> 

<tr> 
<td> Password </td> 
<td><input type="password" name="password" placeholder="Password" 
    value="<%=login.getPassword()%>" id="password" class="required" minlength="8" /></td> 
</tr> 
<tr> 
<td> <input name="submit" type="submit" value="Enviar"> </td> 
</tr> 
</table> 
</form> 

loginOk.jsp:

<script type="text/javascript"> 
$(document).ready(function() { 
     $('#navigation').load('menu.jsp'); 
}); 
</script> 

Logged in! 

loginController.jsp:

/** 
    * Servlet implementation class logincontroller 
    */ 
    public class logincontroller extends HttpServlet { 
     private static final long serialVersionUID = 1L; 
     private static DAO Dao; 

     /** 
     * @throws Exception 
     * @see HttpServlet#HttpServlet() 
     */ 
     public logincontroller() throws Exception { 
      super(); 
      Dao = new DAO(); 
      // TODO Auto-generated constructor stub 
     } 

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

      BeanLogin login = new BeanLogin(); 
      BeanUtilities.populateBean(login, request); 
      try { 
       if (login.isComplete() && checkLogin(login)) { 

        HttpSession session = request.getSession(); 
        session.setAttribute("user",login.getUser()); 
        System.out.println("Se ha hecho el login."+session.toString()); 
        System.out.println("User:"+session.getAttribute("user")); 
        session.setMaxInactiveInterval(10); 
        RequestDispatcher dispatcher = request.getRequestDispatcher("loginOk.jsp"); 
        if (dispatcher != null) dispatcher.forward(request, response); 
       } else { 

       request.setAttribute("login",login); 
       RequestDispatcher dispatcher = request.getRequestDispatcher("/login.jsp"); 
       if (dispatcher != null) dispatcher.forward(request, response); 

       } 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     /** 
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
     */ 
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
      // TODO Auto-generated method stub 
      doGet(request,response); 
     } 

     protected boolean checkLogin(BeanLogin bean) throws SQLException{ 
      String query = "SELECT id,password FROM ts1.users;"; 

      ResultSet rs = Dao.executeQuerySQL(query); 

      while(rs.next()){ 

       if (rs.getString(1).equals(bean.getUser()) && rs.getString(2).equals(bean.getPassword())){ 
        return true; 
       } 
      } 

      int[] errors = bean.getError(); 
      errors[0]++; 
      bean.setError(errors); 
      return false; 
     } 


    } 

web.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>Practica3</display-name> 
    <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
    <servlet> 
    <description></description> 
    <display-name>Content</display-name> 
    <servlet-name>Content</servlet-name> 
    <servlet-class>controllers.Content</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>Content</servlet-name> 
    <url-pattern>/Content</url-pattern> 
    </servlet-mapping> 
    <servlet> 
    <description></description> 
    <display-name>formcontroller</display-name> 
    <servlet-name>formcontroller</servlet-name> 
    <servlet-class>controllers.formcontroller</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>formcontroller</servlet-name> 
    <url-pattern>/formcontroller</url-pattern> 
    </servlet-mapping> 
    <servlet> 
    <description></description> 
    <display-name>logincontroller</display-name> 
    <servlet-name>logincontroller</servlet-name> 
    <servlet-class>controllers.logincontroller</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>logincontroller</servlet-name> 
    <url-pattern>/logincontroller</url-pattern> 
    </servlet-mapping> 
    <servlet> 
    <description></description> 
    <display-name>closesession</display-name> 
    <servlet-name>closesession</servlet-name> 
    <servlet-class>controllers.closesession</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>closesession</servlet-name> 
    <url-pattern>/closesession</url-pattern> 
    </servlet-mapping> 
    <servlet> 
    <description></description> 
    <display-name>logoutcontroller</display-name> 
    <servlet-name>logoutcontroller</servlet-name> 
    <servlet-class>controllers.logoutcontroller</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>logoutcontroller</servlet-name> 
    <url-pattern>/logoutcontroller</url-pattern> 
    </servlet-mapping> 
    <session-config> 
    <session-timeout>-1</session-timeout> 
    </session-config> 
</web-app> 

回答

0

的方法的setMaxInactiveInterval()接受時間爲秒。您將會話的最長非活動時間設置爲10秒。將其設置爲比這更多的時間。

0

我假設你在頁面上按下F5,loginController轉發到,這應該是loginOK.jsp。你可能會認爲你刷新了頁面loginOK.jsp,這絕對不是。實際上,您只是刷新了loginController servlet。

原因是在您的loginController中,您使用轉發而不是重定向。當您的loginOK.jsp出現時,瀏覽器上的網址仍然是loginController而不是loginOK.jsp。刷新此頁面時,會刷新loginController。結果爲loginController創建了新的請求,創建了新的登錄對象。當您在loginController中檢查/驗證登錄對象時,它將無法通過檢查/驗證。因此,loginController通過您的else聲明轉發給login.jsp

一個可能的解決方案是使用重定向,而不是前鋒:
response.sendRedirect(...);

不過,我建議你重新設計了MVC模式下的登錄流程,不要把Java代碼/代碼在JSP頁面。
祝你好運。

相關問題