Getting Started with Ajax

Source: Internet
Author: User

<%@ page language= "java" import= "java.util.*" pageencoding= "gb2312"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>ajax verifying that the username exists </TITLE>
<script>

Set a variable

var Xmlhttpreq=false;
Create a XMLHttpRequest Object
function Createxmlhttprequest () {
if (window. XMLHttpRequest) {//mozilla
Xmlhttpreq=new XMLHttpRequest ();
}
else if (window. ActiveXObject) {
try{
Xmlhttpreq=new ActiveXObject ("msxml2.xmlhttp");
}catch (e) {
try{
Xmlhttpreq=new ActiveXObject ("Microsoft.XMLHTTP");
}catch (e) {}
}
}
}
Send Request function
function Send (URL) {
Createxmlhttprequest ();
Xmlhttpreq.open ("Get", url,true);
Xmlhttpreq.onreadystatechange=proce; function that specifies the response
Xmlhttpreq.send (NULL); Send Request
}
function Proce () {
if (xmlhttpreq.readystate==4) {//object state
if (xmlhttpreq.status==200) {//information has been successfully returned to start processing information
<!--test read XML start-up
var root=xmlhttpreq.responsexml;
var res=root.getelementsbytagname ("Content") [0].firstchild.data;
Window.alert (RES);
<!--test reads end of XML--
var xmlreturn = Xmlhttpreq.responsetext;
Window.alert (Xmlreturn);
}else{
Window.alert ("The requested page has an exception");
}
}
}
Identity verification
function Check () {
var Name=document.getelementbyid ("name"). Value;

if (name== "") {
Alert ("Please enter your name!");
return false;
}
else{
Send (' login?name= ' +name);
document.getElementById ("Load"). style.display= ';
Send (' login.do?name= ' +name);
document.getElementById ("Load"). style.display= ' None ';
}
}

</script>

<body>
<form action= "Login" method= "POST" >
<table>
<tr><td> Name: &nbsp;<input id= "name" type= "text" name= "name"/><p>
<div id=load style= "Display:none; Position:absolute;right:0px;top:0px;background: #FF5B5B; border:1px solid "> Verifying your username, please ......</div><p> later
<input type= "button" value= "Detect!" onclick= "check ()"/>
</td>
</tr>
</table>
</form>
</body>

Structs-config configuration:

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE struts-config Public "-//apache software foundation//dtd struts Configuration 1.2//en" "/http Struts.apache.org/dtds/struts-config_1_2.dtd ">

<struts-config>
<data-sources/>
<form-beans >
<form-bean name= "LoginForm" type= "Org.viman.struts.form.LoginForm"/>

</form-beans>

<global-exceptions/>
<global-forwards/>
<action-mappings >
<action
Attribute= "LoginForm"
input= "/login.jsp"
Name= "LoginForm"
Path= "/login"
Scope= "Request"
Type= "Org.viman.struts.action.LoginAction"/>

</action-mappings>

<message-resources parameter= "Org.viman.struts.ApplicationResources"/>
</struts-config>

Action Implementation (Loginaction.java):

Package org.viman.struts.action;

Import java.io.UnsupportedEncodingException;

Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;


Import org.apache.struts.action.Action;
Import Org.apache.struts.action.ActionForm;
Import Org.apache.struts.action.ActionForward;
Import org.apache.struts.action.ActionMapping;
Import Org.viman.struts.form.LoginForm;

/**
* MyEclipse Struts
* Creation date:01-06-2007
*
* XDoclet Definition:
* @struts. Action path= "/login" name= "LoginForm" input= "/login.jsp" scope= "Request" validate= "true"
*/
public class Loginaction extends Action {
/*
* Generated Methods
*/

/**
* Method Execute
* @param mapping
* @param form
* @param request
* @param response
* @return Actionforward
*/
Public Actionforward Execute (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginform = (loginform) form;//TODO auto-generated Method stub
try {
Request.setcharacterencoding ("gb2312");
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}
try{
String msgstr= "";
Response.setcontenttype ("text/xml;charset=gb2312");
Response.setheader ("Cache-control", "No-cache");
String Name= (String) request.getparameter ("name");
System.out.println ("name =" +name);
if ("Gong". Equals (name)) {//Check the database for the user name
MSGSTR = "Sorry, this user name already exists, please change the username registration!";
}else{
MSGSTR = "User not registered, can use!";
}
Response.getwriter (). println ("<?xml version= ' 1.0 ' encoding= ' GB2312 '?> ');
Response.getwriter (). println ("<root>");
Response.getwriter (). println ("<content>");
Response.getwriter (). print (MSGSTR);
Response.getwriter (). println ("</content>");
Response.getwriter (). println ("</root>");
Response.getwriter (). Close ();
}catch (Exception ex) {

}
return null;


 }  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.