文章目录

  • 前言
  • 首页的修改
  • Servlet通配符映射与初始化参数
  • 全局参数的设置
  • 404,500等错误码的页面设置
  • 总结

前言

本文主要介绍Web.xml常用配置项,包括:

  • web首页的修改
  • Servlet通配符映射与初始化参数
  • 全局参数的设置
  • 404,500等错误码的页面设置

首页的修改

可以在web.xml中通过<welcome-file-list>配置访问时后的默认首页

例如:

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>

效果如下(默认访问到了index.html文件):
Servlet——web.xml配置
二级页面同样适用:
Servlet——web.xml配置

Servlet通配符映射与初始化参数

我们可以通过配置通配符捕获到多个Servlet请求,例如:
配置映射地址
<!-- 统配Servlet -->
    <!-- 声明servlet -->
  <servlet>
  <!-- Servlet别名 -->
  	<servlet-name>pattern</servlet-name>
  	<servlet-class>pattern.PatternServlet</servlet-class>
  </servlet>
  <!-- 将Servlet和URL绑定 -->
  <servlet-mapping>
  	<servlet-name>pattern</servlet-name>
  	<url-pattern>/pattern/*</url-pattern>
  </servlet-mapping>

Servlet代码层:

package pattern;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class PatternServlet
 */
public class PatternServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PatternServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url =request.getRequestURL().toString();
		response.setContentType("text/html;charset=utf-8");
		System.out.println(url);
		String index =url.substring(url.lastIndexOf('/')+1);
		response.getWriter().println(index);
		if("1".equals(index)) {
			response.getWriter().println("张三");
		}else if("2".equals(index)) {
			response.getWriter().println("李四");
		}else {
			response.getWriter().println("王二麻子");
		}
	}
}

结果:
Servlet——web.xml配置

Servlet——web.xml配置

全局参数的设置

看一个之前的例子:
xml中配置全局参数

 <!-- ServletContext -->
   <context-param>
         <param-name>name</param-name>
         <param-value>wangwei</param-value>
  </context-param>
   <context-param>
         <param-name>age</param-name>
         <param-value>24</param-value>
  </context-param>

servlet中获取配置的全局变量


import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class ServletContext
 */
@WebServlet("/ServletContext")
public class ServletContextSendTest extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletContextSendTest() {
        super();
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset =utf-8");
		// 获取servlet配置的Web.xml中配置的全局参数name
		ServletContext servletContext = (ServletContext) this.getServletContext();
		String name =(String)  servletContext.getInitParameter("name");
		response.getWriter().println(name);
		//自定义传入参数
		servletContext.setAttribute("newName", "zhanglongzhu");
		response.getWriter().println("newName 已经写入");
	}
}

效果:
Servlet——web.xml配置

404,500等错误码的页面设置

web.xml配置项通过error-page配置错误信息页面,示例如下:

<!-- 报错页面提示 -->
<error-page>
	<error-code>404</error-code>
	<location>/error/404.html</location>
</error-page>
 <error-page>
	<error-code>500</error-code>
	<location>/error/500.html</location>
</error-page>

错误页面配置及结果:
404.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
页面资源未找到,请检查资源是否存在
</body>
</html>

500.htmll:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
error,please call Adminstrator to help you
</body>
</html>

Servlet——web.xml配置
Servlet——web.xml配置

总结

以上就是今天的内容,包括全局参数配置,web页面配置,统配符配置捕获请求等

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。