• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

session.getAttribute is null?

 
Ranch Hand
Posts: 31
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edit : for the second servlet
The SecondServlet is here.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi William Brogden. Thanks for your reply.
According to you I have tested like this :
 
Bear Bibeault
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And?
 
Bibhudutta Pradhan
Ranch Hand
Posts: 31
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
true

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

HttpSession session = request.getSession(false);
 
I child proofed my house but they still get in. Distract them with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic