jsp获取客户端真实ip地址

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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值