Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 299023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:47:06+00:00 2026-05-12T06:47:06+00:00

I have a design question :) I want to have multiple servlets but I

  • 0

I have a design question 🙂

I want to have multiple servlets but I dont want to configure a new servlet with a new name each time and extend the HttpServlet. I was thinking of having a generic servlet which dispatches different parameters to different specific classes that are actually handling the request.

Ex: theese two calls will be handled by the same generic servlet but dispatched to different services (classes).

/serviceservlet?action=action1&param1=test1&param2=test2
/serviceservlet?action=action2&param21=test

This can be done in many different ways and I dont know for which design to go by. The one I have right now is using guice for bootstrapping and looks like this:

@Singleton
public class ServiceServlet extends HttpServlet {

private static final String PARAM_ACTION = "action";
private final Service1 service1; // Service1 is an interface
private final Service2 service2; // Service2 is another interface

@Inject
public ServiceServlet(final Service1 service1) {
    this.service1 = service1;
}

@Inject
public ServiceServlet(final Service2 service2) {
    this.service2 = service2;
}

@Override
public void doPost(final HttpServletRequest request, final HttpServletResponse response) {
    String action = ServletUtils.getStringParameter(request, PARAM_ACTION);

    if ("action1".equals(action)) {
    ... check that the parameters are correct before executing the below to have typesafety
        service1.process(request.getParameter("param1"), request.getParameter("param2"));
    } else if ("action2".equals(action)) {
        ... check that the parameters are correct before executing the below to have typesafety
        service2.process(request.getParameter("param21"));
    }
}

}

In principle I would like to handle the services in a more generic way and I want it to be as easy as possible to add new services later, but I dont want to loose typesafety. What do you think is the best strategy?

/Br joynes

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:47:06+00:00Added an answer on May 12, 2026 at 6:47 am

    Nowadays web programmers don’t write this kind of code themselves. These use web frameworks like Struts or JSF or Spring (if you’re a Java programmer) which provide elegant implementations of dispatching requests, of mapping from requests to classes that implement the requests. And much, much more.

    However, if you really want to “roll your own” , then I would suggest, first off, that you don’t use an if statment inside your servlet. You should use a map (i.e., HashMap) that maps request URIs to service classes. On servlet init, the map should be populated with instances of the service classes. Inside your servlet add a method, getServiceClass() that return gets the URI and ACTIONS from the HttpServiceRequest and looks up your service class. Better yet, you should map the service classes, not to request parameters, but to URIs: myapp/serviceX maps to ServiceX.class. Define a service interface, Service, with an execute method and call the execute method from the Servlet.

    To sum up:
    1) Load a map of request URIs to service classes when the servlet inits.
    2) Define a service interface between the servlet and the service class.
    3) From the servlet, grab the URI (relevant portion of the URI) from the request and lookup the corresponding Service and call its execute method (or whatever name you choose to give).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement a design in Java where I have multiple event sources
Just wanted opinions on a design question. If you have a C++ class than
I have a question about design winforms. Should I use, or not, group boxes
Please forgive my long question. I have an idea for a design that I
I have a question that is more in the realm of design, than implementation.
I want to attempt an MVC design for my little app. I have a
I have a design issue that I would appreciate some input on. I would
I have started design of a ColdFusion application that is entirely web based. Not
Suppose I have a design like this: Object GUI has two objects: object aManager
For my university assignment I have to design some basic managment system for sicknesses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.