12

I need to use a single servlet with different URL pattern. I have given try in the tomcat server as below. but I want to know the real coding standard please help me out?

String servletPath = request.getServletPath();
    
    if("/HelloServletone".equalsIgnoreCase(servletPath))
    {
        System.err.println("1?*");
                ///logic move to controller one

    }
    
    if("/HelloServlettwo".equalsIgnoreCase(servletPath))
    {
           System.err.println("2*");
           ///logic move to controller two
    }

web.xml

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

 <servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloServletone</url-pattern>
</servlet-mapping>

 <servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloServlettwo</url-pattern>
</servlet-mapping>

 

jsp1:

<form method="post" action="HelloServletone">
  //getting inputs and move to servlet
<input type="submit" value="login " />

jsp2:

<form method="post" action="HelloServlettwo">
  //getting inputs and move to servlet
8
  • I the servlet does two different things based on the path used to invoke it, why not create two different servlets? Commented Jan 22, 2012 at 10:30
  • Nizet my aim is using single servlet and having mutiple controller.. for each different jsp.. Commented Jan 22, 2012 at 13:39
  • Unless this is for learning, you're reinventing the wheel. Use an MVC framework which will do it for you: Stripes, Spring MVC, Struts2, whatever. Commented Jan 22, 2012 at 13:41
  • so your suggestion is, have to use different servlet for different logic... is there is any way to define different methods in servlet and calling it from different jsp... if yes, we can use single servlet right any thoughts NIzet? Commented Jan 22, 2012 at 13:46
  • That's the kind of functionality that is offered by an MVC framework like Stripes, Spring MVC, etc. Unless you're trying to implement such a framework for fun or to learn, pick one of them (Stripes is very easy and really well designed) and use it. Commented Jan 22, 2012 at 13:52

1 Answer 1

15

you can use multiple URL's in one servlet 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>
Sign up to request clarification or add additional context in comments.

3 Comments

ramesh even i done the same.... but could u please tell me how to handle these url pattern in single servlet...
ramesh my need is passing this url pattern from different jsp and identifying it in servlet... in standard way..

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.