My main Index.java class
Then st.buttons(1,0) method:
The tomcat OUTPUT:
In this method HttpSession session=req.getSession(); is throwing NPException. How can I fix it please?
geet kaur wrote:you are first setting the request reference to null and then accessing the method getsession() on the req,i think thats why its throwing null pointer exception!
If I am removing null then it is throwing compile time error "variable req might have not been inititalized"
If removing the initialization (which is the right thing to do) is saying "req not initialised", it means there is no request object available for you to use in that part of the code.
Souvvik Basu wrote:As Bear pointed out, you are not supposed to create a request object. An HttpServletRequest object is created by the container when a client requests your servlet.
You mean when this method is called then calling method will invoke HttpServletRequest?
You just USE the object created by the container.
you mean I should change the code like this?
Please correct me if I understood wrong
If you recall the signature for doGet() or doPost() methods, this is how it mostly looks :
When your client sends in a request through the jsp, using a form, the container sees the method of the request, GET, POST etc. Suppose the method is GET. So container invokes the doGet() method in the servlet, and passes it the reference to the HttpServletRequest and HttpServletResponse objects that it created to handle this particular request. The container does not take responsibility for passing references of these objects to any other method that you have written yourself.
Hope this clears the confusion.
Thanks for your help but if you check my code:
1) I am setting session Id while a client access the doGet/doPost method.
2) Then in another method within doGet I am checking that what Id is set in this session
3) And then doing something on basis of this session Id.
My question is how can I use HttpServletRequest without doGet or doPost? Please check my code and make changes as this will be very easily understandable what you are saying.
Bear Bibeault wrote:You can't. Why would you want to?
1) Can you please specify why I can not use it without doGet/doPost?
2) I am coding one main center website and this website has many sub websites of its centers. I am putting check when the client access the domain of main website then I am checking either user has redirected from any center website or directly accessing. The logic behind this process is to forward proper static contents from database according to the center or main website ID. My doGet method sets ID and sub method is displaying records according to this session ID. This is working fine when am doing like this:
from doGet method I am doing:
I want to know why its not working independently