javascript

位置:IT落伍者 >> javascript >> 浏览文章

ajax验证用户名是否可用


发布日期:2023年01月16日
 
ajax验证用户名是否可用

Ajax很简单(jsp例子)

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

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

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

<%@ page contentType=text/html; charset=GBK %>

<html>

<head>

<title>

ajax

</title>

</head>

<body bgcolor=#ffffff>

<h>

<input name=username type=text maxlength= />

<input id=chknamebtn type=button value=检测帐号 onclick=testName(<%=requestgetContextPath()%>) />

<div id=view_name></div>

</h>

</body>

</html>

<script language=javascript>

if (windowActiveXObject && !windowXMLHttpRequest) {

windowXMLHttpRequest=function() {

return new ActiveXObject((navigatoruserAgenttoLowerCase()indexOf(msie ) != ) ? MicrosoftXMLHTTP : MsxmlXMLHTTP);

};

}//取得XMLHttpRequest对象

function testName(path){

//path是取得系统路径

var view_name=documentgetElementById(view_name);

var req=new XMLHttpRequest();

if (req) {

reqonreadystatechange=function() {

if (reqreadyState== && reqstatus==) {//判断状态是已发送已完成

if(reqresponseText==){

lor=green;

view_nameinnerHTML=该用户名可以正常使用;

}else if(reqresponseText==){

lor=red;

view_nameinnerHTML=该用户名已经被使用;

}else{

lor=red;

view_nameinnerHTML=该用户名含有非法字符!;

}

}

}

reqopen(POST path+/ajaxdo);//struts

//reqopen(POST path+/ajaxservlet);//servlet

//reqopen(POST path+/ajaxaction);//webwork

reqsetRequestHeader(ContentType application/xwwwformurlencoded);

reqsend();//发送参数如果有参数reqsend(username=+user_name);用request取得

}

}

</script>

这个jsp页面并没有取得用户名就是演示一下还有<div>可以换span具体作用问美工吧

接受ajax请求的action

import orgapachestrutsaction*;

import javaxservlethttp*;

import javaxservlethttpHttpServlet;

import javaxservlethttpHttpServletRequest;

import javaxservlethttpHttpServletResponse;

import javaioPrintWriter;

/**

* <p>Title:AjaxAction </p>

*/

public class AjaxAction extends Action {

public ActionForward execute(ActionMapping mapping

ActionForm form

HttpServletRequest request

HttpServletResponse response)

throws Exception {

PrintWriter out = responsegetWriter();

outprint();//ajax取得都是字符的输出如果数据量大的话还可以用xml来发送和接受

return null;

}

}

strutsconfigxml

<action type=testwhwuploadAjaxAction validate=false scope=request path=/ajax/>

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

如果是servlet

webxml

<servlet>

<servletname>AjaxServlet </servletname>

<servletclass>servletAjaxServlet </servletclass>

</servlet>

<servletmapping>

<servletname>AjaxServlet </servletname>

<urlpattern>/AjaxServlet servlet</urlpattern>

</servletmapping>

AjaxServletjava

import javaioIOException;

import javaioPrintWriter;

import javaxservletServletException;

import javaxservlethttpHttpServlet;

import javaxservlethttpHttpServletRequest;

import javaxservlethttpHttpServletResponse;

public class AjaxServlet extends HttpServlet {

Logger log = LoggergetLogger(thisgetClass());

public void doGet(HttpServletRequest request HttpServletResponse response)

throws ServletExceptionIOException {

responsesetContentType(text/xml; charset=GBK);

PrintWriter out = responsegetWriter();

outprint();

}

}

//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

xworkxml

<action name=ajax class=comwhwuploadactionwebworkAjaxAction method=ajax/>

AjaxActionjava

import javaioPrintWriter;

public class AjexAlbumAction extends ActionSupport implements Action{

public void ajax()throws IOException {

PrintWriter pw = ServletActionContextgetResponse()getWriter();

ServletActionContextgetResponse()setContentType(text/html;charset=GBK);

pwprint();

pwclose();

}

}

上一篇:是什么让我们爱上Javascript

下一篇:ajax中如何解决中文乱码