JSP取得客户端IP的问题

[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个回答

首先那 你的写法我很不认识 JSP一般只当表现曾使用 JAVA逻辑最好写在自定义标签里 或者 控制器里

第二那 如果你偏要这么写 那么在JSP里声明方法是这样的
<%!

...在这里写代码

%>
你的没叹号啊 虚拟机每当你是声明处理啊 当然报错了 你得先声明了 再调用啊

写错了 应该是你的写法 我很不赞同

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问