Ajax local Refresh application case---simple login _ajax related

Source: Internet
Author: User
Apart, directly to the code, believe that the need is the code rather than a pile of nonsense ...


1.java Code:


Copy Code code as follows:



Package TT;





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 Testa extends HttpServlet {





Private static final long serialversionuid = -7999241892841130740l;





protected void doget (HttpServletRequest request,


HttpServletResponse response) throws Servletexception, IOException {


Request.setcharacterencoding ("Utf-8");


String name = Request.getparameter ("name");





Response.setcontenttype ("Text/xml; Charset=utf-8 ");


Response.setheader ("Cache-control", "No-cache");


PrintWriter out = Response.getwriter ();


Out.println ("<pront>");


if (Name.equals ("Yangjinde")) {


Out.println ("<content>" + "Sorry, ' Yangjinde ' is already registered" + "</content>");


} else {


Out.println ("<content>" + "can register Oh" + "</content>");


}


Out.println ("</pront>");


Out.close ();


}


}





2.jsp Code:


Copy Code code as follows:



<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>


<%


String path = Request.getcontextpath ();


String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";


%>





<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


<html>


<head>


<base href= "<%=basePath%>" >





<title>my JSP ' index.jsp ' starting page</title>


<meta http-equiv= "Pragma" content= "No-cache" >


<meta http-equiv= "Cache-control" content= "No-cache" >


<meta http-equiv= "Expires" content= "0" >


<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >


<meta http-equiv= "description" content= "This are my page" >


<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 returned successfully, start processing information


var res=xmlhttpreq.responsexml.getelementsbytagname ("Content") [0].firstchild.data;


Window.alert (RES);


document.getElementById ("Data"). InnerHTML = res;


document.getElementById ("name"). Value = res;


}else{


Window.alert ("Sorry, the requested page has an exception");


}


}


}


Authentication


function Check () {


var Name=document.getelementbyid ("name"). Value;


if (name== "") {


Alert ("Please enter the content");


return false;


}


else{


Send (' login?name= ' +name);


}


}





</script>


</head>





<body>


<form action= "Login" method= "POST" >


<tr><td> Name: <input id= "name" type= "text" name= "name"/>


<input type= "button" value= "dot I try" onclick= "check ()"/>


</td>


</tr>


<tr><td><div id= "Data" > Wait, I'll change!! </div></td></tr>


</form>


</body>


</html>





3.xml configuration File Code:


Copy Code code as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.4 "
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" >
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>tt. Testa</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>login< ;/servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
< Welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
& Lt;/web-app>
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.