//我这几天学到ajax看到有道API查询就做了一个简单的查询系统
//这个是网页使用了twitter的框架
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>小工具</title>
<%@ include file="import.jsp"%>
<style type="text/css">
body {
margin-top: 50px;
}
#prependedInput {
height: 28px;
}
.input-prepend {
margin-left: 10px;
margin-top: 50px;
float: left;
margin-top: 50px;
}
.btn-group {
margin-left: 50px;
margin-top: 50px;
float: left;
}
#loading {
display: none;
}
.well {
clear: both;
width: 380px;
margin-left: 130px;
font-size: 20px;
display: none;
}
.nav-list li {
margin-top: 20px;
}
</style>
</head>
<body>
<%@ include file="navber.jsp"%>
<div class="container">
<div class="btn-group">
<a id="find" class="btn btn-primary" href="javascript:void(0)">查询</a>
<a class="btn btn-primary dropdown-toggle" href="javascript:void(0)"
data-toggle="dropdown" style="height: 18px;"> <span
class="caret"></span> </a>
<ul class="dropdown-menu">
<li><a href="javascript:void(0)">IP地址</a>
</li>
<li><a href="javascript:void(0)">电话归属地</a>
</li>
<li><a href="javascript:void(0)">身份证信息</a>
</li>
<li class="divider"></li>
<li><a href="javascript:void(0)">其他</a>
</li>
</ul>
</div>
<div class="input-prepend">
<span class="add-on">IP地址</span> <input id="prependedInput"
class="span5" type="text" size="16"><span id="loading"><img
src="img/loading.gif" /> </span>
</div>
<div class="well">
<ul class="nav nav-list">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
</body>
<script type="text/javascript">
$(function() {
var type = "ip";
$(".dropdown-menu li a").click(function() {
if ($(this).text() == "IP地址") {
type = "ip";
} else if ($(this).text() == "电话归属地") {
type = "tel";
} else if ($(this).text() == "身份证信息") {
type = "id";
} else {
type = "";
}
$(".add-on").text($(this).text());
});
$("#find").click(function() {
$(".nav-list li").text("");
$(".well").hide();
if (type.length != 0) {
sendMSG(type, $("#prependedInput").val());
}
});
$("#prependedInput").click(function() {
$(".nav-list li").text("");
$(".well").hide();
});
var xmlHttp;
function createXMLRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
xmlHttp = new XMLHttpRequest();
}
}
function sendMSG(type, value) {
createXMLRequest();
xmlHttp.open("post", "login.jspx", true);
xmlHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = callback;
xmlHttp.send("task=find&type=" + type + "&value=" + value);
}
function callback() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
$("#loading").hide();
respxml = xmlHttp.responseXML;
if (type == "id") {
idxml(respxml);
} else if (type == "ip") {
ipxml(respxml);
} else if (type == "tel") {
telxml(respxml);
}
}
} else {
$("#loading").show();
}
}
function idxml(xml) {
$(".nav-list li").text("");
var preson = xml.getElementsByTagName("product");
if (preson.length == 0) {
$(".nav-list li:eq(0)").text("信息提示: 您查询的身份证信息不存在");
$(".well").show();
return;
}
preson = preson[0];
id = preson.getElementsByTagName("code")[0].childNodes[0].nodeValue;
add = preson.getElementsByTagName("location")[0].childNodes[0].nodeValue;
bir = preson.getElementsByTagName("birthday")[0].childNodes[0].nodeValue;
sex = preson.getElementsByTagName("gender")[0].childNodes[0].nodeValue;
$(".nav-list li:eq(0)").text("身份证号码: " + id);
$(".nav-list li:eq(1)").text("家庭位置: " + add);
$(".nav-list li:eq(2)").text(
"出生日期: " + bir.substring(0, 4) + "年" + bir.substring(4, 6)
+ "月" + bir.substring(6) + "日");
if (sex == "m") {
sex = "男";
} else {
sex = "女";
}
$(".nav-list li:eq(3)").text("性别: " + sex);
$(".well").show();
}
function telxml(xml) {
$(".nav-list li").text("");
var telphone = xml.getElementsByTagName("product");
if (telphone.length == 0) {
$(".nav-list li:eq(0)").text("信息提示: 您查询的电话信息不存在");
$(".well").show();
return;
}
telphone = telphone[0];
number = telphone.getElementsByTagName("phonenum")[0].childNodes[0].nodeValue;
add = telphone.getElementsByTagName("location")[0].childNodes[0].nodeValue;
$(".nav-list li:eq(0)").text("手机号码: " + number);
$(".nav-list li:eq(1)").text("归属地: " + add);
$(".well").show();
}
function ipxml(xml) {
$(".nav-list li").text("");
var address = xml.getElementsByTagName("product");
if (address.length == 0) {
$(".nav-list li:eq(0)").text("信息提示: 您查询的IP地址不存在");
$(".well").show();
return;
}
address = address[0];
ip = address.getElementsByTagName("ip")[0].childNodes[0].nodeValue;
add = address.getElementsByTagName("location")[0].childNodes[0].nodeValue;
$(".nav-list li:eq(0)").text("IP号码: " + ip);
$(".nav-list li:eq(1)").text("归属地: " + add);
$(".well").show();
}
});
</script>
</html>
//这个事使用servlet进行跳转和信息传递
String task = request.getParameter("task");
String method = request.getMethod();
if (task == null || task.equals("")) {
if (method.equalsIgnoreCase("get")) {
toLogin(request, response);
} else if (method.equalsIgnoreCase("post")) {
login(request, response);
}
} else if (task.equals("loginout")) {
loginOut(request, response);
} else if (task.equals("check")) {
check(request, response);
} else if (task.equals("tools")) {
toTools(request, response);
} else if (task.equals("find")) {
find(request, response);
}
}
private void find(HttpServletRequest request, HttpServletResponse response)
throws IOException {
String type = request.getParameter("type");
String value = request.getParameter("value");
response.setCharacterEncoding("utf-8");
response.setContentType("text/xml");
PrintWriter out = response.getWriter();
LoadXML xml = new LoadXML();
String text = "";
if (type.equals("ip")) {
text = xml.findIP(value);
} else if (type.equals("id")) {
text = xml.findId(value);
} else if (type.equals("tel")) {
text = xml.findTelPhone(value);
}
out.print(text);
out.flush();
out.close();
}
private void toTools(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("WEB-INF/view/tools.jsp").forward(request,
response);
}
private void check(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
User user = userServic.checkUsername(username);
if (user != null) {
out.print("ok");
} else {
out.print("error");
}
out.flush();
out.close();
}
private void loginOut(HttpServletRequest request,
HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
session.invalidate();
response.sendRedirect("login.jspx");
}
private void toLogin(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("WEB-INF/view/login.jsp").forward(request,
response);
}
private void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = userServic.login(username, password);
if (user != null) {
HttpSession session = request.getSession();
session.setAttribute("user", user);
response.sendRedirect("admin.jspx?task=view");
} else {
response.sendRedirect("login.jspx?errorcode=-00001");
}
}
}
//这个是java解析从客户端哪来的数据
package com.kaishengit.tools;
public class LoadXML {
private WebInfo info = new WebInfo();
public String findId(String id) {
String url = "http://www.youdao.com/smartresult-xml/search.s?type=id&q="
+ id;
return info.getXML(url);
}
public String findTelPhone(String tel) {
String url = "http://www.youdao.com/smartresult-xml/search.s?type=mobile&q="
+ tel;
return info.getXML(url);
}
public String findIP(String ip) {
String url = "http://www.youdao.com/smartresult-xml/search.s?type=ip&q="
+ ip;
return info.getXML(url);
}
}
//这个事抓取网页上的xml文件
package com.kaishengit.tools;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class WebInfo {
public String getXML(String url) {
URL ur = null;
try {
ur = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection conn = getURLConnection(ur);
if (conn == null) {
return "";
}
return readWebInfo(conn);
}
private HttpURLConnection getURLConnection(URL url) {
URLConnection conn = null;
try {
conn = url.openConnection();
HttpURLConnection httpconn = (HttpURLConnection) conn;
int responseCode = httpconn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
return httpconn;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private String readWebInfo(HttpURLConnection conn) {
BufferedReader in;
StringBuilder sb = new StringBuilder();
try {
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String text = "";
while ((text = in.readLine()) != null) {
sb.append(text);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}