
/**
* - String getMethod():获取请求方式GET/POST
* - String getContextPath():获取虚拟目录
* - String getServletPath():获取Servlet路径
* - String getQueryString():获取get方式的请求参数
* - String getRequestURI():获取请求URI(不包含协议和IP地址)
* - StringBuffer getRequestURL():获取请求URI(包含协议和IP地址)
* - String getProtocol():获取协议及版本
* - String getRemoteAddr():获取客户机IP地址
*/
@WebServlet(value="/requestDemo1")
public class RequestDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("请求方式:"+request.getMethod());
System.out.println("虚拟目录:"+request.getContextPath());
System.out.println("Servlet路径:"+request.getServletPath());
System.out.println("get方式的请求参数:"+request.getQueryString());
System.out.println("请求URI:"+request.getRequestURI());
System.out.println("请求URL:"+request.getRequestURL());
System.out.println("协议及版本:"+request.getProtocol());
System.out.println("客户机IP地址:"+request.getRemoteAddr());
}
}
/*
运行结果:
请求方式:GET
虚拟目录:/JavaWebTest2
Servlet路径:/requestDemo1
get方式的请求参数:username=renboyu010214
请求URI:/JavaWebTest2/requestDemo1
请求URL:http://localhost:8080/JavaWebTest2/requestDemo1
协议及版本:HTTP/1.1
客户机IP地址:0:0:0:0:0:0:0:1
*/@WebServlet("/requestDemo2")
public class RequestDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取所有请求头的名称request.getHeaderNames()
Enumeration<String> enumeration=request.getHeaderNames();
while (enumeration.hasMoreElements()){
//利用迭代器逐个获取请求头名称
String headerName=enumeration.nextElement();
//利用请求头名称获取请求头的值request.getHeader(headerName)
String headerValue=request.getHeader(headerName);
//输出结果
System.out.println(headerName+" : "+headerValue);
}
}
}运行结果:(有特殊字符*/不便放在注释中)
host : localhost:8080
connection : keep-alive
upgrade-insecure-requests : 1
user-agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36
accept : text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
sec-fetch-site : none
sec-fetch-mode : navigate
sec-fetch-user : ?1
sec-fetch-dest : document
accept-encoding : gzip, deflate, br
accept-language : zh-CN,zh;q=0.9,en;q=0.8,bo-CN;q=0.7,bo;q=0.6
cookie : JSESSIONID=B675242192B257B0274786E223DE5A0F; JSESSIONID=9C6F2A1D8D267CD4F8849D8D1158DDFF@WebServlet("/requestDemo3")
public class RequestDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取流对象
BufferedReader br=request.getReader();
String line=null;
//从流对象中逐行获取数据
while((line=br.readLine())!=null){
System.out.println(line);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
//运行结果:
//username=renboyu010214&password=123456以下四种方式不论是POST还是GET方式都可以获取到请求参数
当获取请求参数出现乱码时,只需要在获取参数前,提前设置编码请求参数的格式即可
一种在服务器内部的资源跳转方式
@WebServlet("/requestDemo5")
public class RequestDemo5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("访问RequestDemo5...");
RequestDispatcher requestDispatcher=request.getRequestDispatcher("/requestDemo6");
requestDispatcher.forward(request,response);
//一般情况下利用链式编程化简为一句即可:request.getRequestDispatcher("/requestDemo6").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
/* 运行结果:
访问RequestDemo5...
访问RequestDemo6...
*/@WebServlet("/requestDemo6")
public class RequestDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("访问RequestDemo6...");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}一个有作用范围的对象,可以在范围内共享数据
代表一次请求的范围,一般用于请求转发的多个资源中共享数据
用于简化数据的封装过程
标准的Java类
封装数据
设置响应消息(相应行,响应头,响应体)
资源跳转的一种方式
常规重定向操作
设置状态码为302
设置location响应头,值为重定向资源路径
@WebServlet("/responseDemo1")
public class ResponseDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//访问demo1资源会自动跳转到demo2资源
//1. 设置状态码为302
response.setStatus(302);
//2. 设置location响应头,值为重定向资源路径
response.setHeader("location","/LoginTest/responseDemo2");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}上述重定向操作中不难看出,状态码设为302是固定的,响应头的名称设为location也是固定的,唯一在重定向时会发生变化的就是跳转资源的路径,所以response对象将重定向进行了封装
更简单的重定向操作
@WebServlet("/responseDemo1")
public class ResponseDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//访问demo1资源会自动跳转到demo2资源
response.sendRedirect("/LoginTest/responseDemo2");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}@WebServlet("/responseDemo3")
public class ResponseDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式(还可以通过直接设置响应头的ContentType来实现编码的设置,例如下面)
//response.setHeader("content-type","text/html;charset=utf-8");
response.setContentType("text/html;charset=utf-8");
//获取字符输出流
PrintWriter pw=response.getWriter();
//输出数据
pw.write("你好!hello world!");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}@WebServlet("/responseDemo3")
public class ResponseDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式(还可以通过直接设置响应头的ContentType来实现编码的设置,例如下面)
//response.setHeader("content-type","text/html;charset=utf-8");
response.setContentType("text/html;charset=utf-8");
//获取字节输出流
ServletOutputStream sos=response.getOutputStream();
//输出数据
sos.write("加油!good job!".getBytes(StandardCharsets.UTF_8));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}通过相对路径不可以确定唯一资源,相对路径的写法:./index.html
通过绝对路径可以确定唯一资源,但在书写时一般不写整个绝对路径的全部内容而是简化写法,例如:http://localhost/JavaWebTest/responseDemo1 可以简化为/JavaWebTest/responseDemo1