本文是servlet的入门篇,主要简单介绍下http协议html
__ 1.http协议:__java
2.查看http协议的工具:git
GET / HTTP/1.1 Host: www.baidu.com
GET /HttpSer HTTP/1.1 Host: localhost:8080 Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.85 Chrome/45.0.2454.85 Safari/537.36 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8
HTTP/1.1 302 Found Server: Apache-Coyote/1.1 Location: http://localhost:8080/HttpSer/ Transfer-Encoding: chunked Date: Fri, 09 Oct 2015 08:55:42 GMT
下面将对这两个协议进行介绍:github
GET /HttpSer HTTP/1.1-请求行 Host: localhost:8080--请求头;有多个key-value组成 Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.85 Chrome/45.0.2454.85 Safari/537.36 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8 --可选,实体内容;
GET /HttpSer HTTP/1.1 -请求行
1.请求资源的URL和URI比较:
URL: 统一资源定位符。http://localhost:8080/HttpSer/index.html。只能定位互联网资源。是URI 的子集.
URI: 统一资源标记符。/HttpSer/index.html。用于标记任何资源。能够是本地文件系统,局域网的资源(//192.168.14.10/HttpSer/index.html),能够是互联网。
2.请求方式:
常见的请求方式: GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT 、DELETE
经常使用的请求方式: GET(有将实体信息放在浏览器地址栏) 和 POST(隐藏实体内容)
3. servlet得到请求行信息:_web
/*** 1.1请求行的得到*/ System.out.println("请求方式:"+request.getMethod());//得到提交方式 System.out.println("请求URI:"+request.getRequestURI());//得到uri System.out.println("请求url:"+request.getRequestURL());//得到url System.out.println("得到协议:"+request.getProtocol());//得到所用协议 ##输出: 请求方式:GET 请求URI:/HttpSer/TestRequst 请求url:http://localhost:8080/HttpSer/TestRequst 得到协议:HTTP/1.1
4. 测试提交方式:
新创建web工程,创建TestMethod.html文件:
创建Servlet类TestMethod.java修改get和post方法:chrome
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("get 方式提交"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("post 方式提交"); }
运行tomcat能够看淡提交方式的不一样:浏览器地址栏显示的不一样,servlet调用的方法也不一样;
get提交
post提交
通过对比请求能够发现
get方式在请求行多了?name=wang&pass=123
post多了实体内容:Content-Type: application/x-www-form-urlencoded
请求内容同以下:数组
#get方式: GET /HttpSer/TestMethod?name=wang&pass=123 HTTP/1.1 Host: localhost:8080 Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.85 Chrome/45.0.2454.85 Safari/537.36 Referer: http://localhost:8080/HttpSer/testMethod.html Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8 #post方式: POST /HttpSer/TestMethod HTTP/1.1 Host: localhost:8080 Connection: keep-alive Content-Length: 18 Cache-Control: max-age=0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Origin: http://localhost:8080 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.85 Chrome/45.0.2454.85 Safari/537.36 Content-Type: application/x-www-form-urlencoded Referer: http://localhost:8080/HttpSer/testMethod.html Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.8
1. 请求头介绍:
请求头主要包含一些有用信息:
1.Host: localhost:8080主机地址和端口
2.Connection: keep-alive 链接方式
3.User-Agent:浏览器的一些信息
4.Referer:来访页面
5.Content:实体内容;post才有
2. servlet得到请求头主要的方法:
request.getHeader("Host"));经过建得到相应请求的内容;
Enumeration
3. 演示以下:
修改doget方法
/*** 设置参数查询的编码 *该方法只能对请求实体内容的数据编码起做用。POST提交的数据在实体内容中,因此该方法对POST方法有效! *GET方法的参数放在URI后面,因此对GET方式无效!!! */ request.setCharacterEncoding("utf-8"); /** * 1.1请求行的得到 */ System.out.println("请求方式:"+request.getMethod()); System.out.println("请求URI:"+request.getRequestURI()); System.out.println("请求url:"+request.getRequestURL()); System.out.println("得到协议:"+request.getProtocol()); /** * 1.2请求头的得到 */ //经过键得到请求头的内容 System.out.println("得到host:"+request.getHeader("Host")); System.out.println("得到浏览器的User-Agent:"+request.getHeader("User-Agent")); //经过迭代器迭代,得到键 在取键值 Enumeration<String> headerNames = request.getHeaderNames(); while(headerNames.hasMoreElements()){ String key=headerNames.nextElement(); System.out.println(key+":"+request.getHeader(key)); } /** * 获得请求实体内容 * 好比:实体为name=peace&pass=1234 */ ServletInputStream in = request.getInputStream(); byte[] buf=new byte[1024]; int len=0; while((len=in.read(buf))!=-1){ String str=new String(buf,0,len); System.out.println(str); } #输出以下: 请求方式:GET 请求URI:/HttpSer/TestRequst 请求url:http://localhost:8080/HttpSer/TestRequst 得到协议:HTTP/1.1 得到host:localhost:8080 得到浏览器的User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.101 Chrome/45.0.2454.101 Safari/537.36 host:localhost:8080 connection:keep-alive accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 upgrade-insecure-requests:1 user-agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.101 Chrome/45.0.2454.101 Safari/537.36 referer:http://localhost:8080/HttpSer/testMethod.html accept-encoding:gzip, deflate, sdch accept-language:en-US,en;q=0.8 cookie:CNZZDATA1255712369=1133597550-1443969628-%7C1443969628//此去后面文章会介绍;
1. 输入参数:
输入参数:
对于get来讲就是跟在url后面的内容
/TestParam?name="peace"&password="sisi" ;name="peace"&password="sisi"这就是输入参数
对于post来讲就是实体内容,不可见
2. Servlet中得到输入参数的方法:
String name=request.getParameter("name");得到对应输入参数名字的内容
Enumeration
String[] hobits = request.getParameterValues(names);若是对应名字的内容是一个数组,使用这个方法得到,好比复选框
3. 演示以下:
1.创建testParam.html
2.修改doget方法:服务器
/** * 设置参数查询的编码 * 该方法只能对请求实体内容的数据编码起做用。POST提交的数据在实体内容中,因此该方法对POST方法有效! * GET方法的参数放在URI后面,因此对GET方式无效!!! */ request.setCharacterEncoding("utf-8"); //得到全部的方式 System.out.println("提交方式:"+request.getMethod()); //得到输入参数 String name=request.getParameter("name"); String pass=request.getParameter("password"); System.out.println("name:"+name+",pass:"+pass); /*此去为若是get方式提交出现乱码,使用; * if("GET".equals(request.getMethod())){ password = new String(password.getBytes("iso-8859-1"),"utf-8"); }*/ System.out.println(">>>>>>>>>>>>>>>>>"); //得到全部输入参数的名字 Enumeration<String> params = request.getParameterNames(); while(params.hasMoreElements()) { String names=params.nextElement(); //若是是复选框,使用getParameterValues(names);方法 if("hobit".equals(names)){ System.out.println(names+":"); String[] hobits = request.getParameterValues(names); for(String s:hobits) System.out.print(s+","); System.out.println(); } else{ System.out.println(names+":"+request.getParameter(names)); } } ##输出结果以下: 提交方式:POST name:peace,pass:124 >>>>>>>>>>>>>>>>> name:peace password:124 gender:男 籍贯:湖南 hobit: 篮球,足球, info:一条小鲨鱼peace id:001
HTTP/1.1 302 Found ---响应行 Server: Apache-Coyote/1.1 ---响应头, 有多个key-value组成 Location: http://localhost:8080/HttpSer/ Transfer-Encoding: chunked Date: Fri, 09 Oct 2015 08:55:42 GMT
HTTP/1.1 302 Found
1基本介绍
1.HTTP/1.1:采用的协议
2.302:状态码
常见的状态:
200 : 表示请求处理完成并完美返回
302: 表示请求须要进一步细化。
404: 表示客户访问的资源找不到。
500: 表示服务器的资源发送错误。(服务器内部错误)
3.Found:状态描述,常见ok和found
2. servlet中的方法
tomcat服务器把请求信息封装到HttpServletRequest对象,且把响应信息封装到HttpServletResponse
response.setStatus(404);//设置状态码
response.sendError(404);// 设置错误页面
3. 演示以下
response.setStatus(404);//错误代码,没有反应 response.sendError(404);// 发送404的状态码+404的错误页面 #输出结果: HTTP/1.1 404 Not Found Server: Apache-Coyote/1.1 Content-Type: text/html;charset=ISO-8859-1 Content-Language: en Content-Length: 949 Date: Sat, 10 Oct 2015 13:09:53 GMT
1. 基本介绍
格式:Server: Apache-Coyote/1.1;Server响应头名,后面的是响应值;
头里面主要包括:Server,服务器类型;Location:跳转网页地址 Conten*:实体内容
2. servlet中的方法
response.setHeader("server", "JBoss");修改对应头名的内容;
3. 演示以下
//修改服务器类型 response.setHeader("server", "JBoss"); /** * 修改实体内容 */ //浏览器能直接看到的内容就是实体内容 response.getWriter().println("hello peace");//字符内容,经常使用 //response.getOutputStream().write("hello world".getBytes());//字节内容。不能两个同时使用 #输出以下: HTTP/1.1 200 OK server: JBoss Content-Length: 12 Date: Sat, 10 Oct 2015 13:11:04 GMTHTTP/1.1 200 OK server: JBoss Content-Length: 12 Date: Sat, 10 Oct 2015 13:11:04 GMT
/** * 测试重定向:与转发不一样 * 使用请求重定向:发送一个302状态吗+location的响应 * */ response.setStatus(302);//设置状态码 response.setHeader("location", "/HttpSer/adv.html");//设置重定向页面 //简单写法 // response.sendRedirect("/HttpSer/adv.html"); #输出: HTTP/1.1 302 Found Server: Apache-Coyote/1.1 location: /HttpSer/adv.html Content-Length: 12 Date: Sat, 10 Oct 2015 13:15:26 GMT
/** * 定时刷新 * 原理:浏览器解析refresh头,获得头以后从新请求当前资源 * */ //response.setHeader("refresh", "1");//每隔1秒刷新一次 //隔5秒后转到另外的资源 //response.setHeader("refresh", "5;url=/HttpSer/adv.html"); #输出: HTTP/1.1 200 OK Server: Apache-Coyote/1.1 refresh: 1 Content-Length: 12 Date: Sat, 10 Oct 2015 13:18:39 GMT HTTP/1.1 200 OK Server: Apache-Coyote/1.1 refresh: 5;url=/HttpSer/adv.html Content-Length: 12 Date: Sat, 10 Oct 2015 13:21:29 GMT
response.setCharacterEncoding("utf-8"); /** * 1. 服务器发送给浏览器的数据类型和内容编码 */ //response.setHeader("content-type", "text/html");//设置内容为html //response.setContentType("text/html;charset=utf-8");//和上面代码等价。推荐使用此方法 //response.setContentType("text/xml");//设置内容为xml //response.setContentType("image/png");//设置内容为图片
/** * 设置如下载方式打开文件 */ //response.setHeader("Content-Disposition", "attachment; filename="+file.getName());
File file = new File("/media/peace/本地磁盘/andriod/1.png");//WebContent /** * 发送图片 */ FileInputStream in = new FileInputStream(file); byte[] buf = new byte[1024]; int len = 0; //把图片内容写出到浏览器 while( (len=in.read(buf))!=-1 ){ response.getOutputStream().write(buf, 0, len); }
来自一条小鲨鱼(rlovep.com)
代码下载