4

I am using servlet to get request from frontend. Am i able to make single servlet which could do multiple operation based on url pattern? Here will be my url mapping

<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
<url-pattern>/HelloServletOne</url-pattern>
<url-pattern>/HelloServletTwo</url-pattern>
</servlet-mapping>

That means if i hit to the url as framed below it should invoke its own functionalities.

  • URL:/HelloServlet: it should do function 1
  • URL:/HelloServletOne: it should do function 2
  • URL:/HelloServletTwo: it should do function 3 etc.

How can i achive this by extending servlet.?
Code/link examples are much appreciated.

Madhukar Hebbar
  • 3,113
  • 5
  • 41
  • 69
  • 1
    It seems likely that you could do that, but a different approach might be to use one servlet mapping but use the HTTP verbs to differentiate between the actions that you take in a way that is similar to REST. goGet method retrieves, doPut method updates, doPost inserts, doDelete deletes. I think I'd go in that direction rather than the servlet mapping approach you suggest. – DaveH May 04 '15 at 09:20
  • Just to understand you right - you want to use one servlet (HelloWorld). Based on the URL a different method inside your HelloWorld servlet should be invoked? – swinkler May 04 '15 at 09:21
  • @DaveHowes It can be achievable using rest service. but i am not good at that.If i am able to achieve this using servlet my work will get easier.:) – Madhukar Hebbar May 04 '15 at 09:37
  • Yes @swinkler. Using single servlet helloworld the requested url pattern should be categorized. – Madhukar Hebbar May 04 '15 at 09:38

4 Answers4

11

Regarding your url-pattern you need to know what URL was called. Because a request can be made due to different http-methods (GET, POST etc.) you can use parts of the FrontController Pattern

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {

  private static final String SERLVET = "HelloServlet";
  private static final String SERLVET_ONE = "HelloServletOne";
  private static final String SERLVET_TWO = "HelloServletTwo";

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    processRequest(req, resp);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    processRequest(req, resp);
  }

  private void processRequest(HttpServletRequest req, HttpServletResponse resp) {
    String path = req.getServletPath();
    switch (path) {
      case SERLVET:
        // ... call your function1
        break;
      case SERLVET_ONE:
        // ... call your function2
        break;

      case SERLVET_TWO:
        // ... call your function3
        break;
      default:
        break;
    }

    // do something else
  }

}

The getServletPath method may only work for explicit url-patterns like you have given. For other information on the URL check this link

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
swinkler
  • 1,703
  • 10
  • 20
  • Thanks @swinkler Yes this is the answer what i am expecting . This link is helped me too : http://stackoverflow.com/questions/4931323/whats-the-difference-between-getrequesturi-and-getpathinfo-methods-in-httpservl – Madhukar Hebbar May 04 '15 at 09:56
0

You should not use three different Servlet for this purpose. You should use different methods of Servlet to achieve this.

Use doGet method for get data.
Use doPost method for insert data.
Use doPut method for update data.
Use doDelete method for delete data.

Please refer servlet api documentation for more details.

EDIT:
Read more about this here. It says the url mapping you have provided must work if you are working with servlet api version 2.5 or greater.

Also, please make sure that you have provided fully qualified name of servlet class in <servlet-name>.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Vishal Zanzrukia
  • 4,902
  • 4
  • 38
  • 82
0

You can handle multiple requests by the same servlet by making a contract to have a request parameter like 'ACTION'. Then in your forms add this as hidden field with values like 'ACTION_1' and 'ACTION_2' and 'ACTION_3'. So, in doPost() you can check this parameter value and can invoke respective handling methods in same servlet.

class YourServlet extends HttpServlet{

      public void doPost(HttpReq req, HttpResp resp){
               String action = reg.getParameter('ACTION');
               if('ACTION_1'.equals(action)){
                   doAction_1();
               }
               if('ACTION_2'.equals(action)){
                   doAction_2()
               } 
               if('ACTION_3'.equals(action)){
                   doAction_3()
               }
               else {
                   defaultAction();
               }
      }

}
MChaker
  • 2,610
  • 2
  • 22
  • 38
  • This can be achieved in single url pattern. But for me client side api's are already exposed and am not able to do any changes. Using those url pattern i need to differentiate the request come from client side. – Madhukar Hebbar May 04 '15 at 09:35
  • Are you able to work with `spring-mvc` framework? If yes, you can do what you want easily using `MultiActionController`. @MadhukarHebbar – MChaker May 04 '15 at 09:38
  • Yes it can be achievable using many ways, but as per the question i am asking is it able to implement using servlets. Thanks – Madhukar Hebbar May 04 '15 at 09:40
0

I made into.

HttpServletRequest.getRequestURI() returns the URL pattern including /* with query parameter if exist, and HttpServletRequest.getPathInfo() returns the part matched by /* (or null for exact match).

Here in my case i need getPathInfo() where it will returns HelloServlet,HelloServletOne or HelloServletTwo based on request. Thanks.

Madhukar Hebbar
  • 3,113
  • 5
  • 41
  • 69