Servlet使用

本文深入探讨了Java Web开发中的关键技术,包括重定向、转发、字符集设置、路径匹配及监听器的应用。通过具体实例,详细解析了这些技术的实现方式及其应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值