我有一个JSP页面,比如xyz.jsp,我在其中设置了一个会话
request.getSession().setAttribute("path", loc);
在a.java中我成功使用了这个会话并将响应发送回jsp页面,
String fpath = request.getSession().getAttribute("path").toString();
但是,当我在b.java中使用相同的会话值时,我得到空指针异常。
my.jsp->按钮点击 - > a.java-> get session1->发送回复my.jsp-> my.jsp reloads-> b.java尝试使用session1- > NullPointerExceptionError
我试过了,
使用不同的会话变量
session.setAttribute("session1", loc);
session.setAttribute("session2", loc);
在a.java中创建一个新的会话变量并在b.java中访问该变量
仍有同样的错误。
my.jsp看起来像,
String loc = "/u/poolla/workspace/FirstServlet/WebContent/WEB-INF/" + ip +"/" +timeStamp;
session.setAttribute("path", loc);
a.java看起来像
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+ " at " +uploadedFile.getAbsolutePath()+ "<br>" + "</span>";
request.setAttribute("f2stat", f2);
}
RequestDispatcher rd = request.getRequestDispatcher("geco.jsp");
rd.forward(request, response);
b.java看起来像
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();
}
答案 0 :(得分:0)
这只是为了帮助您解决问题。我的意思是在jsp中使用scriptlet,在这种情况下使用两个servlet的命名约定都不推荐..
代码段
<强>的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 from session is " + 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 from session is " + loc);
}
控制台日志:
Mar 20, 2014 9:28:33 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 343 ms
location in ServletA from session is tvm
location in ServletB from session is tvm
编辑:执行&#39; gotobjava.go&#39;的jsp;根据OP的编辑请求将操作重命名为page2.jsp