HttpServletRequest对象

什么是HttpServletRequest?有什么作用?

公共接口类HttpServletRequest继承自ServletRequest。客户端浏览器发出的请求被封装成为一个HttpServletRequest对象。对象包含了所有的信息包括请求的地址,请求的参数,提交的数据,上传的文件客户端的ip甚至客户端操作系统都包含在其内。

 

常用方法

request.getContextPath(): /webdemo ---即斜杆加工程名

request.getScheme(): http ---返回请求的方案名,如http,ftp,https等

request.getServerName(): localhost ---服务器主机名

request.getServerPort(): 8080 ---服务器上web应用的访问端口

request.getSession():org.apache.catalina.session.StandardSessionFacade@313b8479 ---取得当前会话对象

 

使用上述方法在jsp页面中生成basePath,这样做最大的好处是在页面中使用绝对路径,不易出错。

在页面中路径可以写出这种形式:<a href="<%=basePath%>pages/hello.jsp">点击我</a>

即该超链接最终的跳转路径为:http://localhost:8080/webdemo/pages/hello.jsp

 

posted @ 2018-04-19 23:56  chenghaow  阅读(265)  评论(0编辑  收藏  举报