简体   繁体   中英

java servlet. redirect to static page do not work

I have a servlet, who should initially redirect to static html page (example.html)

public class TestServlet extends HttpServlet{

        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws IOException{

            response.sendRedirect("/example.html");
        }

}

But it not works, because on redirect it's again request servlet instead of static page, then redirect, etc.

Why it always asking for servlet, and what should I change to make it simply redirect to my example.html page.

App hosted on Tomcat 7

UPD1: Mapping looks like this:

<servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>com.web.TestServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping> 

add this lines to your web.xml file

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

The reason why it's not work currently, because you configure only single mapping for your requests, and then they handled by your servlet. Adding this mapping you told to your app that you want to handle html file in different (default) manner.

You don't need to amed the web.xml . Instead give the correct URL for the sendRedirect function argument. Remember the sendRedirect function will trigger a fresh request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM