快捷搜索:  xxx  as  1111

ajax验证用户名是否可用

Ajax很简单(jsp例子)

首先是jsp页面和脚本,为了方便写在一个里面

这是一个很常见的检测用户名是否存在的功能

这里用的是struts ,可以在路径path中加加上要传的值name 在action中连接数据库 验证是否存在

--------------------------------------------------------------------------------

’)" />

if (window.ActiveXObject && !window.XMLHttpRequest) {

window.XMLHttpRequest=function() {

return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf(’msie 5’) != -1) ? ’Microsoft.XMLHTTP’ : ’Msxml2.XMLHTTP’);

};

}//取得XMLHttpRequest工具

function testName(path){

//path是取得系统路径

var view_name=document.getElementById("view_name");

var req=new XMLHttpRequest();

if (req) {

req.onreadystatechange=function() {

if (req.readyState==4 && req.status==200) {//判断状态,4是已发送,200已完成

if(req.responseText==0){

view_name.style.color=’green’;

view_name.innerHTML=’该用户名可以正常应用’;

}else if(req.responseText==1){

view_name.style.color=’red’;

view_name.innerHTML=’该用户名已经被应用’;

}else{

view_name.style.color=’red’;

view_name.innerHTML=’该用户名含有不法字符!’;

}

}

}

req.open(’POST’, path+’/ajax.do’);//struts

//req.open(’POST’, path+’/ajax.servlet’);//servlet

//req.open(’POST’, path+’/ajax.action’);//webwork

req.setRequestHeader(’Content-Type’, ’application/x-www-form-urlencoded’);

req.send("");//发送参数假如有参数req.send("username="+user_name);用request取得

}

}

这个jsp页面并没有取得用户名,便是演示一下。还有

可以换span,详细感化问美工吧。

吸收ajax哀求的action。

import org.apache.struts.action.*;

import javax.servlet.http.*;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.PrintWriter;

/**

*

Title:AjaxAction

*/

public class AjaxAction extends Action {

public ActionForward execute(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws Exception {

PrintWriter out = response.getWriter();

out.print(1);//ajax取得都是字符的输出。假如数据量大年夜的话,还可以用xml来发送和吸收

return null;

}

}

struts-config.xml

----------------------------

假如是servlet

web.xml

AjaxServlet

servlet.AjaxServlet

AjaxServlet

/AjaxServlet .servlet

AjaxServlet.java

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class AjaxServlet extends HttpServlet {

Logger log = Logger.getLogger(this.getClass());

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException,IOException {

response.setContentType("text/xml; charset=GBK");

PrintWriter out = response.getWriter();

out.print(2);

}

}

//Process the HTTP Post request

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

//Process the HTTP Put request

public void doPut(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

}

//Process the HTTP Delete request

public void doDelete(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

}

//Clean up resources

public void destroy() {

}

}

-------------------------------------

假如是webwork

xwork.xml

AjaxAction.java

import java.io.PrintWriter;

public class AjexAlbumAction extends ActionSupport implements Action{

public void ajax()throws IOException {

PrintWriter pw = ServletActionContext.getResponse().getWriter();

ServletActionContext.getResponse().setContentType("text/html;charset=GBK");

pw.print(1);

pw.close();

}

}

您可能还会对下面的文章感兴趣: