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.
posted 9 years ago
yes.it is working.but problem is now each request is taking as a new user.it doesn't goes to else part.
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.
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.
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.
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...
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.
posted 9 years ago
this also print nothis machang.because it forward the request to the form.jsp to before printing.
it is ?