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
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);
}
...
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>
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