【Java Web开发学习】Servlet
转发:https://www.cnblogs.com/yangchongxing/p/9274739.html
1、Servlet
package cn.ycx.servlet; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("myservlet");
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
web.xml中的配置
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>cn.ycx.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
servlet3.0知乎新增了注解@WebServlet
属性 | 类型 | 描述 |
name | String | 指定 Servlet 的 name 属性,等价于 <servlet-name>。如果没有显式指定,则该 Servlet 的取值即为类的全限定名 |
value | String[] | 该属性等价于 urlPatterns 属性。两个属性不能同时使用 |
urlPatterns | String[] | 指定一组 Servlet 的 URL 匹配模式。等价于 <url-pattern> 标签 |
initParams | WebInitParam[] | 指定一组 Servlet 初始化参数,等价于 <init-param> 标签 |
loadOnStartup | int | 指定 Servlet 的加载顺序,等价于 <load-on-startup> 标签 |
description | String | 该 Servlet 的描述信息,等价于 <description> 标签 |
displayName | String | 该 Servlet 的显示名,通常配合工具使用,等价于 <display-name> 标签 |
asyncSupported | boolean | 声明 Servlet 是否支持异步操作模式,等价于 <async-supported> 标签 |
largeIcon | String | 大图标 |
smallIcon | String | 小图标 |
2、Filter
继承javax.servlet.Filter接口就能自定义一个过滤器
web.xml配置,执行顺序是按照Filter在web.xml中的先后顺序执行
<filter>
<filter-name>UserFilter</filter-name>
<filter-class>cn.ycx.web.filter.UserFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UserFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>LogFilter</filter-name>
<filter-class>cn.ycx.web.filter.LogFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LogFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
servlet3.0之后新增了 @WebFilter 注解
@WebFilter 的属性
属性 | 类型 | 描述 |
filterName | String | 指定过滤器的 name 属性,等价于 <filter-name> |
value | String[] | 该属性等价于 urlPatterns 属性。但是两者不应该同时使用。 |
urlPatterns | String[] | 指定一组过滤器的 URL 匹配模式。等价于 <url-pattern> 标签。 |
servletNames | String[] | 指定过滤器将应用于哪些 Servlet。取值是 @WebServlet 中的 name 属性的取值,或者是 web.xml 中 <servlet-name> 的取值。 |
dispatcherTypes | DispatcherType | 指定过滤器的转发模式。具体取值包括: ASYNC、ERROR、FORWARD、INCLUDE、REQUEST。 |
initParams | WebInitParam[] | 指定一组过滤器初始化参数,等价于 <init-param> 标签。 |
asyncSupported | boolean | 声明过滤器是否支持异步操作模式,等价于 <async-supported> 标签。 |
description | String | 该过滤器的描述信息,等价于 <description> 标签。 |
displayName | String | 该过滤器的显示名,通常配合工具使用,等价于 <display-name> 标签。 |
largeIcon | String | 大图标 |
smallIcon | String | 小图标 |
使用@WebFilter注解定义的Filter的执行顺序目前发现和类名(也就是文件名)的ASCII码的排序一直。
我们自定义一个跨域访问允许的Filter
package cn.ycx.web.filter; import java.io.IOException; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 跨域访问
* @author 杨崇兴 2018-07-05
*/
@WebFilter(filterName="/bcrossDomainFilter", urlPatterns="/*")
public class CrossDomainFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
String path = httpServletRequest.getServletPath();
System.out.println("CrossDomainFilter->" + path);
// 跨区请求 crossDomainFilter
httpServletResponse.addHeader("Access-Control-Allow-Origin", "*");
httpServletResponse.addHeader("Access-Control-Allow-Methods", "POST");
httpServletResponse.addHeader("Access-Control-Allow-Methods", "GET");
httpServletResponse.addHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
chain.doFilter(request, response);
}
}
3、Listener
servlet context events
javax.servlet.ServletContextListener
javax.servlet.ServletContextAttributeListener
http session events
javax.servlet.http.HttpSessionListener
javax.servlet.http.HttpSessionAttributeListener
javax.servlet.http.HttpSessionActivationListener
javax.servlet.http.HttpSessionBindingListener
javax.servlet.http.HttpSessionIdListener
servlet request events
javax.servlet.ServletRequestListener
javax.servlet.ServletRequestAttributeListener
javax.servlet.AsyncListener
使用注解@WebListener
package cn.ycx.listener; import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class MyListener implements ServletRequestListener {
public void requestDestroyed(ServletRequestEvent sre) {
}
public void requestInitialized(ServletRequestEvent sre) {
System.out.println(">>>>> mylistener");
}
}
去掉@WebListener注解,在web.xml中配置
<listener>
<listener-class>cn.ycx.listener.MyListener</listener-class>
</listener>