Ajax Local Refresh---login simple example

Source: Internet
Author: User
Tags xmlns
apart, directly to the code, believe that the need is code and not nonsense a bunch of ... 1.java Code:

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 ' This name has been registered" + "</content>");
} else {
Out.println ("<content>" + "can register Oh" + "</content>");
}
Out.println ("</pront>");
Out.close ();
}


}


2.jsp Code:

<%@ 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" >
<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 is 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 has been successfully returned to 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");
}
}
}
Identity verification
function Check () {
var Name=document.getelementbyid ("name"). Value;
if (name== "") {
Alert ("Please enter the content oh");
return false;
}
else{
Send (' login?name= ' +name);
}
}

</script>

<body>
<form action= "Login" method= "POST" >
<tr><td> Name: <input id= "name" type= "text" name= "name"/>
<input type= "button" value= "point I'll Try" onclick= "check ()"/>
</td>
</tr>
<tr><td><div id= "Data" > So I will change oh. </div></td></tr>
</form>
</body>


3.xml configuration file code:

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