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 4044632
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:16:57+00:00 2026-05-20T13:16:57+00:00

Good day! I am planning to do a simple CRUD application. I am planning

  • 0

Good day!

I am planning to do a simple CRUD application. I am planning to make my add, edit, delete and view handled by one controller. Something like this:

@WebServlet(name="ControllerServlet",
            urlPatterns = {"/add","/edit","/delete","/view"}

then just use this code in my controller:

 String userPath = request.getServletPath();
    if (userPath.equals("/add")) {
 }

But I am not sure if this is a common practice. Anyway, I am wondering what is the best practice for this? What are the pros and cons of doing this instead of separating each controller?

Thank you in advance.

  • 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-20T13:16:57+00:00Added an answer on May 20, 2026 at 1:16 pm

    Frankly, the common practice is to adopt a MVC framework. Java EE 6 offers JSF 2.0 out the box as a component based MVC framework. CRUD is possible with a single bean and a single view. You can find a basic example in this answer. The sole controller is provided by JSF itself, the FacesServlet. Other MVC frameworks follows less or more the same ideology.

    If you don’t want to adopt a MVC framework because you would like to learn JSP/Servlets first and/or your project won’t go beyond a CRUD form, then it is hard to point out the “right” approach. At least, the use of multiple URL patterns and if/else statements is a poor sign. You have basically 2 options.

    1. Just use 4 separate servlets. With Servlet 3.0 you don’t need to fiddle with web.xml anymore and it’s really easy to add another servlet class. Each servlet class functions as an “action” class and each one has a clear responsibility.

    2. Use a single servlet, but don’t use multiple URL patterns and don’t use if/else blocks to determine the actions. Map it on a single URL pattern such as /action/* or *.do so that you can invoke it by URLs like action/create, action/read, etc or by create.do, read.do, etc. Then create an interface like follows

      public interface Action {
          void execute(HttpServletRequest request, HttpServletResponse response);
      }
      

      Implement all actions based on this interface, CreateAction, ReadAction, etc and have in your servlet a Map<String, Action> which you fill as follows during init() method:

      actions.put("create", new CreateAction());
      actions.put("read", new ReadAction());
      // ...
      

      And invoke it as follows (assuming an URL pattern of /action/* is been used)

      actions.get(request.getPathInfo().substring(1)).execute(request, response);
      

      That’s also how the average MVC framework works deep under the covers.

    See also:

    • Design patterns web based applications
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day! I'm planning to upgrade WebForms application and I have two possible choices:
G'Day programmers...I am planning to make an application which will be using peer to
Good Day All we are trying to do is inside a trigger make sure
Good day! I want to add and remove CSS files according to the size
Good day everyone, this is one of those areas that is a little over
Good Day, I have two connection strings defined in my web.config file. <connectionStrings> <add
Good Day! I am tying to add keyevent listener to jTextField so that if
Good day! I am planning to create a flash game on my own. Is
Good day, We just converted our web application .NET 1.1 to .NET 2.0. We
Good day, I have a hibernate mapping which goes something like this <class name=Person>

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.