• Servlet--HttpServletRequest一些不常用的方法


    我们在使用Servlet和表单进行交互的时候,不管是传参和接参经常要写一些路径。关于具体的Servlet的传参和接参我后面会有详细的整理,这里先整理一下不怎么常用的到一些HttpServletRequest的方法,这些我们其实都应该是知道的。

    OK,我们用myeclipse new一个jsp页面的时候,最上面的2行会带出信息说:

    <%
    	String path = request.getContextPath();
    	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    %>

    这里的path就是我们在提交表单时候要写的绝对路径的开头,也就是项目名。

    basePath是整个的提交请求时候的完整的路径。


    我下面自己写一个Servlet,里面调用下这些方法,统一做一个整理,虽然不怎么用到,但是还是要知道这些方法的。

    package linkin;
    
    import java.io.IOException;
    import java.util.Enumeration;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class LinkinServlet extends HttpServlet
    {
    	private static final long serialVersionUID = 1L;
    
    	@Override
    	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    	{
    		//请求的上下文路径     /linkin
    		System.out.println(req.getContextPath());
    		//请求的完整的URI /linkin/LinkinServlet
    		System.out.println(req.getRequestURI());
    		//请求的完整的URL http://localhost:8080/linkin/LinkinServlet
    		System.out.println(req.getRequestURL());
    		//请求的Servlet在项目中的路径	/LinkinServlet
    		System.out.println(req.getServletPath());
    		//请求的http请求类型	GET
    		System.out.println(req.getMethod());
    		//请求的协议类型	http
    		System.out.println(req.getScheme());
    		//请求的主机IP	localhost
    		System.out.println(req.getServerName());
    		//请求的主机的端口	8080
    		System.out.println(req.getServerPort());
    		//请求的header里面的信息,这里是get方式,所以里面都是null
    		Enumeration enumeration = req.getHeaderNames();
    		while (enumeration.hasMoreElements())
    		{
    			String header = (String) enumeration.nextElement();
    			System.out.println(header + ":" + req.getHeader("header"));
    			/*host:null
    			connection:null
    			cache-control:null
    			accept:null
    			user-agent:null
    			accept-encoding:null
    			accept-language:null*/
    		}
    	}
    
    	@Override
    	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    	{
    		this.doGet(req, resp);
    	}
    
    }
    

    • 获取请求行的相关信息

    HTTP请求消息的请求行包括请求方式、资源路径和HTTP协议版本:
    GET /lampbrother/servlet/RequestURI?param1=a&param2=b HTTP/1.1
    getMethod:返回HTTP请求消息中的请求方式。
    getRequestURI:返回请求行中的资源名部分。
    getQueryString :返回请求行中的参数部分。
    getProtocol:返回请求行中的协议名和版本。
    getContextPath:返回请求资源所属于的WEB应用程序的路径。
    getPathInfo:返回请求URL中的额外路径信息。额外路径信息是请求URL中的位于Servlet的路径之后和查询参数之前的内容,它以“/”开头。
    getPathTranslated:返回URL中的额外路径信息所对应的资源的真实路径。 
    getServletPath方法:Servlet的名称或Servlet所映射的路径。  

    • 获取网络连接信息
    getRemoteAddr方法返回发出请求的客户机的IP地址,其格式为“192.168.0.3”这种形式的字符文本。 (*) 
    getRemoteHost方法返回发出请求的客户机的完整主机名,即“pc1.atguigu.com”这种格式。
    getRemotePort方法返回发出请求的客户机所使用的网络接口的端口号。
    getLocalAddr方法返回WEB服务器上接收当前请求的网络接口的IP地址。
    getLocalName方法返回WEB服务器上接收当前请求的网络接口的IP地址所对应的主机名。
    getLocalPort方法返回WEB服务器上接收当前请求的网络接口的端口号。
    getServerName方法返回当前请求所指向的主机名。 
    getServerPort方法返回当前请求所连接的服务器端口号。
    getScheme方法返回请求的协议名,例如http、https或ftp。
    getRequestURL方法返回客户端发出请求时的完整URL。   

  • 相关阅读:
    mysql聚合函数
    轮播图与定时器
    3.23 参数 DOM 鼠标点击跟移入事件
    循环+数组
    for循环
    JS讲解跟遇到的问题
    CSS标签和属性回顾,JS初步了解
    2018.03.14理工大网站问题及解决办法
    2018.3.13 浮动 定位
    2018.3.12CSS样式
  • 原文地址:https://www.cnblogs.com/LinkinPark/p/5233009.html
Copyright © 2020-2023  润新知