在声明它的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> </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>
还可以提出替代解决方案吗?
答案 0 :(得分:0)
最好的解决方案可能是使servlet中的会话无效,但要确保JSP所需的任何值都存储在请求中而不是存储在会话中。我这样说是因为最好将所有逻辑放在bean或servlet代码中,并保留JSP仅用于布局。
答案 1 :(得分:0)
无需使会话无效。此外,不需要使用会话属性。如果要转发到错误页面,可以使用请求属性。大量使用会话是一种不好的做法,因为它需要大量内存来利用您放入其中的变量。
} catch (DBException e) {
request.setAttribute("exception",e);
request.getRequestDispatcher("FileUpload.jsp").forward(request, response);
}
正如我所说,当你要转发错误页面时,使会话无效是不好的做法。即使JSP页面在同一个线程中呈现,其他可以使用相同会话的线程也可能无效。在会话失效后,您无法使用会话变量。但是很少发生,但是在呈现JSP时,其他请求可能会使会话无效。