您的位置:首页 > Web前端 > JavaScript

jsp获取客户端真实ip地址

2011-11-24 16:59 405 查看
index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>用户登录</title>

</head>

<body>

<form action='deal.jsp' method='post'>

<font color="red">你的ip地址:</font>

<%

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();

}

out.println(ip);

System.out.println(ip);

Cookie c = new Cookie("ip",ip);

c.setMaxAge(24*3600);

response.addCookie(c);

%>

<br>

<font color="red">*</font>代表必填项

<br>

<font color="red">用户名、密码都是123</font>

<br>

<font color="red">

<%

if(request.getAttribute("tip")!=null)

out.print(request.getAttribute("tip"));

/* request.setCharacterEncoding("utf-8");

if(request.getParameter("tip")!=null){

String tip = new String(request.getParameter("tip").getBytes("ISO8859_1"),"utf-8");

out.print(tip);

} */

%>

</font>

<br>

<table>

<tr><td>用户名:</td><td><input name="name" length="20"/><font color="red">*</font></td></tr>

<tr><td>密码:</td><td><input type='password' name="pass" length="20"/><font color="red">*</font></td></tr>

<tr><td>你喜欢的颜色:</td><td><font color="red">*</font></td></tr>

<tr>

<td><font color="red">红:</font><input type="checkbox" name="color" value="红"/></td>

<td> <font color="green">绿:</font><input type="checkbox" name="color" value="绿"/></td>

<td> <font color="blue">蓝:</font><input type="checkbox" name="color" value="蓝"/></td>

</tr>

</table>

<br>

<button>提交</button>

</form>

</body>

</html>

deal.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

pageEncoding="UTF-8" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>用户登录</title>

</head>

<body>

<%

String name = new String(request.getParameter("name").getBytes("ISO8859_1"),"utf-8");

String pass = new String(request.getParameter("pass").getBytes("ISO8859_1"),"utf-8");

String []colors = request.getParameterValues("color");

if("123".equals(name)&&"123".equals(pass)){

out.println("用户名:"+name);

out.println("密码:"+pass);

out.println("你喜欢的颜色:");

if(colors!=null)

{

for(String temp: colors){

String color = new String(temp.getBytes("ISO8859_1"),"utf-8");

if(color.equals("红")){

out.println( "<font color='red'>"+color+"</font>");

}

if(color.equals("绿")){

out.println("<font color='green'>"+color+"</font>");

}

else if(color.equals("蓝")){

out.println("<font color='blue'>"+color+"</font>");

}

}

}

else

{

request.setAttribute("tip", "请选择喜欢的颜色!");

request.getRequestDispatcher("index.jsp").forward(request, response);

}

}

else

{

request.setAttribute("tip", "用户名或者密码错误,请重新输入!");

request.getRequestDispatcher("index.jsp").forward(request, response);

}%>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: