经过前两篇的学习,我们知道了我们需要继承一个HttpServlet类,并且需要重写do Post与
do Get方法
public class ServletDemo extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
在重写过后会出现HttpServletRequest request与 HttpServletResponse response这么一串串东西;它们就是我们今天主要学习的请求对象与响应对象。
请求对象和响应对象,由服务器创建,管理,和销毁,服务器传给你,你只是拿来用
HttpServletResponse response:响应对象
HttpServletResponse response的意思是:响应对象;用来将响应的数据,封装进response 然后做出响应;
ServletRequest(接口) < --------继承 < -------HttpServletRequest(接口) < --------实现;
响应字符串数据:
获取字符打印流
在响应文本数据之前,设置响应编码
response.setCharacterEncoding(“utf-8”);
在告诉浏览器你用utf-8去解码
通过http协议来告诉浏览器,说白了就是,设置一个响应头
Content - Type: 键
text / html;charset = GB2312-- 告诉浏览器,服务器返回的文本采用什么编码 值
response.setHeader(“Content-Type”,“text/html; charset=utf-8”);
以上两行代码,可以综合成一行,来设置响应中文的编码:
response.setContentType(“text/html; charset=utf-8”);
@WebServlet(name = "ServletDemo2")
public class ServletDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("请求来了");
response.setContentType("text/html; charset=utf-8");
PrintWriter writer = response.getWriter();
writer.write("<h1 style='color:red'>Hello Welecome</h1>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
响应中文乱码的问题:
中文乱码:客户端和服务端的编码不一致
tomcat 服务器默认使用的编码是 ISO-8859-1
使用响应对象给浏览器响应字节数据:
来读取 WEB-INF 下的 xingye.jpg文件,然后使用字节流,把图片的字节数据,响应给浏览器
大体上分为三步走:
1:动态获取服务器路径;
2:获取字节输出流;
3:读写文件;
@WebServlet(name = "ServletImage")
public class ServletImage extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//动态获取服务器路径
String realPath = this.getServletContext().getRealPath("/");
System.out.println(realPath);
FileInputStream in = new FileInputStream(realPath+"/WEB-INF/xingye.jpg");
//获取字节输出流
ServletOutputStream out = response.getOutputStream();
//读写文件
int len=0;
byte[] bytes = new byte[1024];
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
out.flush();
}
in.close();
out.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
HttpServletRequest request:请求对象
HttpServletRequest request的意思是:响应对象;由服务器创建 管理 和销毁,你只是拿来用;request 用来封装请求的信息,我们通过request就可以获取这些请求信息;
获取请求对象:
通过String queryString = request.getQueryString();
获取请求信息的一些方法:
获取客户端的ip:
String ip = request.getRemoteAddr();
获取协议:
String protocol = request.getProtocol();
获取请求的路径:
URI:统一资源标识符
URL:统一资源定位符
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
动态获取项目的上下文路径:
String contextPath = request.getContextPath();
获取请求方式:
String method = request.getMethod();
@WebServlet(urlPatterns = "/demo2")
public class ServletDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//获取请求参数
String queryString = request.getQueryString();
System.out.println(queryString);
//获取客户端的ip
String ip = request.getRemoteAddr();
if(ip.equals("192.168.11.233")){
response.getWriter().write("张三 你好");
}else if(ip.equals("192.168.3.232")){
response.getWriter().write("李四 你好");
}else{
response.getWriter().write("其他人 你好");
}
//获取协议
String protocol = request.getProtocol();
System.out.println(protocol);
//获取请求的路径
//URI:统一资源标识符
//URL:统一资源定位符
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURI);
System.out.println(requestURL);
//URL:协议:主机:端口://资源
//动态获取项目的上下文路径
String contextPath = request.getContextPath();
System.out.println(contextPath);
//获取请求方式
String method = request.getMethod();
System.out.println(method);
/* 1. 获取请求行:
GET / MyServlet / index.jsp ? name = zhangsan & age = 23 HTTP / 1.1
request.getMethod();//获取请求方式
request.getContextPath();//获取项目名称
request.getRequestURI();//获取URI
request.getRequestURL();//获取URL
request.getRemoteAddr();//获取IP地址
request.getQueryString();//获取请求参数
request.getProtocol();//获取协议版本*/
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}