java - Session not available -
in jsp page, calculating session value following
my.jsp consists 2 buttons, 1 submit below form upload.java , on click of button leads exec.java command prompt execution stuff
my.jsp looks like,
<% 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 , set attribute %> <div id="elements"> //form accept files 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 upload files</button></li> </div> <script> validatefile() { //does validation stuff , subvmits form upload.java document.myform.submit(); </script>
upload.java
receive session,
string fpath = request.getsession().getattribute("path").tostring();
use commons file upload upload files , send response my.jsp
requestdispatcher rd = request.getrequestdispatcher("geco.jsp"); rd.forward(request, response);// access session set in my.jsp
redirected my.jsp,
on click of button 2, exec.java started.
where, need access session
try { string b = request.getsession().getattribute("path").tostring(); } catch(nullpointerexception e) { b = "no value"; } printwriter out = response.getwriter(); out.println(b);
which being printer null value, do?
from below comments, i've learned loosing session value on page refresh. there way can preserve session value???
thanks comments above, i've understood session getting refreshed.
so,
in my.jsp i've included stuff check existence of session shown below,
<% 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"); } %>
in upload.java, i've received session , added received value session again this,
string fpath = request.getsession().getattribute("path").tostring(); httpsession session = request.getsession(); session.setattribute("path", fpath); //did file upload stuff using apache commons requestdispatcher rd = request.getrequestdispatcher("my.jsp"); rd.forward(request, response);
finally, in mu exec.java after using session invalidating it
httpsession ses = request.getsession(); ses.invalidate();
now, able access session when page being refreshed.
Comments
Post a Comment