Url template for html file in web.xml

we know how to set url template for servlet but i cant set url template for html in web.xml can help me find solution i googled but cant get it please find below 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 the above code i am setting url pattern for **Login**

servlet class in web.xml for example i can set url pattern for html file in web.xml pls help find a solution in advance

+3


source to share


3 answers


URL pattern for servlets 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>

      

This is not for the Html file. Infact there is no template config for JSP either.

+1


source


If you want to protect * .html files from direct access (by putting * .html files in WEB-INF), you can use a servlet that will only be responsible for forwarding all such requests to the 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>

      



The code in the servlet class might 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);
}
...

      

+1


source


If you don't mind changing your HTML page to JSP, you can set the 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>

      

+1


source







All Articles