Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use single Servlet with different URL pattern?

Tags:

jsp

servlets

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
like image 961
jcrshankar Avatar asked Jan 22 '12 10:01

jcrshankar


People also ask

Can a servlet have multiple url patterns?

Previous versions of the servlet schema allows only a single url-pattern in a filter mapping. For filters mapped to multiple URLs this results in needless repetition of whole mapping clauses.

Can we have multiple url pattern in Web xml?

Yes that works just fine, but that is Servlet 2.4 style and I am trying to avoid the problem of extra typing. because <url-pattern> element is allowed only once under <filter-mapping> . "Multiple <url-pattern> elements should be fine, but the value /einwenig/*.

How do you mapping a servlet to an url pattern?

To map a URL to a servlet, you declare the servlet with the <servlet> element, then define a mapping from a URL path to a servlet declaration with the <servlet-mapping> element.

What does /* mean in url pattern?

URL patterns use an extremely simple syntax. Every character in a pattern must match the corresponding character in the URL path exactly, with two exceptions. At the end of a pattern, /* matches any sequence of characters from that point forward. The pattern *.


1 Answers

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>
like image 165
Ramesh Kotha Avatar answered Dec 05 '22 06:12

Ramesh Kotha