一、Request原理

1、原理

第一步:用户访问url,浏览器向服务器发出请求,请求携带着请求消息数据;

第二步:Tomcat服务器会根据请求url中的资源路径,创建对应的Servlet对象;

第三步:Tomcat服务器创建request对象和seponse对象,request对象封装着请求消息数据;

第四步:Tomcat服务器将request对象和seponse对象传递给service方法,并调用service方法;

第五步:我们(程序员)可以在service方法中通过request对象获取请求消息数据,通过response对象设置响应消息数据;

第六步:Tomcat服务器从response对象中获取响应消息数据,返回给浏览器;

备注:

1、request对象和response对象是由服务器创建的,我们(程序员)来使用它;

2、request对象用户获取请求消息数据,response对象用于设置响应消息数据;

二、获取请求数据

1、获取请求行数据

    GET:/web/index?name=zhangsan HTTP/1.1
方法:

package cn.itcast.web.request; 
 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.IOException; 
 
@WebServlet("/RequestDemo1") 
public class request extends HttpServlet { 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
    } 
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        //1.获取请求方式:GET 
        String method = request.getMethod(); 
        System.out.println(method); 
        //2.(*)获取虚拟目录:/Tomcat_servlet 
        String contextPath = request.getContextPath(); 
        System.out.println(contextPath); 
        //3.获取Servlet路径:/RequestDemo1 
        String servletPath = request.getServletPath(); 
        System.out.println(servletPath); 
        //4.获取get方式请求参数:name = zhangsan 
        String queryString = request.getQueryString(); 
        System.out.println(queryString); 
        //5.(*)获取请求URI:/Tomcat_servlet/RequestDemo1 
        String requestURI = request.getRequestURI(); 
        StringBuffer requestURL = request.getRequestURL(); 
        System.out.println(requestURI); 
        System.out.println(requestURL); 
        //6.获取协议及版本:HTTP/1.1 
        String protocol = request.getProtocol(); 
        System.out.println(protocol); 
        //7.获取客户机的IP地址: 
        String remoteAddr = request.getRemoteAddr(); 
        System.out.println(remoteAddr); 
    } 
} 

2、获取请求头数据

方法:

    getHeaderNames():获取所有的请求头名称

package cn.itcast.web.request; 
 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.IOException; 
import java.util.Enumeration; 
 
@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 { 
        //演示获取请求头数据 
 
        //1.获取所有请求头名称 
        Enumeration<String> headerNames = request.getHeaderNames(); 
        //2.遍历 
        while (headerNames.hasMoreElements()){ 
            String name = headerNames.nextElement(); 
            //genju名称获取请求头的值 
            String value = request.getHeader(name); 
            System.out.println(name + "---" + value); 
        } 
    } 
} 

    string getHerder(String name):通过请求头的名称获取请求头的值

package cn.itcast.web.request; 
 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.IOException; 
import java.util.Enumeration; 
 
@WebServlet("/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 { 
        //演示获取请求头数据:user-agent 
 
        String agent = request.getHeader("user-agent"); 
        //判断agent的浏览器版本 
        if(agent.contains("Chrome")){ 
            //谷歌 
            System.out.println("谷歌浏览器"); 
        }else if(agent.contains("Firefox")){ 
            //火狐 
            System.out.println("火狐浏览器"); 
        } 
 
 
    } 
} 

3.获取请求提数据

    请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数

    步骤:

        1.获取流对象

            BufferedReader getReader():获取字符输入流,只能操作字符数据

            ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据

        2.再从流中拿数据

在wed下定义一个注册页面:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>注册页面</title> 
</head> 
<body> 
    <form action="/Tomcat_servlet/RequestDemo3" method="post"> 
        <input type="text" placeholder="请输入用户名" name="username"><br> 
        <input type="text" placeholder="请输入密码" name="password"><br> 
        <input type="submit" value="提交"> 
    </form> 
</body> 
</html>

在src下定义一个类:

package cn.itcast.web.request; 
 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.BufferedReader; 
import java.io.IOException; 
 
@WebServlet("/RequestDemo3") 
public class RequestDemo3 extends HttpServlet { 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        //获取请求消息体 -- 请求参数 
 
        //1.获取字符流 
        BufferedReader br = request.getReader(); 
        //2.获取读取数据 
        String line = null; 
        while ((line = br.readLine()) != null){ 
            System.out.println(line); 
        } 
    } 
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
    } 
} 

发布评论
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Java>Tomcat:wed服务器软件下载及项目配置的三种方法讲解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。