Partial Ajax refresh-simple logon example

Source: Internet
Author: User
If you don't want to talk about it, just give it to the code. I believe all you need is code instead of a bunch of crap ...... 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' has been registered" + "</content> ");
} Else {
Out. println ("<content>" + "can be registered" + "</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">
<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 is my page">
<SCRIPT>
// Set a variable
VaR xmlhttpreq = false;
// Create an 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 the request Function
Function send (URL ){
Createxmlhttprequest ();
Xmlhttpreq. Open ("get", URL, true );
Xmlhttpreq. onreadystatechange = proce; // specify the Response Function
Xmlhttpreq. Send (null); // send the request
}
Function proce (){
If (xmlhttpreq. readystate = 4) {// object status
If (xmlhttpreq. Status = 200) {// The message is returned successfully. Start to process the 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 ("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 = "Click here to try" onclick = "check ()"/>
</TD>
</Tr>
<Tr> <TD> <Div id = "data"> I will change it later !! </Div> </TD> </tr>
</Form>
</Body>
</Html>

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.