servlet里经常会用到的一些GET方法

本文详细解析了Servlet上下文(ServletContext)和HTTP请求(HttpServletRequest)中常用属性的作用,包括获取上下文路径、真实路径、服务器信息、资源路径、地址、主机名、端口号等,并通过代码示例展示了如何在实际开发中应用这些概念。
 servlet里经常会用到一些GET方法,容易弄混,把它们记在这儿了。


servletContext.getContextPath()=
servletContext.getRealPath("/")=F:\ess-branch\web\
servletContext.getRealPath("")=F:\ess-branch\web
servletContext.getContextPath()=
servletContext.getServerInfo()=Apache Tomcat/6.0.18
servletContext.getResource("/")=jndi:/localhost/
httpServletRequest.getContextPath()=
httpServletRequest.getLocalAddr()=127.0.0.1
httpServletRequest.getLocalName()=localhost
httpServletRequest.getLocalPort()=8090
httpServletRequest.getPathInfo()=null
httpServletRequest.getPathTranslated()=null
httpServletRequest.getProtocol()=HTTP/1.1
httpServletRequest.getQueryString()=null
httpServletRequest.getRealPath("")已过时=F:\ess-branch\web
httpServletRequest.getRemoteAddr()=127.0.0.1
httpServletRequest.getRemoteHost()=127.0.0.1
httpServletRequest.getRemotePort()=3288
httpServletRequest.getRemoteUser()=null
httpServletRequest.getRequestURI()=/support/indexinfo/uploadPicture.htm
httpServletRequest.getRequestURL()=http://localhost:8090/support/indexinfo/uploadPicture.htm
httpServletRequest.getScheme()=http
httpServletRequest.getServerName()=localhost
httpServletRequest.getServerPort()=8090
httpServletRequest.getServletPath()=/support/indexinfo/uploadPicture.htm
httpServletRequest.getUserPrincipal()=null


        ServletContext servletContext = ActionContext.getServletContext();
        HttpServletRequest httpServletRequest = ActionContext.getRequest();
        if (servletContext == null) {
            System.out.println("servletContext is null");
        }
        if (httpServletRequest == null) {
            System.out.println("httpServletRequest is null");
        }
        System.out.println("servletContext.getContextPath()="
                + servletContext.getContextPath());


        System.out.println("servletContext.getRealPath(\"/\")="
                + servletContext.getRealPath("/"));
        System.out.println("servletContext.getRealPath(\"\")="
                + servletContext.getRealPath(""));
        System.out.println("servletContext.getContextPath()="
                + servletContext.getContextPath());
        System.out.println("servletContext.getServerInfo()="
                + servletContext.getServerInfo());


        try {
            System.out.println("servletContext.getResource(\"/\")="
                    + servletContext.getResource("/"));
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        }
        // httpServletRequest
        System.out.println("httpServletRequest.getContextPath()="
                + httpServletRequest.getContextPath());
        System.out.println("httpServletRequest.getLocalAddr()="
                + httpServletRequest.getLocalAddr());
        System.out.println("httpServletRequest.getLocalName()="
                + httpServletRequest.getLocalName());
        System.out.println("httpServletRequest.getLocalPort()="
                + httpServletRequest.getLocalPort());
        System.out.println("httpServletRequest.getPathInfo()="
                + httpServletRequest.getPathInfo());
        System.out.println("httpServletRequest.getPathTranslated()="
                + httpServletRequest.getPathTranslated());
        System.out.println("httpServletRequest.getProtocol()="
                + httpServletRequest.getProtocol());
        System.out.println("httpServletRequest.getQueryString()="
                + httpServletRequest.getQueryString());
        System.out.println("httpServletRequest.getRealPath(\"\")已过时="
                + httpServletRequest.getRealPath(""));
        System.out.println("httpServletRequest.getRemoteAddr()="
                + httpServletRequest.getRemoteAddr());
        System.out.println("httpServletRequest.getRemoteHost()="
                + httpServletRequest.getRemoteHost());
        System.out.println("httpServletRequest.getRemotePort()="
                + httpServletRequest.getRemotePort());
        System.out.println("httpServletRequest.getRemoteUser()="
                + httpServletRequest.getRemoteUser());
        System.out.println("httpServletRequest.getRequestURI()="
                + httpServletRequest.getRequestURI());
        System.out.println("httpServletRequest.getRequestURL()="
                + httpServletRequest.getRequestURL());
        System.out.println("httpServletRequest.getScheme()="
                + httpServletRequest.getScheme());
        System.out.println("httpServletRequest.getServerName()="
                + httpServletRequest.getServerName());
        System.out.println("httpServletRequest.getServerPort()="
                + httpServletRequest.getServerPort());
        System.out.println("httpServletRequest.getServletPath()="
                + httpServletRequest.getServletPath());
        System.out.println("httpServletRequest.getUserPrincipal()="
                + httpServletRequest.getUserPrincipal());
#j2ee开发
 (责任编辑:SQL吧信息编辑)
本文引自:SQL吧|企业网站源码下载(www.sql8.net) 原文参考:http://www.sql8.net/a/course/chengxu/java/servlet/2014/0620/25822.html
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值