wood burning stoves*
The moose likes Servlets and the fly likes session.getAttribute is null? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "session.getAttribute is null? " Watch "session.getAttribute is null? " New topic
Author

session.getAttribute is null?

Bibhudutta Pradhan
Ranch Hand

Joined: Jun 24, 2011
Posts: 31



Here is the Second Servlet



name=null , in the SecondServlet
Whats the problem?
Bibhudutta Pradhan
Ranch Hand

Joined: Jun 24, 2011
Posts: 31

Edit : for the second servlet
The SecondServlet is here.
Mario Alcantara
Greenhorn

Joined: Mar 29, 2011
Posts: 16
When you use response.sendRedirect("someLocation"), the servlet container create a new request and response object, and, if the request has not associated a session, when you call request.getSession(), a new session is created. You can use





insteand of


Thus, you send the current request and response, which have a session associated

PD. Sorry by my english
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

A redirect does create a new request, but it should be participating in the same session. Changing to a forward is usually not an acceptable substitution.

Are you sure that the two servlets are in the same web app? Your redirect URL is also suspect. It should start with the context path.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mario Alcantara
Greenhorn

Joined: Mar 29, 2011
Posts: 16
Bear Bibeault wrote:A redirect does create a new request, but it should be participating in the same session. Changing to a forward is usually not an acceptable substitution.

Are you sure that the two servlets are in the same web app? Your redirect URL is also suspect. It should start with the context path.


Hi Bear!

I have a question, why changing redirect to a forward is usually not an acceptable substitution?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

Precisely because a forward does not create a new request, and a redirect does. When you need a new request, a forward is not acceptable. When you don't need a new request, a redirect is not acceptable.

They are not interchangeable.
Bibhudutta Pradhan
Ranch Hand

Joined: Jun 24, 2011
Posts: 31

Hi Bear!

Both servlets are in the same web-app. Infact in the same package.
The Redirect URL without a context path also works for me.
But I have tried with a context path also. But with no results.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
How about this diagnostic test. In your "second servlet" use the HttpSession method isNew() just to check.

If it is not new, use getAttributeNames to find out what it does have.

Bill
thopex Arcilla
Greenhorn

Joined: Jan 03, 2012
Posts: 10
try something like this in your second servlet:

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String name = (String)session.getAttribute("name");
System.out.println("welcome" +name);
}
Bibhudutta Pradhan
Ranch Hand

Joined: Jun 24, 2011
Posts: 31

Hi William Brogden. Thanks for your reply.
According to you I have tested like this :
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

And?
Bibhudutta Pradhan
Ranch Hand

Joined: Jun 24, 2011
Posts: 31

true

false
Bibhudutta Pradhan
Ranch Hand

Joined: Jun 24, 2011
Posts: 31

I cant understand now, that the same code I did a copy-paste in a new web application.
And it works fine.

I tried the old one by deleting the application from C:\Tomcat 6.0\work\Catalina\localhost\oldProject
don't work.

Then I tried to run it on my friend's computer. But did not work.


Then I created a new project from the scratch. And just copy paste the same code from the old project.
And its just working fine.

May it be a virus effect ??? I cant understand.
Please share your experience.
Niraj Jha
Ranch Hand

Joined: Feb 20, 2013
Posts: 63

try this in second servlet:

HttpSession session = request.getSession(false);
 
Don't get me started about those stupid light bulbs.
 
subject: session.getAttribute is null?
 
Similar Threads
export daa in CSV and store the file in local drive
Sessions on a servlet
Implemented a filter, trying to prevent caching during logout from servlet.
Session Related Question
Can I "cut" the chain in the filter?