2015-04-26 21 views
1

在servlet中聲明一個會話或在JSP頁面中使用它的值會失效嗎?在聲明它的servlet中或者在jsp頁面中使用它的值將會話失效會更好嗎?

我張貼的servlet代碼如下 -

package Controller.UploadInfo; 

import File.FileOperations; 
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; 

import Controller.DatabaseException.*; 


public class AttendenceInfoUpload extends HttpServlet { 


    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     try (PrintWriter out = response.getWriter()) { 

      HttpSession session; 

      if (FileOperations.fileUpload(request)) { 

       try { 

        FileOperations.excelToAttendence(); 

        request.getRequestDispatcher("UploadSuccess.jsp").forward(request, response); 


       } catch (DBException e) { 

        session = request.getSession(true); 
        session.setAttribute("exception",e); 

        request.getRequestDispatcher("FileUpload.jsp").forward(request, response); 

        session.invalidate(); 
       } 

      } else { 

       session = request.getSession(true); 
       session.setAttribute("exception"," File Upload Failed "); 

       request.getRequestDispatcher("FileUpload.jsp").forward(request, response); 

      } 
     } 
    } 


    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 


    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 


    @Override 
    public String getServletInfo() { 
     return "Short description"; 
    }// </editor-fold> 

} 

在這個servlet上面我已經在catch塊getRequestDispatcher()後立即失效了會議。儘管代碼正在工作,但我擔心的是它會在JSP頁面中顯示異常消息之前將其釋放。或者,更好的方法是在JSP頁面中的servlet中聲明的會話中使其值被顯示的會話無效。

JSP頁面 -

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@page import = "java.io.*" %> 


<!DOCTYPE html> 
<html> 
    <head> 
     <title>TODO supply a title</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    </head> 

    <body> 
     <h1>Excel File Upload Example</h1> 

     <form name="form1" method="post" action="AttendenceInfoUpload" enctype="multipart/form-data"> 
      <table border="1"> 
       <col width="120">         
       <tr> 
        <td>Upload Excel File:</td> 
        <td><input type="file" name="Select File"/></td> 
       </tr>    
       <tr> 
        <td>&nbsp;</td> 
        <td><input name="" type="submit" value="upload" /></td>      
       </tr> 
      </table> 
     </form>   

     <c:if test="${not empty exception}"> 

      <label> 
       <font color="red"> 

       <c:out value="Error:${exception}"></c:out> 

       </font>    
      </label>              
     </c:if>   

    </body>  
</html> 

人們可以提出一個替代的解決方案呢?

+2

爲什麼你首先使會話無效?爲什麼只存儲對此請求感興趣的會話屬性?你爲什麼不把它們存儲在請求屬性中? –

+0

會話未在servlet中聲明。它由servlet容器維護*。你無效的地方取決於你。你的問題沒有意義。 – EJP

回答

0

最好的解決方案可能是使servlet中的會話無效,但要確保JSP所需的任何值都存儲在請求中而不是會話中。我這樣說是因爲最好的做法是將所有的邏輯放在bean或servlet代碼中,並保持JSP僅用於佈局。

+0

你的結論並不遵循你的前提。 – EJP

+0

怎麼回事?我的意思不是在JSP中,因爲JSP用於佈局,會話的失效更符合邏輯。 – redge

0

沒有必要使會話無效。也不需要使用會話屬性。如果你想轉發到錯誤頁面,你可以使用request屬性。大量使用會話是一種不好的做法,因爲它需要大量的內存來利用您輸入的變量。

} catch (DBException e) { 
    request.setAttribute("exception",e); 
    request.getRequestDispatcher("FileUpload.jsp").forward(request, response); 
} 

正如我所說的,當您要轉發到錯誤頁面時,使會話失效並不是一個好習慣。即使JSP頁面在同一線程中呈現,其他可以使用相同會話的線程也可能無法工作。會話失效後,您無法使用會話變量。然而,這種情況很少發生,但其他請求可能在JSP呈現時使會話無效。

+0

你在說什麼情況,我無法從問題中讀出。 –

+0

我沒有使用單獨的錯誤頁面。 JSP頁面(在上面的示例中給出)既是輸入頁面,也顯示在輸入操作期間從servlet接收到的錯誤。它本質上是動態的。在第一次運行時,它不顯示錯誤,但當發生錯誤時,它顯示在底部的標籤中。我的問題是,當我沒有使會話無效時,即使運行它,新的舊會話值仍然顯示在底部。所以我不應該在servlet中使用HttpSession會話對象,還是應該在上述情況下使用請求屬性? – Saber

+0

您不應使用請求屬性,使另一個servlet中的會話無效並重定向到登錄頁面。 –

相關問題