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.

4
  • 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. Commented May 4, 2015 at 9: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? Commented May 4, 2015 at 9: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.:) Commented May 4, 2015 at 9:37
  • Yes @swinkler. Using single servlet helloworld the requested url pattern should be categorized. Commented May 4, 2015 at 9:38

4 Answers 4

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

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

1 Comment

Thanks @swinkler Yes this is the answer what i am expecting . This link is helped me too : stackoverflow.com/questions/4931323/…
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();
               }
      }

}

3 Comments

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.
Are you able to work with spring-mvc framework? If yes, you can do what you want easily using MultiActionController. @MadhukarHebbar
Yes it can be achievable using many ways, but as per the question i am asking is it able to implement using servlets. Thanks
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.

Comments

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>.

1 Comment

This is not the answer i am looking for. i need to call some function based on url pattern. Please check the updated question.

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.