HttpRequestServlet的方法介绍

package com.dl.code;

import javax.jws.soap.SOAPBinding;
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.Map;

/**
 * @公司 DL19121630工作室
 * @作者 代先生
 * @日期 2021/1/14 -- 21:07
 * @微信 D19121630L
 * @温馨提示:原创代码,翻版必究!如需代写,微信联系!
 */
@WebServlet("/testServlet")
public class TestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获得请求的完整的url
        StringBuffer url = req.getRequestURL();
        System.out.println("完整的url:"+url);
        //请求的路径为:http://localhost:8080/learn_HttpServletRequest/testServlet
        //打印的结果为:http://localhost:8080/learn_HttpServletRequest/testServlet

        //2.获得资源的名字
        String uri = req.getRequestURI();
        System.out.println("项目名称+资源名称:"+uri);
        //请求的路径为:http://localhost:8080/learn_HttpServletRequest/testServlet
        //打印的结果为:/learn_HttpServletRequest/testServlet   项目名称+资源名称

        //3.或的请求的参数
        String param = req.getQueryString();
        System.out.println("请求的参数:"+param);
        //请求的路径为:http://localhost:8080/learn_HttpServletRequest/testServlet?username=123&password=456
        //打印的结果为:username=123&password=456

        //4.返回客户端的IP地址
        String IP = req.getRemoteAddr();
        System.out.println("访问者的IP地址:"+IP);
        //这个需要在局域网中测试
        //本机的IP地址为:10.8.151.20
        //客户端请求的路径为:http://10.8.151.20:8080/learn_HttpServletRequest/testServlet
        //打印的结果为:10.8.151.61                        ---访问者的IP地址

        //5.获取请求的方式
        String method = req.getMethod();
        System.out.println("请求服务器的方式:"+method);
        //请求的路径为:http://localhost:8080/learn_HttpServletRequest/testServlet
        //打印的结果为:GET

        //6.获取远程主机的IP地址
        String hostIP = req.getRemoteHost();
        System.out.println("远程主机的IP地址:"+hostIP);
        //请求的路径为:http://10.8.151.20:8080/learn_HttpServletRequest/testServlet?username=123
        //打印的结果为:远程主机的IP地址:10.8.151.61        ---访问者的IP地址

        //7.获得远程主机的端口
        int port = req.getRemotePort();
        System.out.println("远程主机的端口:"+port);
        //请求的路径为:http://10.8.151.20:8080/learn_HttpServletRequest/testServlet?username=123
        //打印结果为:远程主机的端口:61308                  ---访问者的端口号

        //8.获得web服务器的地址
        String localAddr = req.getLocalAddr();
        System.out.println("web服务器的地址:"+localAddr);
        //请求的路径为:http://10.8.151.20:8080/learn_HttpServletRequest/testServlet?username=123
        //打印的结果为:web服务器的地址:10.8.151.20         ---本机的IP地址

        //9.获得web服务器的主机的名字
        String localName = req.getLocalName();
        System.out.println("web服务器的主机名:"+localName);
        //请求的路径为:http://10.8.151.20:8080/learn_HttpServletRequest/testServlet?username=123
        //打印的结果为:web服务器的主机名:LAPTOP-D3SQ4GFM   ---本机的名称

        //10.获取前端传入的数据
        String username = req.getParameter("username");
        System.out.println("前端传入的数据为:"+username);
        //请求的路径为:http://10.8.151.20:8080/learn_HttpServletRequest/testServlet?username=123
        //打印的结果为:前端传入的数据为:123

        //11.获取前端传入的所有参数和值
        Map map = req.getParameterMap();
        System.out.println("前端传入的所有的参数:"+map);
        //请求的路径为:http://10.8.151.20:8080/learn_HttpServletRequest/testServlet?username=123
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

 

你可能感兴趣的:(Java知识分享)