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>

解决方案 »

  1.   

    补充说明:
    为了在 方法/类 内 得到  Response,Request 获取数据,以方便在 类/方法 内操作。获取IP 只为了说明存在这个错误,无法得到 数据
      

  2.   

    static 直接调用就是了,还new什么啊?<%=Struts2Util.getIp()%>
      

  3.   

    还是一样的错误!ype Exception reportmessage 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 1411:   </head>
    12:   <body>
    13:     This is my JSP page. <br>
    14:     <%=Struts2Util.getIp()%>
    15:   </body>
    16: </html>
      

  4.   

    搞那么麻烦,在jsp页面直接request就可以得到。action里面才勇你的那个类
      

  5.   

    汗!为了 在 action用的,所以 先测试是否有错啊
      

  6.   

    你直接访问的jsp页面吧? getRequest()空指针了。
    新建个a。jsp 先访问a。jsp,  跳转到你的jsp。
      

  7.   


    简单点
    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();
        }
    }
      

  8.   

    获得servlet api的request即可获得服务器端、客户端ip、port、addr的信息
      

  9.   

     request.getRemoteAddr();
      
    获取客户端的ip   本机的获取出来是:127....