-1

I am working on the Web Project, and i want to get the information of the request object in our simple java program which is not extending the HttpServlet class. The same problem i am facing in the @init() of servlet , i want to call some functions in init() in that i need request object , but i am not getting how can i do this functionality.

please do not post any answer with related to the Spring technology :)

I googles alot but didn't find anything for this.Please help me out.

Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
  • show your java class and servlet class if you need help. – Nomesh DeSilva Sep 24 '15 at 05:48
  • 2
    When the servlet initializes itself, there is no request yet. Requests come in after the servlet has been initialized, when someone actually uses the application. If you need access to a request in a class, pass it as argument. – JB Nizet Sep 24 '15 at 06:05
  • Access request object in init method does not make sense.As @JB Nizet said,When the servlet initializes itself, there is no request yet.After the web container loads and instantiates the servlet class and before it delivers requests from clients, the web container initializes the servlet.In this case,even google can't help. – RockAndRoll Sep 24 '15 at 07:07

1 Answers1

1

First of all, HttpServletRequest and HttpServletResponse are interfaces.

The implementation classes for these interfaces are provided by the application server (server container) vendor (like Tomcat, JBoss, Glassfish, etc..).

When the application server (where the your web application is deployed), receives the request from the client, the objects for the HttpServletRequest and HttpServletResponse implementation classes are created. And the creation of these objects happens for each hit (request) from client.

In general, these request/response objects (created by container) will be passed to the HttpServlet class as method parameters, from which we will retrieve the data the client intended to send to the server/servlet methods inside doGet() or doPost() (using request.getParameters() method).

Also refer below docs to know more about the HttpServletRequest and HttpServletResponse are interfaces.

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html

Vasu
  • 21,832
  • 11
  • 51
  • 67