1、欢迎页面怎么设置?
- 假设在WebRoot目录下创建login.html,想让login.html作为整个webapp的欢迎页面。应该做这样的设置,编写web.xml文件:
-
<welcome-file-list> <welcome-file>login.html</welcome-file> </welcome-file-list>
-
- 假设在WebRoot目录下创健html目录,html目录中创健welcome.html,想让welcome.html作为整个webapp的欢迎页面。应该做这样的设置,编写web.xml文件:
-
<welcome-file-list> <welcome-file>html/welcome.html</welcome-file> </welcome-file-list>
-
2、为什么设置欢迎页面?
为了访问更方便,为了提高用户的体验。
设置欢迎页面之后,直接在浏览器地址栏上访问该webapp即可,自动定位到欢迎页面,例如
http://localhost:8080/servlet-06
直接访问的就是login.html。
3、欢迎页面可以设置多个,越靠上面的优先级越高
<welcome-file-list> <welcome-file>login.html</welcome-file> <welcome-file>html/welcome.html</welcome-file> </welcome-file-list>
以上的配置:优先选择login.html,若这个资源不存在,才会选择html/welcome.html作为欢迎页面。
4、注意,欢迎页面设置的时候,路径不需要以“/”开头。
<welcome-file>login.html</welcome-file>
要求在webapp的根目录下,必须有一个文件,叫做login.html
<welcome-file>html/welcome.html</welcome-file>
要求在webapp的根目录下,必须有一个文件夹,叫做html,该文件夹中必须有一个文件叫做html。
5、一个webapp的欢迎页面不一定是一个HTML资源,可以是任何一种类型的web资源。比如:欢迎页面可以是servlet。
6、欢迎页面包括全局配置和局部配置:
- 全局配置:CATALINA_HOME/conf/web.xml
- 局部配置:CATALINA_HOME/webapps/webapp/WEB-INF/web.xml
- 注意:就近原则
- 若一个页面的名称是: index.html、index.htm、index.jsp,这些都是默认的欢迎页面已经在全局配置中已经配置过了。
附:
login.html
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>后台管理</title> <link href="css/admin_login.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="admin_login_wrap"> <h1>后台管理</h1> <div class="adming_login_border"> <div class="admin_input"> <form action="index.html" method="post"> <ul class="admin_items"> <li> <label for="user">用户名:</label> <input type="text" name="username" value="admin" id="user" size="40" class="admin_input_style" /> </li> <li> <label for="pwd">密码:</label> <input type="password" name="pwd" value="admin" id="pwd" size="40" class="admin_input_style" /> </li> <li> <input type="submit" tabindex="3" value="提交" class="btn btn-primary" /> </li> </ul> </form> </div> </div> <p class="admin_copyright"><a tabindex="5" href="#">返回首页</a> © 2021 Powered by <a href="http://jscss.me" target="_blank">我喜欢你</a></p> </div> </body> </html>
admin_login.css
@charset "utf-8"; *{margin:0;padding:0;} body{font:14px/1.5 '微软雅黑';background:#f9f9f9;} a{color:#00007F;text-decoration:none;} a:hover{color:#bd0a01;text-decoration:underline;} .admin_login_wrap{margin:140px auto 0;width:312px;} .admin_login_wrap h1{font-family:'微软雅黑';font-weight: normal;font-size:20px;margin-bottom:15px;} .adming_login_border{box-shadow:1px 2px 3px #ccc;border:1px solid #eee;background:#fff;padding:0 5px 15px;overflow:hidden;} .admin_input{margin:15px auto 0;width:280px;} .admin_items li{line-height:28px;margin-bottom: 5px;list-style:none;clear: both;} .admin_input label{display:block;margin-bottom:5px;} .admin_input_style{border:1px solid #e6e6e6;background:#fff;padding:3px;height:30px;line-height:30px;font-family:Arial;color:#666;border-radius:5px;transition-duration:0.3s;} .admin_input_style:focus{border-color: rgba(82, 168, 236, 0.8);box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);background:#f8f8f8;} .btn{display:inline-block;*display:inline;padding:12px 12px;margin-bottom:0;*margin-left:.3em;font-size:14px;line-height:20px;color:#333;text-align:center;text-shadow:0 1px 1px rgba(255,255,255,0.75);vertical-align:middle;cursor:pointer;background-color:#f5f5f5;*background-color:#e6e6e6;background-image:-moz-linear-gradient(top,#ffffff,#e6e6e6);background-image:-webkit-gradient(linear,0 0,0 100%,from(#ffffff),to(#e6e6e6));background-image:-webkit-linear-gradient(top,#ffffff,#e6e6e6);background-image:-o-linear-gradient(top,#ffffff,#e6e6e6);background-image:linear-gradient(to bottom,#ffffff,#e6e6e6);background-repeat:repeat-x;border:1px solid #bbbbbb;*border:0;border-color:#e6e6e6 #e6e6e6 #bfbfbf;border-color:rgba(0,0,0,0.1) rgba(0,0,0,0.1) rgba(0,0,0,0.25);border-bottom-color:#a2a2a2;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffffff',endColorstr='#ffe6e6e6',GradientType=0);filter:progid:DXImageTransform.Microsoft.gradient(enabled=false);*zoom:1;-webkit-box-shadow:inset 0 1px 0 rgba(255,255,255,0.2),0 1px 2px rgba(0,0,0,0.05);-moz-box-shadow:inset 0 1px 0 rgba(255,255,255,0.2),0 1px 2px rgba(0,0,0,0.05);box-shadow:inset 0 1px 0 rgba(255,255,255,0.2),0 1px 2px rgba(0,0,0,0.05);} .btn:hover,.btn:active,.btn.active,.btn.disabled,.btn[disabled]{color:#333;background-color:#e6e6e6;*background-color:#d9d9d9;} .btn:active,.btn.active{background-color:#cccccc 9;} .btn:first-child{*margin-left:0;} .btn:hover{color:#333333;text-decoration:none;background-position:0 -15px;-webkit-transition:background-position 0.1s linear;-moz-transition:background-position 0.1s linear;-o-transition:background-position 0.1s linear;transition:background-position 0.1s linear;} .btn:focus{outline:thin dotted #333;outline:5px auto -webkit-focus-ring-color;outline-offset:-2px;} .btn.active,.btn:active{background-image:none;outline:0;-webkit-box-shadow:inset 0 2px 4px rgba(0,0,0,0.15),0 1px 2px rgba(0,0,0,0.05);-moz-box-shadow:inset 0 2px 4px rgba(0,0,0,0.15),0 1px 2px rgba(0,0,0,0.05);box-shadow:inset 0 2px 4px rgba(0,0,0,0.15),0 1px 2px rgba(0,0,0,0.05);} .btn-primary{width: 100%;color:#fff;text-shadow:0 -1px 0 rgba(0,0,0,0.25);background-color:#006dcc;*background-color:#0044cc;background-image:-moz-linear-gradient(top,#0088cc,#0044cc);background-image:-webkit-gradient(linear,0 0,0 100%,from(#0088cc),to(#0044cc));background-image:-webkit-linear-gradient(top,#0088cc,#0044cc);background-image:-o-linear-gradient(top,#0088cc,#0044cc);background-image:linear-gradient(to bottom,#0088cc,#0044cc);background-repeat:repeat-x;border-color:#0044cc #0044cc #002a80;border-color:rgba(0,0,0,0.1) rgba(0,0,0,0.1) rgba(0,0,0,0.25);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff0088cc',endColorstr='#ff0044cc',GradientType=0);filter:progid:DXImageTransform.Microsoft.gradient(enabled=false);} .btn-primary:hover,.btn-primary:active,.btn-primary.active,.btn-primary.disabled,.btn-primary[disabled]{color:#ffffff;background-color:#0044cc;*background-color:#003bb3;} .btn-primary:active,.btn-primary.active{background-color:#003399 9;} .admin_copyright{color:#585858;margin-top:10px;font-family:Arial;text-align:center;} .admin_copyright a:link,.admin_copyright a:visited{color:#f90;} .admin_copyright a:hover{color:#f90;}
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <!-- 欢迎页面的设置 --> <!-- <welcome-file-list> <welcome-file>login.html</welcome-file> </welcome-file-list> --> <servlet> <servlet-name>welcome page</servlet-name> <servlet-class>/myweb.WelcomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>welcome page</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>welcome</welcome-file> <welcome-file>html/welcome.html</welcome-file> <welcome-file>login.html</welcome-file> </welcome-file-list> </web-app>
welcome.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>welcome page</title> </head> <body> welcome page </body> </html>
WelcomeServlet.java
package myweb; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class WelcomeServlet implements Servlet { @Override public void destroy() { } @Override public ServletConfig getServletConfig() { return null; } @Override public String getServletInfo() { return null; } @Override public void init(ServletConfig arg0) throws ServletException { } @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.print("<html>"); out.print("<head>"); out.print("<title>AServlet</title>"); out.print("</head>"); out.print("<body>"); out.print("我是一个servlet"); out.print("</body>"); out.print("</html>"); } }