我正在通过 Servlet 上传图像。
try{
//Image Upload Code
} catch(Exception e){
//Exception Handler
} finally {
request.setAttribute("job-id", jobId);
request.setAttribute("message", message);
System.out.println("SJA : " + message);
getServletContext().getRequestDispatcher("ReloadJob.jsp").forward(request, response);
}
然后,在 ReloadJob.jsp 上,我尝试使用这些附加属性:
String jobId = request.getParameter("job-id");
String message = request.getParameter("message");
System.out.println("RJ : " + jobId);
System.out.println("RJ : " + message);
整个过程打印以下输出:
SJA : MESSAGE RECEIVED
RJ : BYWEGRI76T46U34T
RJ : null
为什么message变量为空?
最佳答案
String message=(String)request.getAttribute("message");
getParameter()
返回 http 请求参数。这些从客户端传递到服务器。
getAttribute()
仅适用于服务器端使用 - 您可以使用可在同一请求中使用的属性填充请求。
例如 - 您在 servlet 中设置一个属性,然后从 JSP 中读取它。它可以用于任何对象,而不仅仅是字符串。
关于java - 在 Servlet RequestDispatcher 中发送两个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35427831/