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
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.
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/*.
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.
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 *.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With