All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
重新计算自动编号_[SQL,Server教程]
查询表里面有N组相同的记录经典SQL语句_[SQL Server教程]
扩展LINQ,to,SQL:使用Lambda,Expression批量删除数据_[Asp.Net教程]
关于ASP.NET编程里Session的用法_.net资料_编程技术
重新安装系统后,Sql,Server,2005,升级注意_[SQL,Server教程]
SQL,server2000简介_mssql学习_编程技术
php5的simplexml解析错误_[PHP教程]
ASP.NET的基本编程习惯_[Asp.Net教程]
注册起动脚本,ASP.NET,AJAX的一项重要功能_.net资料_编程技术
用PHP5的DirectoryIterators递归扫描目录_php资料_编程技术
Reading number is top 10 pictures
The real super beauty12
Athena chu perspective cheongsam shine with New York
The household design, do not do bridal chamber a pity
Kim jong il's mistress, national beauty JinYuJi actor2
girl of HuNan name is LiXiang(湖南辣姐李湘的写真)
NeedWallpaper10
壮丽的云彩1
红楼梦金陵十二钗(2)
A man's favorite things16
沙漠里的美女
Download software ranking
WebService在.NET中的实战应用教学视频 → 第4集
双旗镇刀客B
VeryCD电驴(EasyMule) V1.1.9 Build09081
SP3 for SQL2000
VC++6.0简体中文版
Kung fu panda - the secret of the teacher
dreamweaver8中文版
Ashlynn Video3
艳兽都市
Professional killers2 for Android
归海一刀 published in(发表于) 2014/3/16 2:52:05 Edit(编辑)
ajax实现无刷新验证用户名是否存在_[AJAX教程]

ajax实现无刷新验证用户名是否存在_[AJAX教程]

ajax实现无刷新验证用户名是否存在_[AJAX教程]

实现用的是jdbc+jsp+servlet,数据库用的是mysql


表就2个字段


实现原理获得text的值后,通过调用servlet查询数据库里是否存在,在通过xml传到前台


OperationName.java


response.setContentType("text/xml;charset=UTF-8")这句很重要,没有他前台获得不到xml,之前做的时候没有加


所以前台获得不到xml


package servlet;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class OperationName extends HttpServlet
{


/** *//**
* Constructor of the object.
*/
public OperationName()
{
super();
}


/** *//**
* Destruction of the servlet.

*/
public void destroy()
{
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


/** *//**
* The doGet method of the servlet.

*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String names = request.getParameter("names");
Collection students = null;
Connection con = null;
Statement st = null;
ResultSet rs = null;
try
{
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection("jdbc:mysql:"+
"//127.0.0.1:3306/test?useUnicode=true&characterEncoding=GBK","root","eetrust");
st = con.createStatement();
rs = st.executeQuery("select count(*) from test where name='"+names+"'");
StringBuffer result = new StringBuffer();
result.append("");
System.out.println(result);
while(rs.next())
{
int num = rs.getInt(1);
result.append("" + num + "");
}
result.append("
");
String responseHtml = result.toString();
response.setCharacterEncoding( "UTF-8");
response.setContentType("text/xml;charset=UTF-8");
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
response.getWriter().write(responseHtml);
}catch(Exception e)
{
e.printStackTrace();
}finally
{
try
{
rs.close();
st.close();
con.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}


/** *//**
* The doPost method of the servlet.

*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.doGet(request,response);
}


/** *//**
* Initialization of the servlet.

*
* @throws ServletException if an error occure
*/
public void init() throws ServletException
{
// Put your code here
}


}
index.jsp


ajax("servlet/OperationName?names=" + newOperationName + "&form=form1");"&form=form1"可去掉


<%@ page language="java" contentType="text/html;charset=GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>







My JSP 'MyJsp.jsp' starting page




























asdas















web.xml



xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

OperationName
servlet.OperationName



OperationName
/servlet/OperationName


刚刚接触ajax不久,如有不合理的地方还望指点,谢谢!








添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.