2019-04-09 | UNLOCK

Java-Servlet 规范

注解列表

名称 描述
@HandlesTypes This annotation is used to declare an array of application classes which are passed to a ServletContainerInitializer.
@HttpConstraint This annotation represents the security constraints that are applied to all requests with HTTP protocol method types that are not otherwise represented by a corresponding HttpMethodConstraint in a ServletSecurity annotation.
@HttpMethodConstraint Specific security constraints can be applied to different types of request, differentiated by the HTTP protocol method type by using this annotation inside the ServletSecurity annotation.
@MultipartConfig This annotation is used to indicate that the Servlet on which it is declared expects requests to made using the multipart/form-data MIME type.
@ServletSecurity Declare this annotation on a Servlet implementation class to enforce security constraints on HTTP protocol requests.
@WebFilter The annotation used to declare a Servlet Filter.
@WebInitParam The annotation used to declare an initialization parameter on a Servlet or Filter, within a WebFilter or WebServlet annotation.
@WebListener The annotation used to declare a listener for various types of event, in a given web application context.
@WebServlet This annotation is used to declare the configuration of an Servlet.

WebServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@WebServlet(name="helloUser2", urlPatterns={"/getUser","/userInfo"},loadOnStartup=1)
public class HelloUser extends HttpServlet{
private static final String DEFAULT_USER="rzm";

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String user=request.getParameter("user");

response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter writer=response.getWriter();
writer.append("<!DOCTYPE html>")
.append("<html><head></head><body>")
.append("用户是"+user)
.append("</body></html>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}

继承HttpServlet重写里面的doGet doPost 等等方法进行参数路由

WebListener

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

@WebListener
public class ContextListener implements ServletContextAttributeListener,ServletContextListener {

@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ServletContext destroyed");
}

@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("ServletContext initialized");
}

@Override
public void attributeAdded(ServletContextAttributeEvent event) {
System.out.println("ServletContext attribute added");
}

@Override
public void attributeRemoved(ServletContextAttributeEvent event) {
System.out.println("ServletContext attribute removed");
}

@Override
public void attributeReplaced(ServletContextAttributeEvent event) {
System.out.println("ServletContext attribute replaced");
}
}

ServletContext与属性监听器

评论加载中