简体   繁体   中英

A new session is created when doing href from one jsp page to another and session variables are returned as null

on first jsp page i have -

<%@ page language="java" %>
<%@ page import="java.io.*,java.util.*" %>
<html>
<head>
<style type="text/css">
body {
    font-family:verdana,arial,sans-serif;
    font-size:10pt;
    margin:10px;
    background-color:#ff9900;
    }
</style>

<%
   session.setAttribute( "theName", "abc" );
%>
<li><a href="http://localhost:8080/auto/second.jsp" target="content">click here!!</a></li><br />
</body>
</html>

on second.jsp, i have,

<%@ page language="java" %>
<%@ page import="java.io.*,java.util.*" %>
<html>
 <head>
Hello, <%out.print(session.getAttribute( "theName" )); %>
</table>
</body>
</html>

But here the value of session variable " theName " is returned as null. I also did a <% out.print( session.getId()); %> <% out.print( session.getId()); %> on both the first and second pages and the issue is that two different session ids are returned like - 230EC6DA6ECD25BC96268942D0ACE5EB (on first page) C2399D151F0D6D6002D16A126EDDC9FB (on second page)

This seems to be the reason that session variable is not getting passed successfully. I also tried putting

<%@ page session="false" %>

so that a new session is not created in second.jsp. But if i use <%@ page session="false" %> in second.jsp, then it throws jasper exception -

Jul 23, 2012 5:23:47 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 32
<%out.print(session.getAttribute( "theName" ));
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:93)
    at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:451)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:328)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:307)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:565)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:843)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:679)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1303)

Kindly suggest as how can I pass session variables from one jsp page to another. Thanks in advance for any help !!

It sounds to me like your JSP/servlet engine is expecting to use URL-rewriting rather than using cookies, for the session ID, for whatever reason. If you use encodeURL to output the link to the second.jsp page, eg:

<a href="<%=response.encodeURL("second.jsp")%>">second page</a>

...you should find that it ends up being second.jsp;jsessionid=longstringhere .

Now, you could use encodeURL all over the place, but my guess is you'd rather use cookies. You'll need to check the configuration of your JSP/servlet engine. In Tomcat, the default is to use cookies , but you might check your Context element to make sure it doesn't have cookies="false" on it or something...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM