标签:headers 案例 中国 火狐 线程 mit 另一个 oct int
服务器处理请求的流程:
一、HttpServletResponse对象
1、发送状态码相关的方法
ServletResponse:与协议无关的类型。
HttpServletResponse:与协议相关的类型
@WebServlet(name = "AServlet" ,urlPatterns = "/Aservlet")
public class AServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.sendError(404,"资源存在,但还发你404");
}
}
/*
* 演示重定向
* 用户请求BServlet,然后BServlet响应302,给出Location头
* */
@WebServlet(name = "BServlet",urlPatterns = "/BServlet")
public class BServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("BServlet");
/*
* 重定向:
* 1、设置Location
* 2、发送302状态码
* */
response.setHeader("Location","/CServlet");
response.sendError(302);
}
}
/*
* 浏览器会重定向到这来
* */
@WebServlet(name = "CServlet",urlPatterns = "/CServlet")
public class CServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("CServlet");
}
}
// BServlet响应头
HTTP/1.1 302
Location: /CServlet
Content-Length: 0
Date: Wed, 30 Aug 2017 01:57:17 GMT
//BServlet请求头
GET /BServlet HTTP/1.1
Host: localhost:8080
User-Agent: *****
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Cookie: Idea-e96526d7=db12919d-58f1-479a-b0f7-3104911c767b; Webstorm-2933ea9e=87a0f860-7465-47bb-8d57-4358bd45ea39; JSESSIONID=AD5DC7F501C87B36FF2186A0A1596564
Connection: keep-alive
Upgrade-Insecure-Requests: 1
//CServlet响应头
HTTP/1.1 200
Content-Length: 0
Date: Wed, 30 Aug 2017 01:57:17 GMT
//CServlet请求头
GET /CServlet HTTP/1.1
Host: localhost:8080
User-Agent: *****
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Cookie: Idea-e96526d7=db12919d-58f1-479a-b0f7-3104911c767b; Webstorm-2933ea9e=87a0f860-7465-47bb-8d57-4358bd45ea39; JSESSIONID=AD5DC7F501C87B36FF2186A0A1596564
Connection: keep-alive
Upgrade-Insecure-Requests: 1
/*
* 演示定时刷新
* 设置一个Refresh,表示定时刷新
* */
@WebServlet(name = "DServlet",urlPatterns = "/DServlet")
public class DServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 下面是用来发送响应体
* */
PrintWriter writer = response.getWriter();
writer.print("欢迎xxx登陆!5秒钟后会自动跳转到主页!乱码来的");
//设置名为Refresh的响应头
response.setHeader("Refresh","5;URL=/EServlet");
}
}
/*
* 禁用浏览器缓存
* */
@WebServlet(name = "FServlet",urlPatterns = "/FServlet")
public class FServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
response.setDateHeader("Expires",-1);
}
}
1、获取常用信息
2、获取请求头
案例:
1 @WebServlet(name = "AServlet",urlPatterns = "/AServlet")
2 public class AServlet extends HttpServlet {
3 protected void doGet(HttpServletRequest request, HttpServletResponse response)
4 throws ServletException, IOException {
5 String addr = request.getRemoteAddr();
6 System.out.println("IP:"+addr);
7 System.out.println("METHOD:"+request.getMethod());
8 String userAgent = request.getHeader("User-Agent");
9 // System.out.println(userAgent);
10 // Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36
11 //是否包含Chrome,如果包含,说明用户使用的是google浏览器
12 if(userAgent.toLowerCase().contains("chrome")){
13 System.out.println("您好:"+addr+",您用的是谷歌");
14 }else if (userAgent.toLowerCase().contains("firefox")){
15 System.out.println("您好:"+addr+",您用的是火狐");
16 }
17 }
@WebServlet(name = "BServlet",urlPatterns = "/BServlet")
public class BServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* 使用Referer请求头,来防盗链
* 直接在地址栏输入,则Referer头值为null
* */
String referer = request.getHeader("Referer");
System.out.println(referer);
if (referer == null || !referer.contains("localhost")){
response.sendRedirect("http://www.baidu.com");
}else {
System.out.println("hello");
}
}
}
3、获取请求URL
http://localhost:8080/day10_2/AServlet?username=xx&password=yyy
4、获取请求参数:请求参数:请求参数是由客户端发送给服务的,有可能是在请求体中(POST),也有可能是在URL之后(GET)。
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>Title</title>
6 </head>
7 <body>
8 <h1>测试请求参数</h1>
9 <a href="/AServlet?xxx=XXX&yyy=YYY">点击这里</a>
10 <hr/>
11 <form action="/AServlet" method="POST">
12 用户名:<input type="text" name="username"/><br/>
13 密码:<input type="password" name="password"/><br/>
14 爱好:<input type="checkbox" name="hobby" value="dq"/>打球
15 <input type="checkbox" name="hobby" value="ms"/>美食
16 <input type="checkbox" name="hobby" value="ks"/>看书
17 <br/>
18 <input type="submit" value="提交"/>
19 </form>
20 </body>
21 </html>
1 /*
2 * 演示request获取请求参数!
3 * */
4 @WebServlet(name = "AServlet",urlPatterns = "/AServlet")
5 public class AServlet extends HttpServlet {
6 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
7 String username = request.getParameter("username");
8 String password = request.getParameter("password");
9 String[] hobby = request.getParameterValues("hobby");
10 System.out.println(username+","+password+","+ Arrays.toString(hobby));
11 /*
12 * 测试获取所有请求参数的名称
13 * */
14 Enumeration names = request.getParameterNames();
15 while (names.hasMoreElements()) {
16 System.out.println(names.nextElement());
17 }
18 /*
19 * 获取所有请求参数,封装到Map中
20 * */
21 Map<String,String[]> map = request.getParameterMap();
22 for (String name : map.keySet()) {
23 String[] values = map.get(name); System.out.println(name+"="+Arrays.toString(values));
24 }
25 System.out.println("hello");
26 }
27 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
28 System.out.println("GET:"+request.getParameter("xxx"));
29 System.out.println("GET:"+request.getParameter("yyy"));
30 }
31 }
5、请求转发和请求包含:
RequestDispatcher rd = request.getRequestDispatchet("/MyServlet"); 使用request获取RequestDisapatcher对象,方法的参数是被转发或包含的Servlet的Servlet路径
有时一个请求需要多个Servlet协作才能完成,所以需要在一个Servlet跳到另一个Servlet!
6、request域
Servlet中三大域对象:request、session、application,都有如下方法
7、请求转发和重定向的区别
/*
* 演示请求转发和包含
* 注意在一个Tomcat中不能有name、urlPatterns名称相同的情况,否则抛异常
* */
@WebServlet(name = "OneServlet",urlPatterns = "/OneServlet")
public class OneServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("OneServlet");
response.setHeader("aaa","AAA");//设置响应头
/*
* 向request域中添加一个属性
* */
request.setAttribute("username","zhangsan")
response.getWriter().print("hello OneServlet");//设置响应体,包含时显示,转发时不显示 request.getRequestDispatcher("/TwoServlet").include(request,response);//请求包含
//等同于调用TwoServlet的service()方法
//request.getRequestDispatcher("/TwoServlet").forward(request,response);//请求转发
}
}
@WebServlet(name = "IncludeTwoServlet",urlPatterns = "/include/TwoServlet")
public class TwoServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getAttribute("username"));
System.out.println("TwoServlet...");
response.getWriter().print("hello TwoServlet");//设置响应体
}
}
三、编码
常见字符编码:ISO-8889-1(不支持中文),gbk(系统默认编码,中国的国标码),utf-8(万国码,支持全世界的编码)
1、响应编码
2、请求编码
3、URL编码
表单的类型:Content-Type: application/x-www-form-urlencoded,就是把作为转换成%后面跟随两位的16进制。
客户端和服务器之间传递中文时需要把它转换成网络适合的方式。
我们需把链接中的中文参数,使用url来编码,使用jsp,因为HTML不能给出Java代码。
4、路径
标签:headers 案例 中国 火狐 线程 mit 另一个 oct int
原文地址:http://www.cnblogs.com/gdwkong/p/7623620.html