1

I am writing a subclass of HttpServlet. And I want to have a method in that subclass to get the HttpServletRequest. However, it seems HttpServlet does not provide that method.

Is there any way to get round to it?

public class CustomServlet extends HttpServlet {
   public HttpServletRequest getRequest() {
     ...
   }
}
5
  • 2
    What exactly do you think you mean by 'the HttpServletRequest object'? There is one per request. Not one per Servlet. Your question doesn't make sense. Commented May 6, 2019 at 1:15
  • What I mean is to add a method in CustomServlet to get the request object that the servlet is currently processing. Commented May 6, 2019 at 1:39
  • 1
    What makes you think there is only one at a time? Commented May 6, 2019 at 1:53
  • OK, I get it. Can I say it this way? Since there may be more than 1 request being processed by the same Servlet instance, it is not possible to get the request being processed? Commented May 6, 2019 at 3:07
  • Read the answer by @BorLaze. It is possible to get it from inside the Servlet, as it is supplied as a parameter, but otherwise, from outside the Servlet, there is no such thing as 'the request currently being processed'. There could be none, or thousands. Commented May 6, 2019 at 3:30

1 Answer 1

2

HttpServletRequest is a parameter of methods like doGet(), doPost() etc - how do you think to get it from class?

There is no way to do what you want; you can only override some methods and get HttpServletRequest inside them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.