文章目录
前言
本文主要介绍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通配符映射与初始化参数
我们可以通过配置通配符捕获到多个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代码层:
packagepattern;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;/**
* Servlet implementation class PatternServlet
*/publicclassPatternServletextendsHttpServlet{privatestaticfinallong serialVersionUID =1L;/**
* @see HttpServlet#HttpServlet()
*/publicPatternServlet(){super();// TODO Auto-generated constructor stub}/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/protectedvoidservice(HttpServletRequest request,HttpServletResponse response)throwsServletException,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("张三");}elseif("2".equals(index)){
response.getWriter().println("李四");}else{
response.getWriter().println("王二麻子");}}}
结果:
全局参数的设置
看一个之前的例子:
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中获取配置的全局变量
importjava.io.IOException;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;/**
* Servlet implementation class ServletContext
*/@WebServlet("/ServletContext")publicclassServletContextSendTestextendsHttpServlet{privatestaticfinallong serialVersionUID =1L;/**
* @see HttpServlet#HttpServlet()
*/publicServletContextSendTest(){super();// TODO Auto-generated constructor stub}/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/protectedvoiddoGet(HttpServletRequest request,HttpServletResponse response)throwsServletException,IOException{// TODO Auto-generated method stub
response.setContentType("text/html;charset =utf-8");// 获取servlet配置的Web.xml中配置的全局参数nameServletContext servletContext =(ServletContext)this.getServletContext();String name =(String) servletContext.getInitParameter("name");
response.getWriter().println(name);//自定义传入参数
servletContext.setAttribute("newName","zhanglongzhu");
response.getWriter().println("newName 已经写入");}}
效果:
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:
<!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>Insert title here</title></head><body>
页面资源未找到,请检查资源是否存在
</body></html>
500.htmll:
<!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>Insert title here</title></head><body>
error,please call Adminstrator to help you
</body></html>
总结
以上就是今天的内容,包括全局参数配置,web页面配置,统配符配置捕获请求等
版权归原作者 知虚 所有, 如有侵权,请联系我们删除。