jsp方式:查询ip地址与介绍如何生命方法

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中可以使用多个"<%! %>"生命多个方法,也可以将多个方法生命在一对<%! %>中

转载于:https://www.cnblogs.com/a464566082/archive/2013/02/16/2913640.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值