• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

session.getAttribute is null?

 
Bibhudutta Pradhan
Ranch Hand
Posts: 31
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here is the Second Servlet



name=null , in the SecondServlet
Whats the problem?
 
Bibhudutta Pradhan
Ranch Hand
Posts: 31
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit : for the second servlet
The SecondServlet is here.
 
Mario Alcantara
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mario Alcantara
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi William Brogden. Thanks for your reply.
According to you I have tested like this :
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And?
 
Bibhudutta Pradhan
Ranch Hand
Posts: 31
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
true

false
 
Bibhudutta Pradhan
Ranch Hand
Posts: 31
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this in second servlet:

HttpSession session = request.getSession(false);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic