在我的JSP页面中,我正在计算会话值,如下所示
my.jsp包含两个按钮,一个用于将下面的表单提交到upload.java,单击另一个按钮会导致exec.java执行某些命令提示执行的操作
my.jsp看起来像,
<%
String timeStamp = new SimpleDateFormat("dd:MM:yyyy_HH:mm:ss:SSS").format(Calendar.getInstance().getTime());
timeStamp = timeStamp + ":" + System.nanoTime();
String loc = "/u/poolla/workspace/FirstServlet/WebContent/WEB-INF/"+timeStamp;
session.setAttribute("path", loc); // cal session and set attribute
%>
<div id="elements"> //form to accept files for upload
Left File : <input type="file" name="dataFile1" id="fileChooser1" /></li>
Right File : <input type="file" name="dataFile2" id="fileChooser2" /></li>
Config File :<input type="file" name="dataFile3" id="fileChooser3" /></li>
Gc File : <input type="file" name="dataFile4" id="fileChooser4" /></li>
<input type="hidden" id="myField" value="" /></li>
<button type="button" id="execute" onclick="ValidateFile()">Click to Upload files</button></li>
</div>
<Script>
ValidateFile()
{
//Does some validation stuff and subvmits the form to upload.java
document.myform.submit();
</Script>
upload.java
接收会话,
String fpath = request.getSession().getAttribute("path").toString();
使用commons文件上传来上传文件并将响应发送到my.jsp
RequestDispatcher rd = request.getRequestDispatcher("geco.jsp");
rd.forward(request, response);// Access session set in my.jsp
重定向到my.jsp,
单击按钮2,启动exec.java。
其中,我需要访问会话
try
{
String b = request.getSession().getAttribute("path").toString();
}
catch(NullPointerException e)
{
b = "No value";
}
PrintWriter out = response.getWriter();
out.println(b);
这是打印机作为空值,该怎么办?
从以下评论中, 我已经了解到我在页面刷新时失去了会话值。有什么办法可以保存会话值???
答案 0 :(得分:0)
感谢您上面的评论,我了解到我的会话正在刷新。
所以,
在my.jsp中我已经包含了检查会话是否存在的内容,如下所示,<%
String timeStamp = new SimpleDateFormat("dd:MM:yyyy_HH:mm:ss:SSS").format(Calendar.getInstance().getTime());
timeStamp = timeStamp + ":" + System.nanoTime();
HttpSession ses = request.getSession();
if(ses == null || ses.getAttribute("path") == null)
{
session.setAttribute("path", timeStamp);
}
else
{
session.getAttribute("path");
}
%>
在Upload.java中,我已收到此会话,并将收到的值再次添加回会话,
String fpath = request.getSession().getAttribute("path").toString();
HttpSession session = request.getSession();
session.setAttribute("path", fpath);
//Did my file upload stuff using apache commons
RequestDispatcher rd = request.getRequestDispatcher("my.jsp");
rd.forward(request, response);
最后,在使用会话后的mu exec.java中我将其无效
HttpSession ses = request.getSession();
ses.invalidate();
现在,即使正在刷新页面,我也能够访问会话。