request对象和response对象是通过Servlet容器(如Tomcat)自动创建并传递给Servlet的。
Servlet容器负责接收客户端的请求,并将请求信息封装到request对象中,然后将request对象传
递给相应的Servlet进行处理。类似地,Servlet容器会创建一个response对象,并将其传递给
Servlet,用于生成响应并发送给客户端。
总的来说,request对象是来获取请求消息,response对象是来设置响应消息
在Servlet的service()方法被调用时,Servlet容器会创建一个包含请求信息的HttpServletRequest对象,并将其作为参数传递给Servlet的service()方法。类似地,Servlet容器还会创建一个HttpServletResponse对象,并将其作为参数传递给Servlet的service()方法。
请求行的数据有:请求方式 请求url 请求协议/协议版本号
①获取请求方式:getMethod()
②获取虚拟目录:getContextPath()
③获取servlet路径:getServletPath()
④获取get方式请求参数:getQueryString()
⑤获取请求URI:统一资源定位符:getRequestURI()
统一资源标识符:getRequestURL()
⑥获取协议及版本: getProtocol()
⑦获取客户机的IP地址:getRemoteAddr()
代码示例:
@WebServlet("/requestDemo1")
public class RequestDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1获取请求方式
String method=request.getMethod();
System.out.println(method);
//2获取虚拟目录
String contextPath = request.getContextPath();
System.out.println(contextPath);
//3获取servlet路径
String servletPath = request.getServletPath();
System.out.println(servletPath);
//4获取请求参数
String queryString = request.getQueryString();
System.out.println(queryString);
//5获取请求URI
String requestURI = request.getRequestURI();
System.out.println(requestURI);
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURL);
//6获取请求协议及版本号
String protocol = request.getProtocol();
System.out.println(protocol);
//7获取客户机的ip地址
String remoteAddr = request.getRemoteAddr();
System.out.println(remoteAddr);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
方法:getHeader(String name):通过请求头的名称获取请求头的值
getHeaderNames():获取所有的请求头名称
@WebServlet("/requestDemo2")
public class RequestDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//演示获取请求头数据
//获取所以请求头数据
Enumeration headerNames = request.getHeaderNames();
//遍历
while(headerNames.hasMoreElements()){
String s = headerNames.nextElement();
//根据名称获取请求头的值
String header = request.getHeader(s);
System.out.println(s+"......"+header);
}
//演示获取请求头数据:user-Agent
String header = request.getHeader("user-agent");
//判断agent的浏览器版本
System.out.println(header);
}
只有post请求有请求体
步骤:①获取流对象
BufferedReader getReader():获取字符输入流,只能操作字符数据
ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据
② 再从流对象中拿数据
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求消息体--请求参数
//1、获取字符流
BufferedReader reader = request.getReader();
//2、读取数据
String line=null;
while((line=reader.readLine())!=null){
System.out.println(line);
}
System.out.println("-------------------------------------");
}
①根据参数名称获取参数值:getParameter(String name)
②根据参数名称获取参数值数组:getParameterValues(String name)
③获取所有请求的参数名称:getParameterNames()
④获取所有参数的map集合:getParameterMap()
@WebServlet("/requestDemo4")
public class RequestDemo4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决中文乱码问题
request.setCharacterEncoding("utf-8");
//根据请求参数的名称获取请求参数值
String username = request.getParameter("username");
System.out.println(username);
System.out.println("post");
System.out.println();
//根据参数名称获取请求参数数组
String[] parameterValues = request.getParameterValues("hobby");
for (String hobby:parameterValues) {
System.out.println(hobby);
}
System.out.println();
//根据所以请求参数名称
Enumeration parameterNames = request.getParameterNames();
while(parameterNames.hasMoreElements()){
String s = parameterNames.nextElement();
System.out.print(s);
String parameter = request.getParameter(s);
System.out.println(parameter);
}
System.out.println();
//获取所以参数的map集合
Map parameterMap = request.getParameterMap();
Set strings = parameterMap.keySet();
for(String name:strings){
String[] strings1 = parameterMap.get(name);
System.out.print(name);
for(String value:strings1){
System.out.println(value);
}
}
}
}
在服务器内部的资源跳转方式
步骤:
通过request对象获取请求转发器对象:getRequestDispatcher(String path)。
使用RequestDispatcher对象来进行转发:forward(ServletRequest request, ServletResponse response) 。
特点:
浏览器地址栏不发生改变
只能转发到当前服务器内部资源中
转发是一次请求
域对象:一个有作用范围的对象,可以在范围内共享数据
request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据
方法:
①setAttribute(String name,Object obj):存储数据
②getAttitude(String name):通过键获取值
③ void removeAttribute(String name):通过键移除键值对
注意:这个放于forward方法的上面,不然响应太快会导致无法获取信息!
代码示例:
@WebServlet("/requestDemo5")
public class RequestDemo5 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo5被访问了");
request.setAttribute("ttt","hhhh");
//转发到demo6
request.getRequestDispatcher("/requestDemo6").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
@WebServlet("/requestDemo6")
public class RequestDemo6 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object ttt = request.getAttribute("ttt");
System.out.println(ttt);
System.out.println("demo6被访问了");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
response:设置响应消息
(1)设置响应行状态码方法:setStatus();
(2)设置响应头:setHeader(String name,String value)
(3)设置响应体:
使用步骤:
①获取输出流
字符输出流:PrintWriter getWriter()
字节输出流:ServletOutputStream getOutputStream()
② 使用输出流,将数据输出到客户端浏览器
@WebServlet( "/responseDemo1")
public class responseDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo1");
//设置状态码为302
response.setStatus(302);
/* //设置响应头location
response.setHeader("location","/servlet/responseDemo2");*/
//获取当前应用程序的上下文路径
String contextPath = request.getContextPath();
//简单的重定向方法
response.sendRedirect(contextPath+"/responseDemo2");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
@WebServlet("/responseDemo2")
public class responseDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo2");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
步骤:
获取字符输出流
输出数据
@WebServlet("/responseDemo3")
public class responseDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*//获取流对象之前,设置流的默认编码:ISO-8859-1设置为:浏览器默认编码
response.setCharacterEncoding("utf-8");
//告诉浏览器服务器发送的消息体数据的编码,建议浏览器使用改编码解码
response.setHeader("content-type","text/html;charset=utf-8");*/
response.setContentType("text/html;charset=utf-8");//解决乱码问题
//获取字符输出流
PrintWriter writer = response.getWriter();
//输出数据
writer.write("牛牛牛");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
@WebServlet("/responseDemo4")
public class responseDemo4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取字节输出流
ServletOutputStream outputStream = response.getOutputStream();
//输出数据
outputStream.write("你好".getBytes());
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
ServletContext 实例代表了整个 Web 应用程序的上下文环境,可以在整个应用程序中访问和共享,可以和服务器来通信。
方式一:
通过request对象获取:request.getServletContext();
方式二:
通过HttpServlet获取:this.getServletContext();
MIME类型:在互联网通信过程中定义的一种文件数据类型
格式: 大类型/小类型 text/html image/jpeg
获取方法:getMimeType(String file)
代码示例:
@WebServlet("/ContextDemo1")
public class ContextDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取Context对象的方式
// ServletContext servletContext = request.getServletContext();
ServletContext servletContext1 = this.getServletContext();
//定义文件名称
String filename="a.jpg";
//获取MIME类型
String mimeType = servletContext1.getMimeType(filename);
System.out.println(mimeType );
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
① setAttribute(String name,Object value)
② getAttribute(String name)
③removeAttribute(String name)
ServletContext对象范围:所有用户所有请求的数据
@WebServlet(name = "ContextDemo2", value = "/ContextDemo2")
public class ContextDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext1 = this.getServletContext();
servletContext1.setAttribute("n","hello");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
@WebServlet(name = "ContextDemo2", value = "/ContextDemo2")
public class ContextDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext1 = this.getServletContext();
Object n = servletContext1.getAttribute("n");
System.out.println(n);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
方法:String getRealPath(String path)