04设置web站点的欢迎页

设置web站点的欢迎页面

webapp的欢迎页面就是当访问这个web站点时如果没有指定具体的资源路径此时默认会访问的页面

  • http://localhost:8080/servlet06/login.html这种访问方式是指定了要访问的就是login.html资源
  • http://localhost:8080/servlet06这种访问方式没有指定具体的资源路径此时默认访问你设置的欢迎页面

在CATALINA_HOME/conf/web.xml文件中进行的配置属于全局配置, Tomcat服务器默认配置的全局欢迎页面index.html, index.htm, index.jsp

<!--如果一个web站点没有设置局部的欢迎页面,Tomcat服务器就会以index.html index.htm index.jsp作为一个web站点的欢迎页面-->
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

设置欢迎页面的路径不需要以“/”开始, 默认就是从web的根下开始查找,一个webapp可设置多个欢迎页(越靠上配置的欢迎页优先级越高)

  • 在web.xml配置文件中的< welcome-file>标签中的配置属于局部配置, 配置文件的生效原则是局部配置优先原则(就近原则)
<!--webapp设置多个欢迎页面,越靠上配置的优先级越高-->
<welcome-file-list>
    <!--多级目录,默认从web的根下开始查找-->
    <welcome-file>page1/page2/page.html</welcome-file>
    <welcome-file>login.html</welcome-file>
</welcome-file-list>

欢迎页可以是静态的资源index.html,也可以是动态的Servlet程序

  • 在web.xml配置文件中配置Servlet的请求路径, 在< welcome-file>标签中配置欢迎页对应的Servlet的请求路径
public class WelcomeServlet extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.print("<h1>welcome to bjpowernode!</h1>");
    }
}
<!--配置Servlet对应的请求路径-->
<servlet>
    <servlet-name>welcomeServlet</servlet-name>
    <servlet-class>com.bjpowernode.javaweb.servlet.WelcomeServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>welcomeServlet</servlet-name>
    <url-pattern>/fdsa</url-pattern>
</servlet-mapping>

<!--访问web站点时默认访问的资源-->
<welcome-file-list>
    <welcome-file>fdsa</welcome-file>
</welcome-file-list>

猜你喜欢

转载自blog.csdn.net/qq_57005976/article/details/131033309