request 对象和 response 对象
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象
HttpServletResponse
HttpServletResponse对象代表服务器的响应。这个对象中封装了向客户端发送数据、发送响应头,发送响应状态码的方法。
1.向客户端输出中文数据(字节)
package cn.lsl.response;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResponseDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test5(response);
}
//输出整数:最好转换成字符串输出
private void test5(HttpServletResponse response) throws IOException{
int i = 98;
response.getOutputStream().write((i+"").getBytes());
}
//以字节流用默认编码向客户端输出中文数据:没有乱码
//默认的是GBK
private void test1(HttpServletResponse response) throws IOException{
String s = "你好!一";
ServletOutputStream out = response.getOutputStream();
out.write(s.getBytes());
}
//以字节流用utf-8编码向客户端输出中文数据
//以UTF-8编码发送数据,浏览器(默认用GB2312)会出现乱码
private void test2(HttpServletResponse response) throws IOException{
String s = "你好!二";
//通知客户端查看UTF-8码表
response.setHeader("Content-Type", "text/html;charset=UTF-8");
ServletOutputStream out = response.getOutputStream();
out.write(s.getBytes("UTF-8"));
}
//以字节流用utf-8编码向客户端输出中文数据
private void test3(HttpServletResponse response) throws IOException{
String s = "你好!三";
ServletOutputStream out = response.getOutputStream();
out.write("<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>".getBytes("UTF-8"));
out.write(s.getBytes("UTF-8"));
}
//以字节流用utf-8编码向客户端输出中文数据
private void test4(HttpServletResponse response) throws IOException{
String s = "你好!四";
response.setContentType("text/html;charset=UTF-8");
ServletOutputStream out = response.getOutputStream();
out.write(s.getBytes("UTF-8"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
分析:出现乱码的图解
解决办法:
1)通过更改浏览器的编码方式:IE/”查看”/”编码”/”UTF-8”(不可取)
2)通过设置响应头告知客户端编码方式:response.setHeader(“Content-type”, “text/html;charset=UTF-8”);//告知浏览器数据类型及编码
3)通过meta标签模拟请求头:out.write("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />".getBytes());
4)通过以下方法:response.setContentType("text/html;charset=UTF-8");
2.向客户端输出中文数据(字符)
package cn.lsl.response;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResponseDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test3(response);
}
//字符流输出中文数据
private void test1(HttpServletResponse response) throws IOException{
String s = "你好!一";
response.setCharacterEncoding("UTF-8"); //设置查的UTF-8(对内容进行编码,查utf-8)
response.setHeader("Content-Type", "text/html;charset=UTF-8");//通知浏览器以utf-8打开
PrintWriter out = response.getWriter();
out.write(s);//默认查的是ISO-8859-1码表
}
//字符流输出中文数据
private void test2(HttpServletResponse response) throws IOException{
String s = "你好!二";
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write(s);
}
//字符流输出中文数据
private void test3(HttpServletResponse response) throws IOException{
String s = "你好!三";
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write(s);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
分析:
乱码原因:以默认编码发送数据 ISO-8859-1(没有中国二字编码),此时会发生乱码
解决办法:
1)
setCharacterEncoding(“UTF-8”);//更改编码为UTF-8
response.setHead(“Context-type”,”text/html;charset=UTF-8”);//告诉客户端编码方式
2)response.setContentType("text/html;charset=utf-8");
在字符流输出中文数据时:
response.setContentType("text/html;charset=utf-8");有两个作用:
通知字符流以UTF-8编码输出
通知客户端以UTF-8解码显示
3.输出随机验证码
package cn.lsl.response;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResponseDemo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通知浏览器不要缓存
response.setHeader("Expires", "-1");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
int width = 120;
int height = 25;
//创建一副内存图像BufferedImage
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//得到属于该图片的画笔:Graphics();
Graphics g = image.getGraphics();
//画边框
g.setColor(Color.BLUE);
g.drawRect(0, 0, width, height);
//填充背景色
g.setColor(Color.YELLOW);
g.fillRect(1, 1, width-2, height-2);
//画干扰线
g.setColor(Color.GRAY);
Random r = new Random();
for(int i=0; i<10; i++)
g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
//随机数字
g.setColor(Color.RED);
g.setFont(new Font("宋体",Font.BOLD|Font.ITALIC,20));
int x = 23;
for(int i=0; i<4; i++){
g.drawString(r.nextInt(10)+"", x, 20);
x = x+20;
}
//输出到浏览器的页面上:ImageIO
ImageIO.write(image, "jpg", response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
4.定时刷新
package cn.lsl.response;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResponseDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test2(response);
}
//定时刷新到自己
private void test1(HttpServletResponse response) throws IOException{
Random r = new Random();
int i = r.nextInt();
response.setIntHeader("Refresh", 1);
response.getOutputStream().write((i+"").getBytes());
}
//刷到别的页面
private void test2(HttpServletResponse response) throws IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
response.setHeader("Refresh", "2;URL=/Response/index.jsp");
out.write("登陆成功!2秒后将跳转到主页!若没有跳转,请猛点<a href='/Response/index.jsp'>这里</a>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
5.控制缓存时间
package cn.lsl.response;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResponseDemo5 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//缓存一小时
response.setDateHeader("Expires", System.currentTimeMillis()+60*60*1000);
response.getWriter().write("hello");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
6.请求重定向
请求重定向:一个web资源收到客户端请求后,通知客户端去访问另外一个web资源,这称之 为请求重定向。
地址栏会连,并发送2次请求,增加服务器负担。
实现方式:response.sendRedirect();
原理:302/307状态码和location头即可实现重定向
package cn.lsl.response;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResponseDemo6 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.setStatus(302);
// response.setHeader("Location", "/Response/index.jsp");
response.sendRedirect("/Response/index.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
7.getOutputStream和getWriter方法分别用于得到输出二进制数据、输出文本数据的ServletOuputStream、Printwriter对象。
getOutputStream和getWriter这两个方法互相排斥,调用了其中的任何一个方法后,就不能再调用另一方法。会抛异常。
异常为:getOutputStream() has already been called for this response
package cn.lsl.response;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//字节流和字符流同时使用,报错
//getOutputStream() has already been called for this response
public class ResponseDemo7 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String s1 = "a";
response.getOutputStream().write(s1.getBytes());
String s2 = "b";
response.getWriter().write(s2);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
注:通过字符流或字节流输出的数据并不是直接打给浏览器的。而是把数据写到response对象的缓存中的。服务器从缓存中取出数据,按照HTTP协议的响应格式输出给浏览器。如果你调用的response的输出流没有主动关闭,服务器会替你关的。
HttpServletRequest
HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象的方法,可以获得客户这些信息。
1.获取客户机信息的方法
getRequestURL方法返回客户端发出请求时的完整URL。
getRequestURI方法返回请求行中的资源名部分。
getQueryString 方法返回请求行中的参数部分。
getRemoteAddr方法返回发出请求的客户机的IP地址
getRemoteHost方法返回发出请求的客户机的完整主机名
getRemotePort方法返回客户机所使用的网络端口号
getLocalAddr方法返回WEB服务器的IP地址。
getLocalName方法返回WEB服务器的主机名
getMethod得到客户机请求方式
eg:
package cn.lsl.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//获取客户机信息
public class RequestDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取URL和URI
//url:http://localhost:8080/Request/servlet/RequestDemo1
//uri:/Request/servlet/RequestDemo1
System.out.println("url:"+request.getRequestURL());
System.out.println("uri:"+request.getRequestURI());
//通过getQueryString可以获得get方式提交查询串url中?后面部分
System.out.println("queryString:"+request.getQueryString());
//获得客户端ip
System.out.println("ip:"+request.getRemoteAddr());
//通过getContextPath获得工程虚拟目录名称
System.out.println("工程虚拟目录名称:"+request.getContextPath());
//通过getMethod获得请求方式
System.out.println("请求方式:"+request.getMethod());
//获得当前访问资源路径 ----/servlet/RequestDemo1
System.out.println("当前访问资源路径:"+request.getRequestURI().substring(request.getContextPath().length()));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
注:
1)URL和URI的区别
URL: http://localhost:8080/Request/servlet/RequestDemo1
URI: /Request/servlet/RequestDemo1
URI 包含 URL的 ,URL 一定完成路径,URI可以相对路径
http://localhost:8080/Request/servlet/RequestDemo1
是一个URL 同时也是 URI
/Request/servlet/RequestDemo1 ---- 都是URI 不是URL
2)获得当前访问资源路径 :
request.getRequestURI().substring(request.getContextPath().length());
2.获取请求头信息
getHeader 获得头信息的值,转换一个字符串
getHeaders 获得头信息值 ,获得Enumeration
getHeaderNames 获得所有头信息名称 返回 Enumeration
eg:
package cn.lsl.request;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RequestDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取指定头信息字段
String value = request.getHeader("Accept-Encoding");
System.out.println(value);
System.out.println("---------------");
//获得所有头信息内容
Enumeration<String> enumeration = request.getHeaderNames();
while(enumeration.hasMoreElements()){
String name = enumeration.nextElement();
System.out.println(name+":"+request.getHeader(name));
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
3.防盗链程序
referer.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>referer.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>通过链接访问RequestDemo3 不是盗链</h1>
<a href="/Request/servlet/RequestDemo3">link</a>
</body>
</html>
package cn.lsl.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RequestDemo3 extends HttpServlet {
//防盗链
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通过getHeader获得referer头信息
String referer = request.getHeader("referer");
//判断头信息可以知道是不是盗链
response.setContentType("text/html;charset=utf-8");
if(referer == null || !referer.startsWith("http://localhost")){
//盗链
response.getWriter().println("是盗链");
}else{
//不是盗链
response.getWriter().println("真不是盗链");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
4.获取客户机请求参数
常用API四个
getParameter
getParameterValues
getParameterNames
getParameterMap
eg:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>request.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>分别以get和post方式提交数据</h1>
<h1>第一种:通过超链接提交数据以?方式</h1>
<a href="/Request/servlet/RequestDemo4?name=zhangsan&city=shenzhen">超链接提交数据</a>
<h1>第二种:通过form的post方式提交数据</h1>
<form action="/Request/servlet/RequestDemo4" method="post">
姓名<input type="text" name="name" /><br/>
城市<input type="text" name="city" /><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
package cn.lsl.request;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RequestDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
System.out.println(name);
String city = request.getParameter("city");
System.out.println(city);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
5.获取复杂表单的数据及解决中文乱码问题
regist.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>regist.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="/Request/servlet/RequestDemo5" method="post">
<table>
<tr>
<td>
用户名
</td>
<td>
<input type="text" name="username" />
</td>
</tr>
<tr>
<td>
密码
</td>
<td>
<input type="password" name="password" />
</td>
</tr>
<tr>
<td>
性别
</td>
<td>
<input type="radio" name="gender" value="male" />
男
<input type="radio" name="gender" value="female" />
女
</td>
</tr>
<tr>
<td>
爱好
</td>
<td>
<input type="checkbox" name="hobby" value="sport" />
运动
<input type="checkbox" name="hobby" value="music" />
音乐
<input type="checkbox" name="hobby" value="game" />
游戏
</td>
</tr>
<tr>
<td>
城市
</td>
<td>
<select name="city">
<option value="beijing">
北京
</option>
<option value="shanghai">
上海
</option>
<option value="shenzhen">
深圳
</option>
</select>
</td>
</