0

In the past week I've had to do a crash course in servlets and jsp for an interview. Anyway I completely fluffed the interview. What really threw me off though was the idea of multiple servlets in a class. Everything I had practiced had just one servlet performing one function. For example:

Products class

protected void doSearch(HttpServletRequest request, HttpServletResponse response)
//Complete method
protected void doNew(HttpServletRequest request, HttpServletResponse response)
//Complete method
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
//Complete method

How does one register these servlets in the web.xml and perform the doGet/doPost ? Does any one know where I can find an example of this type of method ?

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Calgar99
  • 1,670
  • 5
  • 26
  • 42
  • What do you mean by _idea of multiple servlets in a class_. `Servlet` is an interface. Do you mean many inner classes implementing that interface? – Sotirios Delimanolis May 30 '13 at 21:25
  • Forgive me for not being that knowledgble, it could have been innerclass implmenting the interface – Calgar99 May 30 '13 at 21:26
  • 2
    You need to urgently read [How do servlets work? Instantiation, session variables and multithreading](http://stackoverflow.com/q/3106452/1065197) – Luiggi Mendoza May 30 '13 at 21:26
  • 1
    The code you've included looks more like it belongs in one Servlet that can perform several different functions. I don't see anything that implies that multiple servlets are inside a single class. My interpretation of those methods is this: a parameter is passed to a servlet that specifies what behavior is desired. Based on the value of the parameter, the Servlet invokes one of those methods (by passing along the request and response objects). – jahroy May 30 '13 at 21:32
  • @jahroy Where `doDelete()` is inherited from `HttpServlet` and the rest are custom. – Sotirios Delimanolis May 30 '13 at 21:35

4 Answers4

4

I think you are somewhat confused. doDelete() is an overridable method of the HttpServlet class, just like doPut() and doGet(). All of these are called by the service() method (which is part of the Servlet interface and the only method directly called by the Servlet container) to handle the different methods of the HTTP protocol, i.e. doGet() handles GET requests, doDelete() handles DELETE requests, etc. But it's still a single Servlet. You may be familiar only with doGet() and doPost() because the others were rarely used, but that's changing due to the increasing popularity of the REST concept.

But doSearch() and doNew() don't exist in HttpServlet and there are no HTTP methods with those names. If you really saw such methods in code, then they are just regular methods used to divide the code into more manageable pieces - or did you perhaps make up your example code based on an incorrect generalization of what you saw?

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • That is quite possible. So in an index.jsp would the form by like so?
    as opposed to \product.doSearch. Would the doGet/doPost methods be contained with each method or vice versa?
    – Calgar99 May 30 '13 at 21:46
  • @Calgar99 in your `
    ` you can set the `method="GET"` and `method="POST"` that will trigger the execution of `doGet` or `doPost` methods. By default, HTML uses `
    ` unless you change it.
    – Luiggi Mendoza May 30 '13 at 21:56
  • I understand that but would have "protected void doPost(HttpServletRequest request, HttpServletResponse response)" contained within the "doSearch" method or the other way round?. Would you send the parameter of "doSearch" through JSTL or is there a more direct way of executing "doSearch?" – Calgar99 May 30 '13 at 22:08
  • Your `doPost()` mehod would invoke `doSearch()`. There are many ways to pass parameters to Servlets. It all depends on what your front end looks like. For `GET` requests you could pass a parameter in the URL like this: `http://mysite.com/MyServletUrl?action=search` . The Servlet would then check the value of the parameter named `action` (in its `doGet()` method) and decide what method to invoke based on that. I'll add some basic example code to my answer... – jahroy May 30 '13 at 23:25
2

"How does one register these servlets?"

Those aren't servlets. Those are methods (that are probably part of a Servlet).

You could easily invoke doSearch() or doNew() from doGet() and/or doPost().

For example, a parameter could be used to specify which method the Servlet invokes.

In its simplest form, your Servlet's doPost() method might look like this:

protected void doPost(HttpServletRequest req, HttpServletResponse res) {
    String actionValue = req.getParameter("action");
    if ("search".equals(actionValue)) {
        doSearch(req, res);
    }
    else if ("new".equals(actionValue)) {
        doNew(req, res);
    }
}

When you're doing a GET you can specify a parameter in the URL like this:

http://mysite.com/MyServletUrl?action=search

If you're doing a POST you generally specify request parameters with a form.

You could use a dropdown menu or a hidden field, for example.


The doDelete() method is actually already part of the HttpServlet class (as pointed out by others).

jahroy
  • 22,322
  • 9
  • 59
  • 108
1

Java supports inner classes. Inner class can extend HttpServlet and therefore can be written in web.xml as a servlet.

However creating inner classes without real reason (making servlet as an inner class) is extremely bad practice.

Yet another idea is to deploy the same servlet several times in the same web.xml with different init parameters that affect its behavior. This is applicable and good practice that helps to reuse existing code.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

I think you misunderstood the term "servlet". It means a class actually. Multiple servlets means multiple classes which can fulfill different functions.

Kun
  • 21
  • 2