3

we know how to set url pattern for servlet but I am unable to set url pattern for html in web.xml, can u help me to find solution, I googled but, can't able to get it, please find below for my problem.

<servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>auth.Login</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/login</url-pattern>
</servlet-mapping>

in above code I am setting url pattern for **Login** servlet class in web.xml, like wise can I able to set url pattern for html file in web.xml pls help to find solution thank you in advance

Selva
  • 546
  • 5
  • 12
  • 34
  • 1
    Hello, it's unclear what you are asking. Kindly be much more specific including things you have tried, for example regular expressions. Thanks! Hurry too, because this questions may get closed soon. – Drakes Apr 20 '15 at 09:49
  • @Drakes i clearly mentioned in web.xml – Selva Apr 20 '15 at 09:57
  • I can sense that you are frustrated, so I'm going to bow out of this. Your answer is at this link. Best of luck. http://stackoverflow.com/questions/14018215/what-is-url-pattern-in-web-xml-and-how-to-configure-servlet – Drakes Apr 20 '15 at 10:06

3 Answers3

1

URL pattern are for servlet and filters. For servlet

<servlet-mapping>
    <servlet-name>Servlet-name</servlet-name>
    <url-pattern>/< Pattern ></url-pattern>
</servlet-mapping>

For Filter

<filter-mapping>
    <filter-name>Filter-Name</filter-name>
    <url-pattern>/< Pattern ></url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

Those are not for Html file. Infact there are no pattern configuration for JSPs too.

Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
Jafar Ali
  • 1,084
  • 3
  • 16
  • 39
  • no you can't, inface HTML file doesent require any URL patter configuration. In tomcat HTML files can be accessed directly with their corresponding URLs. – Jafar Ali Apr 20 '15 at 10:20
  • I thought that we can set url pattern for html file, then how in the welcome file list alone they are setting index.html as startup page – Selva Apr 20 '15 at 10:44
  • Welcome file is a special case. It is configurable so that one can set welcome page other than default i.e index.html. This way we can also set a servlet as a welcome page. – Jafar Ali Apr 20 '15 at 10:48
1

If you want to protect *.html files from direct access (by placing *.html files under WEB-INF) you can use a Servlet which would be only responsible for forwarding all such requests to intended html files.

<servlet>
    <servlet-name>HTMLServlet</servlet-name>
    <servlet-class>my.package.HTMLServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HTMLServlet</servlet-name>
    <url-pattern>/somepath/*.html</url-pattern>
</servlet-mapping>

Code in servlet class may look like this

...
protected void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws ServletException, IOException {
  String requestedPath = //... code for getting requested HTML path
  request.getRequestDispatcher(requestedPath).forward(request, response);
}
...
justAbit
  • 4,226
  • 2
  • 19
  • 34
1

If you don't mind to change your HTML page to JSP you can set url pattern for it like this:

<servlet>
    <servlet-name>Error</servlet-name>
    <jsp-file>/pages/error.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>Error</servlet-name>
    <url-pattern>/error</url-pattern>
</servlet-mapping>
Alexander Mann
  • 165
  • 1
  • 1
  • 9