关于一个web站点的欢迎页面

  • 什么是一个web站点的欢迎页面?

  • 对于一个webapp来说,我们是可以设置它的欢迎页面的。

  • 设置了欢迎页面之后,当你访问这个webapp的时候,或者访问这个web站点的时候,没有指定任何"资源路径",这个时候会默认访问你的欢迎页面。

  • 我们一般的访问方式是:

  • http://localhost:8080/servlet06/login.html 这种方式是指定了要访问的就是login.html资源。

  • 如果我们访问的方式是:

  • http://localhost:8080/servlet06 如果我们访问的就是这个站点,没有指定具体的资源路径。它默认会访问谁呢?

  • 默认会访问你设置的欢迎页面。

  • 怎么设置欢迎页面呢?

  • 第一步:我在IDEA工具的web目录下新建了一个文件login.html

  • 第二步:在web.xml文件中进行了以下的配置

  • ```xml

<welcome-file-list>

<welcome-file>login.html</welcome-file>

</welcome-file-list>

```

  • 注意:设置欢迎页面的时候,这个路径不需要以"/"开始。并且这个路径默认是从webapp的根下开始查找。

  • 第三步:启动服务器,浏览器地址栏输入地址

  • http://localhost:8080/servlet07

  • 如果在webapp的根下新建一个目录,目录中再给一个文件,那么这个欢迎页该如何设置呢?

  • 在webapp根下新建page1

  • 在page1下新建page2目录

  • 在page2目录下新建page.html页面

  • 在web.xml文件中应该这样配置

  • ```

<welcome-file-list>

<welcome-file>page1/page2/page.html</welcome-file>

</welcome-file-list>

```

  • 注意:路径不需要以"/"开始,并且路径默认从webapp的根下开始找。

  • 一个webapp是可以设置多个欢迎页面的

  • ```xml

<welcome-file-list>

<welcome-file>page1/page2/page.html</welcome-file>

<welcome-file>login.html</welcome-file>

</welcome-file-list>

```

  • 注意:越靠上的优先级越高。找不到的继续向下找。

  • 你有没有注意一件事:当我的文件名设置为index.html的时候,不需要在web.xml文件中进行配置欢迎页面。这是为什么?

  • 这是因为Tomcat服务器已经提前配置好了。

  • 实际上配置欢迎页面有两个地方可以配置:

  • 一个是在webapp内部的web.xml文件中。(在这个地方配置的属于局部配置)

  • 一个是在CATALINA_HOME/conf/web.xml文件中进行配置。(在这个地方配置的属于全局配置)

  • ```xml

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

```

  • Tomcat服务器的全局欢迎页面是:index.html index.htm index.jsp。如果你一个web站点没有设置局部的欢迎页面,Tomcat服务器就会以index.html 或者index.htm或者 index.jsp作为一个web站点的欢迎页面。

  • 注意原则:局部优先原则。(就近原则)

  • 欢迎页可以是一个Servlet吗?

  • 当然可以。

  • 欢迎页就是一个资源,既然是一个资源,那么可以是静态资源,也可以是动态资源。

  • 静态资源:index.html welcome.html .....

  • 动态资源:Servlet类。

  • 步骤:

  • 第一步:写一个Servlet

  • ```java

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>");

}

}

```

  • 第二步:在web.xml文件中配置servlet

  • ```xml

<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/fds/a/fds/af/ds/af/dsafdsafdsa</url-pattern>

</servlet-mapping>

```

  • 第三步:在web.xml文件中配置欢迎页

  • ```xml

<welcome-file-list>

<welcome-file>fdsa/fds/a/fds/af/ds/af/dsafdsafdsa</welcome-file>

</welcome-file-list>

```

相关推荐
xchenhao2 小时前
基于 Flutter 的开源文本 TTS 朗读器(支持 Windows/macOS/Android)
android·windows·flutter·macos·openai·tts·朗读器
帽儿山的枪手4 小时前
追踪网络流量就这么简单 | 进阶篇 | conntrack
linux·windows·网络协议
兮动人5 小时前
Windows 11 系统关键文件夹详解及安全清理指南
windows·安全
LabVIEW开发9 小时前
LabVIEW调用外部DLL
windows·labview·labview知识·labview功能·labview程序
biubiubiu07069 小时前
FFmpeg Windows安装
windows·ffmpeg
哆啦A梦——11 小时前
dll文件缺失解决方法
windows
漠效12 小时前
Duplicate cleaner pro 的使用技巧
windows·经验分享
DogDaoDao13 小时前
Windows下VScode配置FFmpeg开发环境保姆级教程
windows·vscode·ffmpeg·音视频·gcc
Rudon滨海渔村14 小时前
exe文件图标修改器 - exe图标提取器(ico、png) - 修改360文件夹的图标为windows自带的图标
windows
无名小猴14 小时前
Windows软件卸载
windows