Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 870541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:29:40+00:00 2026-05-15T10:29:40+00:00

I just started exploring Java Servlets and JSP and am a little confused about

  • 0

I just started exploring Java Servlets and JSP and am a little confused about the sessions object. Inside a servlet I have this:

public class SampleServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException {

        HttpSession session = request.getSession(true);
        session.setAttribute("_session", "_value");
                response.sendRedirect("page2.jsp");         
        }
}

Now, inside page2.jsp, there is a session object as well, but when I do this

<%
out.print(session.getAttribute("_session"))
%>

it does not seem to get the value (as if it is not set). I tried setting a boolean attribute to true but in the jsp page it returns a false. Can someone tell me the right way of doing this? As to what I am trying to do, I want to share some session variables.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:29:41+00:00Added an answer on May 15, 2026 at 10:29 am

    Use request.getRequestDispatcher().forward() instead of response.sendRedirect();

    So your code would be:

     HttpSession session = request.getSession(true);
     session.setAttribute("_session", "_value");
     request.getRequestDispatcher("page2.jsp").forward();     
    

    Basically, response.sendRedirect() does not preserve session info, so it’s not there when the jsp gets it. request.forward() does preserve the session.

    See http://www.coderanch.com/t/170618/java-Web-Component-SCWCD/certification/sendRedirect-Vs-requestdispatcher-forward

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started exploring SpringMVC and logback. This is my controller, (the only
I have just started this week in exploring Android Open GL ES and related
Just started learning Haskell. I have an empty source file with this inside: pe
I have just started exploring TWILL . Twill is an amazing scripting language for
I just started exploring Scala in my free time. I have to say that
Just started learning PySide and is having problem with QTimer I have this #!/usr/bin/python
I am a newbie just started exploring Grails so that I can recommend this
I have just started exploring Backbone.js. Upon submission of a form I would like
I've just started exploring java NIO, non-blocking IO. I'm interested to know the fundamentals
We have just started exploring what required for internationalizing our applications. One thing I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.