簡體   English   中英

在聲明了它的servlet或將使用其值的jsp頁面中使會話無效會更好嗎?

[英]Is it better to invalidate a session in a servlet in which it is declared or in the jsp page where its values will be used?

在聲明了它的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>

也可以提出另一種解決方案嗎?

最好的解決方案可能是使Servlet中的會話無效,但要確保JSP所需的所有值都存儲在請求中而不是會話中。 我之所以這樣說,是因為最佳實踐是將所有邏輯放入bean或servlet代碼中,並保留JSP僅用於布局。

無需使會話無效。 同樣,也不需要使用會話屬性。 如果要轉發到錯誤頁面,可以使用request屬性。 大量使用會話是一種不好的做法,因為它需要大量內存才能利用您放入會話中的那些變量。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM