zoukankan      html  css  js  c++  java
  • Servlet request常用方法

    Servlet request常用方法

    方法名 作用
    req.getRemoteAddr 获取客户端IP地址
    req.getLocalAddr() 获取服务器IP地址
    req.getRemoteHost 获取客户端的主机名
    req.getRemotePort 获取客户端的端口号
    req.getLocalPort 获取服务器的端口号
    req.getLocalName 获取服务器主机名
    req.getContextPath 获取ContextPath
    req.getServletPath() 获取ServletPath
    req.getMethod 获取请求方式
    req.getQueryString 获取请求的参数的值
    req.getRequestURL 获取请求全路径

    新建类Requests

    继承HttpServlet并重写doGet方法

    package com.szx.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class Requests extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("客户端ip---------req.getRemoteAddr():"+req.getRemoteAddr());
            System.out.println("获取服务器ip------req.getLocalAddr():"+req.getLocalAddr());
            System.out.println("客户端主机名------req.getRemoteHost():"+req.getRemoteHost());
            System.out.println("客户端端口号------req.getRemotePort():"+req.getRemotePort());
            System.out.println("服务器主机名-------req.getLocalName():"+req.getLocalName());
            System.out.println("获取ContextPath--req.getContextPath():"+req.getContextPath());
            System.out.println("ServletPath()---req.getServletPath():"+req.getServletPath());
            System.out.println("请求方式----------req.getMethod():"+req.getMethod());
            System.out.println("请求的值----------req.getQueryString():"+req.getQueryString());
            System.out.println("请求全路径--------RequestURL():"+req.getRequestURL());
        }
    }
    
    

    配置web.xml文件

        <servlet>
            <servlet-name>Requests</servlet-name>
            <servlet-class>com.szx.servlet.Requests</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>Requests</servlet-name>
            <url-pattern>/requests</url-pattern>
        </servlet-mapping>
    

    开启服务并访问地址,查看结果



    其中QueryString为null因为没有传入参数

    添加请求参数查看结果


  • 相关阅读:
    关于php中trim、ltrim和rtrim
    文件读取
    字典
    列表
    数据类型作业
    常用的数据类型
    字符串
    编码
    MVC 入门
    JavaScript 类型浅解
  • 原文地址:https://www.cnblogs.com/xun-/p/12764298.html
Copyright © 2011-2022 走看看