Servlet&Jsp javax.servlet.http包中的内容

本文详细介绍了HTTP协议中的请求和响应机制,包括HttpServletRequest接口提供的获取项目路径、Cookie、请求头等方法,HttpServletResponse接口用于设置Cookie和重定向等功能,以及HttpSession接口管理和操作会话状态的方法。

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

javax.servlet.http:
[color=red]Interface HttpServletRequest[/color]
this interface extends the interface ServletRequest to provide request interformation for the http HTTP servlets.

The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet's service methods (doGet, doPost, etc).


[color=blue]Method:[/color]
getContextPath(): obtain the project's name '/example'
getCookies():return an array containing all of the Cookie objects the client send with this request
getHeader():
getMethod()
getPathInfo()
getRequestSessionId()可能跟session的id不同,如果客户没有指定一个会话ID,此方法返回null。
getRequestURI() 获得项目的http 请求的uri
getRequestURL()获得url
ps: url和uri的区别 url是通过浏览器访问的地址名称,uri是项目路径没有http和网络地址相关信息
getSession() 获得当前session
getSession(boolean create) 如果create=true 则建立一个新的session,create=false则返回null或者获得当前session

Servlet 3.0中增加的 authenticate().login(),logout()这三个方法实在不知道怎么用啊。。第一次觉得网上的资料好少。


[color=red]Interface HttpServletResponse[/color]
addCookie(Cookie cookie)
addHeader(String name,String value)
setRedirect(String loaction)
encodeURL(String url) return String:重写url 在cookie被当前大多数浏览器禁用的时候,可以选择重写url这方式来报错session的跟踪。
[color=red]Interface HttpSession:
[/color]
Method:
getAttribute()
setAttribute()
getCreationTime()
getId()
getMaxInactiveInterval()
setMaxInactiveInterval()
getServletContext()
invalidate():是当前的session失效,如果调用这个方法后 在执行有关session的操作
如session.getAttribute就是报500的错
isNew()
removeAttribute()
[color=red]
Interface HttpSessionAttributeListener:[/color]
对session中的属性情况进行了监听
method:
attributeAdded(HttpSessionAttributeEvent)
attributeRemoved(HttpSessionAttributeEvent)
attributeReplaced(HttpSessionAttributeEvent)

[color=red]Interface HttpSessionListener[/color]
对session的初始化和销毁情况进行了监控
method:
sessionCreated(HttpSessionEvent)
sessionDestroyed(HttpSessionEvent)

[color=red]Interface HttpSessionBindingListener[/color]
对绑定到session属性的对象进行了监听
valueBound(HttpSessionBindingEvent)
valueUnbound(HttpSessionBindingEvent)

[color=red]HttpSessionActivationListener[/color]
是对不同VM之间session的交互的监听
method:
sessionDidActivate(HttpSessionEvent)
sessionWillPassivate(HttpSessionEvent)

常用类:
Cookie
HttpServlet
<-----------------------以前没用过的------------->
HttpServletRequestWrapper
HttpServletResponseWrapper
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值