* Request 和 Response 原理:
* request对象和response对象由服务器创建,我们只需要在service方法中使用这两个对象即可
* 继承体系结构:
ServletRequest <--继承-- HttpServletRequest <--实现--- RequestFacade
* Response:封装响应消息,设置响应消息
* 设置响应行:HTTP/1.1 200 ok
* setStatus(int code):设置响应状态码
* 演示重定向:302
//简化版的重定向
response.sendRedirect("http://192.168.1.66:8080/zxc/one");
* 实现原理
//1.设置响应状态码为 302
response.setStatus(302);
//2.设置响应头 location
response.setHeader("location", "http://192.168.1.66:8080/zxc/one");
* 设置响应头:
* setHeader(String name,String value):
* setDateHeader(String name, long date)
* setIntHeader(String name, int value)
* 设置响应体:
* getOutputStream():发送字节数据
* 验证码:
* 防止恶意注册
* getWriter():发送字符数据
* 发送中文数据时,需要在获取流之前设置编码方式,并且要告诉客户端浏览器,服务器使用的编码方式
//简化版设置编码的方式
response.setContentType("text/html;charset=utf-8");
package 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 ResponseTest extends HttpServlet { private static final long serialVersionUID = -3007244764565301466L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 设置响应消息体:
* getOutputStream():发送字节数据
* getWriter():发送字符数据
*/
/*
//设置流的编码方式
response.setCharacterEncoding("utf-8");
//告诉客户端,服务器使用的编码方式
response.setHeader("content-type", "text/html;charset=utf-8");
*/ //设置编码方式
response.setContentType("text/html;charset=utf-8");
//获取字符输出流
PrintWriter pw = response.getWriter();//默认编码是 iso-8859-1
//设置数据
pw.write("<h3>你好啊 ! response...</h3>");
//刷新
pw.flush();
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response); } }
* Request:封装请求消息,获取请求消息
* 获取请求消息:
* 获取请求行:GET /day08/req1?name=zs HTTP/1.1
* getMethod():获取请求方式
* getContextPath():获取虚拟目录
* getRequestURI():获取URI(统一资源标识符) /day07/one /one
* getRequestURL():获取URL(统一资源定位符) http://localhost:8080/day07/one
* getRemoteAddr():获取客户机的IP地址
* getQueryString():get方式获取请求参数
* 获取请求头:
* String getHeader(String name)
* long getDateHeader(String name)
* int getIntHeader(String name)
package 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 RequestTest2 extends HttpServlet { private static final long serialVersionUID = -5095708647887091449L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //获取请求头
String agent = request.getHeader("user-agent");
System.out.println(agent); if(agent.contains("MSIE")){
System.out.println("IE 来了...");
}else if(agent.contains("Firefox")){
System.out.println("火狐 来了...");
}else if(agent.contains("Chrome")){
System.out.println("谷歌 来了...");
}else{
System.out.println("其他浏览器 来了...");
} System.err.println("..............................");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response); } }
* 获取请求体:封装post请求参数数据
* BufferedReader getReader():
* ServletInputStream getInputStream():
* 通用的获取请求参数的方式:
* String getParameter(String name):根据键名获取值
* String[] getParameterValues(String name):根据键名获取值的数组,一般用于复选框
* Map<String,String[]> getParameterMap():获取所有参数的map集合
* Enumeration getParameterNames():获取所有的键名枚举
package 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 RequestTest extends HttpServlet { private static final long serialVersionUID = 4919849090529066482L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //演示获取请求行数据 System.out.println(request.getMethod());//获取请求发式
System.out.println(request.getContextPath());//获取虚拟目录
System.out.println(request.getRequestURI());//获取URI
System.out.println(request.getRequestURL());//获取URL
System.out.println(request.getRemoteAddr());//获取ip
System.out.println(request.getQueryString());//获取请求参数
System.out.println(request.getProtocol());//获取协议 System.err.println("--------------------------"); response.sendRedirect(request.getContextPath()+"/index.jsp");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response); } }
* 请求参数中文乱码问题:
1.通用:
//1.解码 iso
byte[] bytes = username.getBytes("iso-8859-1");
//2.编码 utf-8
username = new String(bytes, "utf-8");
2.post专用
//设置输入流的编码为 utf-8
request.setCharacterEncoding("utf-8");
* 域对象:代表一次请求
* 请求转发:
* 转发特点:
1.地址栏不发生变化
2.一次请求,一次响应
3.不能转发外部站点
package 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 RequestTest7 extends HttpServlet { private static final long serialVersionUID = 3744695995118989884L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { System.out.println("req7 被访问了。。。"); //存储数据
request.setAttribute("msg", "hehehe..."); //转发至req
request.getRequestDispatcher("/req").forward(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response); } }
* 重定向的特点:
1.地址栏发生变化
2.两次请求,两次响应
3.可以访问外部的站点
package 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 ResponseTest2 extends HttpServlet { /**
*
*/
private static final long serialVersionUID = -449184946348811489L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { System.out.println("resp2 被访问了。。。");
//演示重定向
//1.设置相应状态码 302
response.setStatus(302); //2.设置相应头 location
//注意: http:// 不可以少
//response.setHeader("location", "http://192.168.1.66:8080/zxc/resp"); //简化版的重定向
response.sendRedirect("http://192.168.1.66:8080/zxc/resp");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response); } }