java - Sessions showing null error -
i have jsp page, xyz.jsp in i've set session
request.getsession().setattribute("path", loc);
and in a.java i've used session , sent response jsp page,
string fpath = request.getsession().getattribute("path").tostring();
but, when use same session value in b.java getting null pointer exception.
my.jsp->on button click->a.java->get session1->send response my.jsp->my.jsp reloads->b.java trying use session1->nullpointerexceptionerror
i've tried,
using different session variables
session.setattribute("session1", loc); session.setattribute("session2", loc);
creating new session variable in a.java , access same in b.java
still same error.
my.jsp looks like,
string loc = "/u/poolla/workspace/firstservlet/webcontent/web-inf/" + ip +"/" +timestamp; session.setattribute("path", loc);
a.java looks like
protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { httpsession session = request.getsession(); string fpath = session.getattribute("path").tostring(); if(!new file(fpath).exists()) { file uploadedfile = new file(fpath, filename); item.write(uploadedfile); two_file = "right file " +filename; request.setattribute("file2", two_file); string f2 = "<span class='blue'>" +"uploaded file " +filename+ " @ " +uploadedfile.getabsolutepath()+ "<br>" + "</span>"; request.setattribute("f2stat", f2); } requestdispatcher rd = request.getrequestdispatcher("geco.jsp"); rd.forward(request, response);
b.java looks like
public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { httpsession session = request.getsession(); string b = session.getattribute("path").tostring(); file file = new file(b); if(!file.exists()) { file.createnewfile(); } filewriter fw = new filewriter(file.getabsolutefile()); bufferedwriter bw = new bufferedwriter(fw); bw.write("hi"); bw.close(); }
this problem. mean using scriptlets in jsp, naming conventions, using 2 servlets in scenario not recommended..
code snippets
web.xml
<servlet> <servlet-name>myservleta</servlet-name> <servlet-class>com.examples.example.myservleta</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>myservletb</servlet-name> <servlet-class>com.examples.example.myservletb</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>myservleta</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>myservletb</servlet-name> <url-pattern>*.go</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
index.jsp
<body> <% session.setattribute("location", "tvm"); %> <form action="gotoajava.do" method="post"> <input type="submit" /> </form> </body>
page2.jsp
<body> <form action="gotobjava.go" method="get"> <input type="submit" /> </form> </body>
myservleta.java
protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string loc = request.getsession().getattribute("location").tostring(); system.out.println("location in servleta session " + loc); requestdispatcher rd = request.getrequestdispatcher("geco.jsp"); rd.forward(request, response); }
myservletb.java
protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { httpsession session = request.getsession(); string loc = session.getattribute("location").tostring(); system.out.println("location in servletb session " + loc); }
console log:
mar 20, 2014 9:28:33 org.apache.catalina.startup.catalina start info: server startup in 343 ms location in servleta session tvm location in servletb session tvm
edit: jsp performs 'gotobjava.go' action renamed page2.jsp per op's edit request
Comments
Post a Comment