All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

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

Writer: 归海一刀 Article type: Programming skills(编程技巧) Time: 2014/3/16 2:52:05 Browse times: 288 Comment times: 0

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


Head photo

Go homepage
Upload pictures
Write articles

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不久,如有不合理的地方还望指点,谢谢!





































There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.