servlet not forwarding session attribute to jsp

后端 未结 2 1011
日久生厌
日久生厌 2021-01-24 12:19

Using embedded tomcat, this code works:

Servlet:

String test = \"test\";
request.setAttribute(\"test\", test);
request.getRequestDispatc         


        
相关标签:
2条回答
  • 2021-01-24 12:29

    You may want to compare the session id in the servlet and the jsp. If they are different maybe check your session and cookie configuration in tomcat

    0 讨论(0)
  • 2021-01-24 12:31

    On the JSP side, you don't need to say request.getSession(), just session.getAttribute();
    And you had a problem in your Main.java when creating the servlet context (a trick of using embedded Tomcat); you were not getting the context created by adding the webapp to tomcat, you had some other context.

    //          File base = new File("src/main/webapp");
    //          context = tomcat.addContext("", base.getAbsolutePath());
    //          tomcat.addWebapp(null, "/", base.getAbsolutePath());
    
            context = tomcat.addWebapp("/", new File("src/main/webapp").getAbsolutePath());
            context.setSessionTimeout(10080);
    

    I commented out your code and changed the context handling and now things work. And a new exception to be caught.

            } catch (ServletException | InterruptedException | LifecycleException exception) {
    
    0 讨论(0)
提交回复
热议问题