Servlet使用

Servlet使用

1.1重定向(如果对方不支持cookie,回写sessionID进行session跟踪)
 response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+"/next"));
******************************************************************
1.2转发
 RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
 dispatcher.forward(request,response);
******************************************************************
1.3字符
  request.setCharacterEncoding("utf-8");
  response.setContentType("text/html;charset=utf-8");
******************************************************************
String servletPath = request.getServletPath();
  servletPath = servletPath.substring(servletPath.lastIndexOf("/") + 1);
  String operation = servletPath.substring(0, servletPath.indexOf(".do"));
1.设置连接超时时间(分钟)
 <session-config>
  <session-timeout>50</session-timeout>
 </session-config>
******************************************************************
4.相对路径匹配
 1>绝对匹配 /xx/yy
 2>后缀匹配 *.xx
 3>后面匹配 /xx/*
******************************************************************
5.监听器
5.1ServletRequestListener
   getServletContext()
   getServletRequest()
 requestDestroyed(ServletRequestEvent)
 requestInitialized(ServletRequestEvent)
5.2HttpSessionListener
   getSession()
 sessionCreated(HttpSessionEvent)
 sessionDestroyed(HttpSessionEvent)
5.3ServletContextListener
   getServletContext()
 contextInitialized(ServletContextEvent)
 contextDestroyed(ServletContextEvent)
 
5.4ServletRequestAttributeListener
   getName()
   getValue()
 attributeAdded(ServletRequestAttributeEvent)
 attributeRemoved(ServletRequestAttributeEvent)
 attributeReplaced(ServletRequestAttributeEvent)
5.5HttpSessionAttributeListener
   getName()
   getValue()
   getSession()
 attributeAdded(HttpSessionBindingEvent)
 attributeRemoved(HttpSessionBindingEvent)
 attributeReplaced(HttpSessionBindingEvent)
5.6ServletContextAttributeListener
   getName()
   getValue()
 attributeAdded(ServletContextAttributeEvent)
 attributeRemoved(ServletContextAttributeEvent)
 attributeReplaced(ServletContextAttributeEvent)

你可能感兴趣的:(Servlet使用)