java获取访问路径、域名、项目名、请求入参
//测试页面-跳转到输入数据的form表单
public String test1(){
ActionContext ctx = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
String url = request.getScheme()+”://”+ request.getServerName()+request.getRequestURI()+”?”+request.getQueryString();
System.out.println(“获取全路径(协议类型://域名/项目名/命名空间/action名称?其他参数)url=”+url);
String url2=request.getScheme()+”://”+ request.getServerName();//+request.getRequestURI();
System.out.println(“协议名://域名=”+url2);
System.out.println(“获取项目名=”+request.getContextPath());
System.out.println(“获取参数=”+request.getQueryString());
System.out.println(“获取全路径=”+request.getRequestURL());
return “success”;
}
补充:一般而言,端口默认为80的时候是不需要显性说明的,但是在端口不为80的时候是需要的,即使在存在域名的情况下,这样在测试的时候,我们应该把端口也加到路径中
request.getServerPort()