lzj312664032
lzj312664032
2009-12-05 23:34

JSP取得客户端IP的问题

  • tomcat

[size=medium]大家帮我一下吧,我在JE上找了一个JSP取得客户端IP地址的方法,但是我放到JSP网页运行的时候总是出错呢?这是为什么呀。我用的是Tomcat 6.0
[code="java"]public String getIpAddr(HttpServletRequest request) {

String ip = request.getHeader("x-forwarded-for");

if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip = request.getHeader("Proxy-Client-IP");

}

if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip = request.getHeader("WL-Proxy-Client-IP");

}

if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip = request.getRemoteAddr();

}

return ip;

}[/code]
出现了这个错误
[code="java"]An error occurred at line: 11 in the jsp file: /ip.jsp
Illegal modifier for the variable getIpAddr; only final is permitted
8: IP地址
9:
10: <%
11: public String getIpAddr(HttpServletRequest request) {

12: String ip = request.getHeader("x-forwarded-for");

13: if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

14: ip = request.getHeader("Proxy-Client-IP");

An error occurred at line: 11 in the jsp file: /ip.jsp
Syntax error on token "(", ; expected
8: IP地址
9:
10: <%
11: public String getIpAddr(HttpServletRequest request) {

12: String ip = request.getHeader("x-forwarded-for");

13: if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

14: ip = request.getHeader("Proxy-Client-IP");

An error occurred at line: 11 in the jsp file: /ip.jsp
Duplicate local variable request
8: IP地址
9:
10: <%
11: public String getIpAddr(HttpServletRequest request) {

12: String ip = request.getHeader("x-forwarded-for");

13: if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

14: ip = request.getHeader("Proxy-Client-IP");

An error occurred at line: 11 in the jsp file: /ip.jsp
Syntax error on token ")", ; expected
8: IP地址
9:
10: <%
11: public String getIpAddr(HttpServletRequest request) {

12: String ip = request.getHeader("x-forwarded-for");

13: if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

14: ip = request.getHeader("Proxy-Client-IP");

An error occurred at line: 22 in the jsp file: /ip.jsp
Void methods cannot return a value
19: if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

20: ip = request.getRemoteAddr();

21: }

22: return ip;

23: }

24: %>
25:

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐