aspose file tools*
The moose likes Servlets and the fly likes java.lang.NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "java.lang.NullPointerException" Watch "java.lang.NullPointerException" New topic
Author

java.lang.NullPointerException

anarkali perera
Ranch Hand

Joined: Sep 10, 2009
Posts: 237
this is my jsp


this is my servlet


when i run this jsp and put any name and then it gives this ERROR?\

anyway i delete the cookies also?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5


This code assumes there will always be a "userName" - obviously this is a bad assumption.

Better check for sessionName == null before using it.

Bill
anarkali perera
Ranch Hand

Joined: Sep 10, 2009
Posts: 237
but my problem is how the it goes to the else part when i am using my first request?
when my first request it is a new session?
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey anarkali,

In jsp page, the default value of session attribute in page tag is true. Due to this session is automatically created as soon as you access the jsp.

In your code when you access the jsp page, the session is created. After that when you click on submit button, the request associated with the session is forwarded to the servlet. In servlet you check whether the session is new. As the session was created in jsp, it returns false and start the execution of else section.

If you update your jsp by setting session attribute in page tag. This issue would be resolved. Kindly try the following jsp and let me know if you still face any issues.



Regards
Salil Verma
anarkali perera
Ranch Hand

Joined: Sep 10, 2009
Posts: 237
yes.it is working.but problem is now each request is taking as a new user.it doesn't goes to else part.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

anarkali perera wrote:yes.it is working.but problem is now each request is taking as a new user.it doesn't goes to else part.

What are the changes you made? And what the output you are getting now? Does your request goes to the servlet correctly ?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
anarkali perera
Ranch Hand

Joined: Sep 10, 2009
Posts: 237
this is my jsp now


this is my servlet


yes.it directly goes to servlet.is it bad?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855


With this in JSP you don't have a session associated with it so the request goes to the servlet from the rendered HTML by the JSP, in servlet a new session will be created.
anarkali perera
Ranch Hand

Joined: Sep 10, 2009
Posts: 237
then how do i change this to when the second request comming it goes to else part?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Once you have created the session in the servlet (for the first request) or use the existing one (on subsequent requests) you have the session associated with that particular client. You don't have to do anything other than what you have done in the servlet to make the session work. Try sending the response from the servlet (by forwarding to JSP) and the next request from that will use the existing session if you check that in the servlet. There can be many ways to test this.
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

yes.it is working.but problem is now each request is taking as a new user.it doesn't goes to else part


Servlet would consider only the first request from jsp as a new user. As soon as the request hit servlet, a new session would be created and all further communication would happen with the same session until you close the browser.

Kindly update the doPost in following way and follow the below given steps in sequence, you yourself would see the same session working

1- go to the jsp and enter a value in text box and click on submit
2- you would see "NEW USERRRR" in the browser.
3- Click on back button of the browser and then enter the same value that you entered last time and click on submit
4- you would see YOU ARE OLD USER.
5- Click on back button of the browser and then enter the different value than you entered last time and click on submit
6- you would see "you are a old user with different name" in the browser.

Using this scenario, you would see all the possible way of executions.



anarkali perera
Ranch Hand

Joined: Sep 10, 2009
Posts: 237
i change the servlet to this.but it gives nothing
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

You should forward the request & response to the defined resource in the RequestDispatcher. Add this after "re" initialization:

anarkali perera
Ranch Hand

Joined: Sep 10, 2009
Posts: 237
ok.i change to this


but still print nothing
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

You should provide more details, How are you invoking the servlet (as you are directly invoking it now) and the mappings in web.xml ? Also take look at RequestDispatcher, ServletRequest & ServletContext for more details.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Don't forward the request after sending something to the response. So either remove the p.println statements or remove the forwards.

Again you are not checking if there is a request parameter named name or not. Also if the session is not new and the name in the session is not the same as request parameter name, then you are not sending anything as output...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey anarkali,




In the above code, you would never see the effect of p.println("NEW USERRRR"); reason being at the end you are forwarding the request to the jsp. so the already existing contents would be cleared while forwarding the request to the jsp


I would recommend using the below code in post method instead...



Try this code and execute the code and let me know if you still face any issues.
anarkali perera
Ranch Hand

Joined: Sep 10, 2009
Posts: 237
this also print nothis machang.because it forward the request to the form.jsp to before printing.
it is ?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

anarkali perera wrote:....because it forward the request to the form.jsp to before printing.it is ?

You can either do the printing in the servlet or use the JSP to render what you want. In this case it is the latter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.NullPointerException