简体   繁体   English

Java Servlet。 重定向到静态页面不起作用

[英]java servlet. redirect to static page do not work

I have a servlet, who should initially redirect to static html page (example.html) 我有一个servlet,最初应该将其重定向到静态html页面(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. 但是它不起作用,因为在重定向时,它再次请求servlet而不是静态页面,然后重定向,依此类推。

Why it always asking for servlet, and what should I change to make it simply redirect to my example.html page. 为什么它总是要求servlet,我应该更改什么以使其简单地重定向到我的example.html页面。

App hosted on Tomcat 7 托管在Tomcat 7上的应用

UPD1: Mapping looks like this: UPD1:映射如下所示:

<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 将此行添加到您的web.xml文件

<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. 它之所以当前无法使用的原因,是因为您只为请求配置单个映射,然后由您的servlet处理。 Adding this mapping you told to your app that you want to handle html file in different (default) manner. 将您告诉您的此映射添加到您的应用中,您希望以其他(默认)方式处理html文件。

You don't need to amed the web.xml . 您无需修改web.xml Instead give the correct URL for the sendRedirect function argument. 而是为sendRedirect函数参数提供正确的URL。 Remember the sendRedirect function will trigger a fresh request. 请记住, sendRedirect函数将触发一个新的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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