Servlet principle
- Servlet is invoked by the web server, and after receiving the browser request, the web server will:
6.4, Mapping problem
- A request can specify a mapping path
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
- A request can specify multiple mapping paths
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello2</url-pattern>
</servlet-mapping>
- A request can specify a generic mapping path
<!--通用映射路径-->
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello/*</url-pattern>
</servlet-mapping>
- default request path
<!--默认请求路径-->
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
- Specify some suffix or prefix, etc...
<!--可以自定义后缀实现请求映射,注意点*前面不能加映射的路径
hello/sfsfssd.tong
-->
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.tong</url-pattern>
</servlet-mapping>
- priority issue
Specifies that the inherent mapping path has the highest priority, and if it cannot be found, it will find the default processing request
<!--404跳转到自己写的界面-->
<servlet>
<servlet-name>error</servlet-name>
<servlet-class>com.tong.servlet.ErrorServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>error</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
6.5、ServletContext
When the web container starts, it will create a corresponding ServletContext object for each web program, which represents the current web application;
1. Sharing data
The data saved in this Servlet can be obtained in another Servlet;
//存数据
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//Servlet上下文
ServletContext context = this.getServletContext();
String name = "tom";//数据
context.setAttribute("username",name);//讲一个数据保存在了ServletContext中,名字为:username,值为tom
}
//取数据
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = (String) context.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字"+username);
}
//配置xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>com.tong.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>getChar</servlet-name>
<servlet-class>com.tong.servlet.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getChar</servlet-name>
<url-pattern>/get</url-pattern>
</servlet-mapping>
</web-app>
Test the access result:
access hello and store the name in it,
access get to retrieve the data and display it
2. Obtain initialization parameters
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().print(url);
}
<!--配置一些web应用初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
<servlet>
<servlet-name>gp</servlet-name>
<servlet-class>com.tong.servlet.ServeltDemo03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>gp</servlet-name>
<url-pattern>/gp</url-pattern>
</servlet-mapping>
3. Request forwarding
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
System.out.println("进入demo04");
//两句话可以合并
// RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");//转发的请求路径
// requestDispatcher.forward(req,resp);//调用forward实现请求转发
context.getRequestDispatcher("/gp").forward(req,resp);
}
<servlet>
<servlet-name>sd4</servlet-name>
<servlet-class>com.tong.servlet.ServletDemo04</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sd4</servlet-name>
<url-pattern>/sd4</url-pattern>
</servlet-mapping>
Added: Redirection
4. Read resource files
properties
- Create new properties in the java directory
- Create new properties in the resources directory
It was found that they were all packaged in the same path: classes, and the path of Express is classpath;
requires a file stream
username=root
password=123456
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(in);
String username = prop.getProperty("username");
String password = prop.getProperty("password");
resp.getWriter().print(username+":"+password);
}
Access test results