jsp方式:查询ip地址
jsp中可以声明方法与属性,单是不能直接在<% %>之间声明也不能在<%=%>之间声明。jsp声明
方法或者全局变量使用另一组符号<%! %>举例如下
<%@ page=language="java" contentType="tezxt/html; charset=utf-8"%>
<%@page import="com.helloweenvsfei.util.ip.ipSeeker"%>
<%//这些是注释%!
private ipSeeker ipSeeker=IpSeeker.getInstane();
pubilc String getArea(String ip){
return ipSeeker.getArea(ip);
}
public String getCountry(String ip){
return ipSeeker.getCountry(ip);
}
public boolean isValidIp(String ip){
return ip!=null &&ip.trim().matches("^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$"); //正则表达式判断是否合法
}
%>
<html>
<head>
<title>ip地址查询</title>
<link rel='stylesheet'type='text/css' href='css/style.css'>
</head>
<body><br/>
<%
String ip=request.getparameter("ip");//从request得到iip参数
String area="";
String country="";
if(is ValidIp(ip)){
country=(getCountry(ip);
area=getArea(ip);
}
%>
<div align="center">
<form action="method.jsp" method="get">
<fieldset style='width: 500'>
<legend>ip地址查询</legend>
<table align="center" width"400">
<%
if(isValidIp(ip)){
%>
<tr>
<td align="right">IP地址:</td>
<td><%=ip%></td>
</tr>
<tr>
<td align="right">国家:</td>
<td><%=country%></td>
</tr>
<tr>
<td allign="right">地区:</td>
<td><%=area%></td>
</tr>
<%
}
%>
<tr height="40">
<td align="right">请输入要查询的ip地址:</td>
<td><input type="text"name="ip" value="" style="width:200px;"/></td>
</tr>
<tr height="400">
<td colspan="2" aling="center">
<input type="submit" name="search" value="查询ip
地址" class="button">
</td>
</tr>
</table>
</fieldest>
</form>
</div>
</body>
</html>
从上述代码可知在jsp中可以使用多个"<%! %>"生命多个方法,也可以将多个方法生命在一对<%! %>中