package com.utils.web;import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;public class Struts2Util { public static HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
} public static HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
public static ServletContext getServletContext() {
return ServletActionContext.getServletContext();
}
public static String getContextPath() {
return getRequest().getContextPath();
}
public static String getQueryString() {
return getRequest().getQueryString();
}
public static String getRequestURL() {
return getRequest().getRequestURL().toString();
} public static String getIp() {
return getRequest().getRemoteAddr();
}
}JSP页面<%@ page language="java" import="java.util.*,com.utils.web.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
This is my JSP page. <br>
<%
Struts2Util util= new Struts2Util(); %>
<%=util.getIp() %>
</body>
</html>错误type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 1613: This is my JSP page. <br>
14: <%
15: Struts2Util util= new Struts2Util(); %>
16: <%=util.getIp() %>
17: </body>
18: </html>
为了在 方法/类 内 得到 Response,Request 获取数据,以方便在 类/方法 内操作。获取IP 只为了说明存在这个错误,无法得到 数据
12: <body>
13: This is my JSP page. <br>
14: <%=Struts2Util.getIp()%>
15: </body>
16: </html>
新建个a。jsp 先访问a。jsp, 跳转到你的jsp。
简单点import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;public class Struts2Util extends ActionSupport{
static HttpServletRequest request = ServletActionContext.getRequest();
public static String getRequestURL() {
return request.getRequestURL().toString();
} public static String getIp() {
return request.getRemoteAddr();
}
}
获取客户端的ip 本机的获取出来是:127....