servlet笔记

今天学习servlet的时候,看到DoubleTen前辈已经做好了非常好的总结,所以我记录了下来。下面除蓝色的为我自身的总结外,其余均为DoubleTen前辈的总结

DoubleTen博客地址:http://www.cnblogs.com/doubleten/p/3355148.html

=========================================================

GET请求报文:

GET /servlet/somename?parma1=value1&parma2=value2 http/1.1
host:hostname
header2:...
...
headern:...
/r/n 

POST请求报文:

POST /servlet/somename http/1.1
host:hostname
header2:...
...
headern:...
/r/n
parma1=value1&parma2=value2 

读取表单数据

request.getParameter("paramName") (不区分get post请求),get请求参数在url中,post请求参数在请求行中
request.getParameterValues("paramName") 读取含有多个值的参数
request.getParameterNames() 读取所有参数名
request.getParameterMap()

读取原始数据:

request.getReader()
request.getInputStream()

请求行信息读取:

request.getMethod() GET POST PUT DELETE
request.getRequestURL() 包含协议名称 主机域名 URI
request.getRequestURI() 不含get请求后附带参数
request.getQueryString() get请求?后附带的参数

常用请求报文头的读取:

request.getCookies()
request.getAuthType() request.getRemoteUser() 读取拆分后的authorization报文头
request.getContentType()
request.getContentLength()
request.getDateHeader("headerName") request.getIntHeader("headerName") 读取包头自动转型

request.getHeader("headerName")
request.getHeaderNames()
request.getHeaders("headerName")

http 1.1请求报头:

accept mime类型
accept-charset 处理的字符集
accept-encoding 编码类型 一般有gzip deflate
accept-language
authorization
connection 1.1默认为持续链接 keep-alive,不使用持续性连接时为close
content-length
cookie 可以有多个
host 域名 http/1.1协议一般必须加入次报头
if-modified-since
if-unmodified-since
referer 请求来源 用于跟踪链接

user-agent 浏览器

  • IE: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) 
  • chrome:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
  • sogou:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0
  • Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0)
  • firefox:Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Firefox/24.0

http响应报文:

HTTP/1.1 200 OK
content-type: text/html
header1: ...
...
headern:...
/r/n
<!DOCTYPE ...>
<html>
<header>...<header>
<body>...</body>
</html>

http重定向:

response.sendRedirect("url")
回复302状态 
Location: 转向url在报文

http not found回复:

response.sendError(HttpServletResponse.SC_NOT_FOUND, "message")

设置响应报头:

response.setHeader(header, value);
response.setDateHeader(arg0, arg1); 自动转型date
response.setIntHeader(arg0, arg1) 自动转型int

response.setContentType("")
response.setContentLength(lengtt:int) http持续性连接时 需设置
response.addCookie(cookie)
response.sendRedirect(url:string)

常用响应报头:

allow 服务器支持的请求动作
cache-control
connection http/1.1不支持持续性链接 需要置为close, http/1.0支持持续性链接时需设置为keep-alive
content-disposition 浏览器向用户询问文件存档位置 response.setHeader("content-disposition", "attachment; filename=somefile");
content-encoding 传输过程中编码方式
content-language
content-length
conent-type mime类型
expires 浏览器缓存时间值
last-modified 配合请求时的if-modified-since,实现客户端缓存
location 重转向状态时配合的报文头,一般300-399之间状态的都需要
refresh 定义一定时间后刷新操作,response.setHeader("Refresh", "5; http://host/path");
retry-after 告诉浏览器多长时间后可以重试 配合503(service unavailable)使用
set-cookie 推荐使用addCookie方法
www-authenticate

cookie使用:

服务器返回设置cookie的header:
Set-Cookie: cookiename=cookievalue; Expires=Sun, 05-Oct-2014 14:35:32 GMT
浏览器上传cookie的header:
Cookie: cookiename1=vlalue; cookiename2=vlalue

    public static Cookie getCookie(HttpServletRequest request,
                                 String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
          for(Cookie cookie: cookies) {
            if (cookieName.equals(cookie.getName())) {
              return(cookie);
            }
          }
        }
        return(null);
    }

cookie一般采用add方式添加

Cookie c = new Cookie("accessCount", String.valueOf(count+1));
response.addCookie(c);

HttpSession使用:

使用request.getSession()获得
session.getId()对应cookie中的JSESSIONID

转载于:https://www.cnblogs.com/LawWongcheung/p/Java_servlet-Header.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值