springmvc在jsp获取完整url地址含参数
阅读(1352)
2018-08-24
如果要在jsp页面用java代码获取完整url,直接用页面的request获取是获取不到spring的请求地址的。
spring controller的request和请求转发后jsp request是2个不同的对象。直接上结果:
例如请求地址为:http://localhost:8080/index/test?time=1&no=2
Controller打印:
request.getScheme() = http request.getServerName() = localhost request.getServerPort() = 8080 request.getContextPath() = request.getServletPath() = /index/test request.getRequestURI() = /index/test request.getRequestURL() = http://localhost:8080/index/test request.getQueryString() = time=1&no=2 fullUrl1 = http://localhost:8080/index/test?time=1&no=2 fullUrl2 = http://localhost:8080/index/test?time=1&no=2
Jsp打印:
request.getScheme() = http request.getServerName() = localhost request.getServerPort() = 8080 request.getContextPath() = request.getServletPath() = /WEB-INF/view/index/test.jsp request.getRequestURI() = /WEB-INF/view/index/test.jsp request.getRequestURL() = http://localhost:8080/WEB-INF/view/index/test.jsp request.getQueryString() = time=1&no=2 ===================================================== req.getScheme() = http req.getServerName() = localhost req.getServerPort() = 8080 req.getContextPath() = req.getServletPath() = /index/test req.getRequestURI() = /index/test req.getRequestURL() = http://localhost:8080/index/test req.getQueryString() = time=1&no=2 fullUrl1 = http://localhost:8080/index/test?time=1&no=2 fullUrl2 = http://localhost:8080/index/test?time=1&no=2
获取完整url
String fullUrl1 = request.getScheme() + "://" + request.getServerName() + ":" + //协议 + 服务器域名 (request.getServerPort()==80 ? "" : request.getServerPort()) + //端口 request.getContextPath() + //项目的名字 request.getServletPath() + //目录下全名称 (request.getQueryString()!=null ? ("?"+request.getQueryString()) : ""); //参数 System.out.println("fullUrl1 = "+fullUrl1); String fullUrl2 = request.getRequestURL() + (request.getQueryString()!=null ? ("?"+request.getQueryString()) : ""); System.out.println("fullUrl2 = "+fullUrl2);
如果jsp获取完整url就用
HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
IndexController.java
package com.cxq.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Controller @RequestMapping(value = "/index") public class IndexController { protected Logger logger = LoggerFactory.getLogger(this.getClass()); @RequestMapping("/test") public String test(Model model, HttpServletRequest request, HttpServletResponse response) { System.out.println("request.getScheme() = "+request.getScheme()); System.out.println("request.getServerName() = "+request.getServerName()); System.out.println("request.getServerPort() = "+request.getServerPort()); System.out.println("request.getContextPath() = "+request.getContextPath()); System.out.println("request.getServletPath() = "+request.getServletPath()); System.out.println("request.getRequestURI() = "+request.getRequestURI()); System.out.println("request.getRequestURL() = "+request.getRequestURL()); System.out.println("request.getQueryString() = "+request.getQueryString()); String fullUrl1 = request.getScheme() + "://" + //协议 request.getServerName() + ":" + //服务器域名 (request.getServerPort()==80 ? "" : request.getServerPort()) + //端口 request.getContextPath() + //项目的名字 request.getServletPath() + //目录下全名称 (request.getQueryString()!=null ? ("?"+request.getQueryString()) : ""); System.out.println("fullUrl1 = "+fullUrl1); String fullUrl2 = request.getRequestURL() + (request.getQueryString()!=null ? ("?"+request.getQueryString()) : ""); System.out.println("fullUrl2 = "+fullUrl2); return "index/test"; } }
test.jsp
<%@ page import="org.springframework.web.context.request.ServletRequestAttributes" %> <%@ page import="org.springframework.web.context.request.RequestContextHolder" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>test path</title> <meta name="keywords" content="" /> <meta name="description" content="" /> <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" media="screen"/> <link type="text/css" rel="stylesheet" href="/resources/css/base.css"> </head> <body> <pre> <% out.println("request.getScheme() = "+request.getScheme()); out.println("request.getServerName() = "+request.getServerName()); out.println("request.getServerPort() = "+request.getServerPort()); out.println("request.getContextPath() = "+request.getContextPath()); out.println("request.getServletPath() = "+request.getServletPath()); out.println("request.getRequestURI() = "+request.getRequestURI()); out.println("request.getRequestURL() = "+request.getRequestURL()); out.println("request.getQueryString() = "+request.getQueryString()); out.println("====================================================="); HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); out.println("req.getScheme() = "+req.getScheme()); out.println("req.getServerName() = "+req.getServerName()); out.println("req.getServerPort() = "+req.getServerPort()); out.println("req.getContextPath() = "+req.getContextPath()); out.println("req.getServletPath() = "+req.getServletPath()); out.println("req.getRequestURI() = "+req.getRequestURI()); out.println("req.getRequestURL() = "+req.getRequestURL()); out.println("req.getQueryString() = "+req.getQueryString()); String fullUrl1 = req.getScheme() + "://" + req.getServerName() + ":" + (req.getServerPort()==80 ? "" : req.getServerPort()) + req.getContextPath() + req.getServletPath() + (req.getQueryString()!=null ? ("?"+req.getQueryString()) : ""); out.println("fullUrl1 = "+fullUrl1); String fullUrl2 = req.getRequestURL() + (req.getQueryString()!=null ? ("?"+req.getQueryString()) : ""); out.println("fullUrl2 = "+fullUrl2); %> </pre> </body> </html>
原创文章,转载请注明出处:https://www.weizhixi.com/article/85.html