Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Default servlet mapping vs RequestDispather(*.html).forward

843841Jun 6 2007
Im having problems with forwarding requests to HTML files while mapping servlet like this :
Mapping #1
<servlet-mapping>
                <servlet-name>MainServlet</servlet-name>
                <url-pattern>/</url-pattern>
</servlet-mapping>
MainServlet :
protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException
        {
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/home.html");
                dispatcher.forward(request,response);
        }
This way i can access my MainServlet via http://localhost:8080/Test/, but i get this error :
javax.servlet.ServletException: WEB2651: Exceeded maximum depth for nested request dispatches: 20
Changing mapping to :
Mapping #2
<servlet-mapping>
                <servlet-name>MainServlet</servlet-name>
                <url-pattern>/servlet</url-pattern>
</servlet-mapping>
Removes that exception, but now MainServlet is accessed via http://localhost:8080/Test/servlet and http://localhost:8080/Test shows my HTML and JSP files which i do not wish user to see. BTW : I do not know the way to make http://localhost:8080/Test/servlet path default (without primitive redirect form welcome-file).

However I found workaround after noticing strange thing :
protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException
        {
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/home.jsp");
                dispatcher.forward(request,response);
        }
Forwarding to JSP works fine. So I figured that *.jsp have default mapping to some servlet and *.html need the same. Since i do not know any ready solution i wrote my own servlet :
FileServlet
protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException
        {
                PrintWriter out = response.getWriter();
                response.setContentType("text/html");
                BufferedReader bf = new BufferedReader( new FileReader(getServletContext().getRealPath(request.getServletPath())) );
                String l;
                while( (l=bf.readLine()) != null ) out.write( l+"\n" );
        }
Now i map servlets as follows :
Mapping #3
<servlet-mapping>
                <servlet-name>MainServlet</servlet-name>
                <url-pattern>/</url-pattern>
        </servlet-mapping>
        <servlet>
                <servlet-name>File</servlet-name>
                <servlet-class>FileServlet</servlet-class>
                <load-on-startup>1</load-on-startup>
        </servlet>

        <!--
        <servlet-mapping>
                <servlet-name>File</servlet-name>
                <url-pattern>*.html</url-pattern>
        </servlet-mapping>
And now i have MainServlet accesed via http://localhost:8080/Test/ and it returns /home.html document as I wanted.

Now, my questions :
1) why doesn't this work without writing my own servlet
2) is there ready servlet for reading html documents ?
3) in last stage (Mapping #3) i can also access http://localhost:8080/Test/home.html, which might be in some cases undisired, so how can i deny access to it in other ways then via MainServlet ?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 4 2007
Added on Jun 6 2007
0 comments
279 views