Article directory
foreword
This article mainly introduces common configuration items in Web.xml, including:
- Modification of the web homepage
- Servlet wildcard mapping and initialization parameters
- Global parameter settings
- Page settings for 404, 500 and other error codes
Modification of the home page
可以在web.xml中通过<welcome-file-list>配置访问时后的默认首页
For example:
<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>
The effect is as follows (the index.html file is accessed by default):
the second-level page is also applicable:
Servlet wildcard mapping and initialization parameters
我们可以通过配置通配符捕获到多个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 code layer:
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("王二麻子");
}
}
}
result:
Global parameter settings
Look at a previous example:
configure global parameters in 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>
Get configured global variables in 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 已经写入");
}
}
Effect:
Page settings for 404, 500 and other error codes
The web.xml configuration item configures the error information page through error-page, the example is as follows:
<!-- 报错页面提示 -->
<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>
Error page configuration and result:
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>
Summarize
The above is today's content, including global parameter configuration, web page configuration, wildcard configuration capture request, etc.