Session
HttpSession se= request.getSession(); //获取session
se.setAttribute(“key”,”value”); //设置
se.getAttribute(“key”); //获取
session.invalidate(); //销毁
Servlet预定义变量
web.xml
=========================
<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns=”http://xmlns.jcp.org/xml/ns/javaee”
xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd”
id=”WebApp_ID” version=”3.1″>
<display-name>testSe</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>com.ishops.test</servlet-class>
<init-param>//只能在该Servlet中使用,不能在整个网站中用,不是全局变量
<param-name>DATABASE</param-name>
<param-value>root</param-value>
//获取方式:ServletConfig config=getServletConfig();
// config.getInitParameter(“DATABASE”);
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<context-param>//整个网站中都可以获取
<param-name>USERNAME</param-name>
<param-value>AMN</param-value>
//获取方式:ServletContext context = request.getServletcontext();
// context.getInitParameter(“USERNAME”);
</context-param>
<context-param>
<param-name>PASS</param-name>
<param-value>AMN1</param-value>
</context-param>
</web-app>
过滤器
注册过滤器
——————
web.xml
<filter>
<filter-name>filterTest</filter-name> //过滤器名字
<filter-class>com.ishops.filter.filterTest</filter-class> //过滤器完整路径
</filter>
<filter-mapping>
<filter-name>filterTest</filter-name>
<url-pattern>/test</url-pattern>
/*访问该路由(servlet-mapping 的 url-pattern为test的servlet)时过滤器起作用
* <url-pattern>/*</url-pattern>
*所有的页面都起作用
*/
</filter-mapping>
——————-
filter过滤器注解方式
@WebFilter(“/test”) //访问该路由时过滤器起作用
===========================================
过滤器
//@WebFilter(“/filterTest”)
public class filterTest implements Filter {
/**
* Default constructor.
*/
public filterTest() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void
destroy() {
System.out.println(“filter 销毁……”);
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void
doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println(“filter……”);
PrintWriter out = response.getWriter();
String name=request.getParameter(“name”);
out.write(name);
if(“amn”.equals(name)){
chain.doFilter(request, response);//验证通过
}
}
/**
* @see Filter#init(FilterConfig)
*/
public void
init(FilterConfig fConfig) throws ServletException {
System.out.println(“filter 创建……”);
}
}
路由
web.xml
———
<servlet>
<servlet-name>test</servlet-name>//Servlet名字
<servlet-class>com.ishops.test</servlet-class>//包(com.ishops),路径
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>//Servlet名字 ,与上面的保持一致
<url-pattern>/te</url-pattern>//设置访问路由 http://localhost:8080/testSe/te
</servlet-mapping>
=================================
Servlet
——-
@WebServlet(“/te”)