Ajax application (JSP Example)

Source: Internet
Author: User

The first is JSP pages and scripts.

This is a common function to check whether a user name exists.

Struts is used here.

<% @ Page contenttype = "text/html; charset = GBK" %>
<HTML>
<Head>
<Title>
Ajax
</Title>
</Head>
<Body bgcolor = "# ffffff">
<H1>
<Input name = "username" type = "text" maxlength = "20"/>
<Input id = "Chk-name-BTN" type = "button" value = "check account" onclick = "testname ('<% = request. getcontextpath () %> ') "/>
<Div id = "view_name"> </div>
</H1>
</Body>
</Html>
<Script language = "JavaScript">
If (window. activexobject &&! Window. XMLHttpRequest ){
Window. XMLHttpRequest = function (){
Return new activexobject (navigator. useragent. tolowercase (). indexof ('msie 5 ')! =-1 )? 'Microsoft. xmlhttp': 'msxml2. xmlhttp ');
};
} // Get the XMLHTTPRequest object

Function testname (PATH ){

// Path is used to obtain the system path
VaR view_name = Document. getelementbyid ("view_name ");
VaR Req = new XMLHttpRequest ();
If (req ){
Req. onreadystatechange = function (){
If (req. readystate = 4 & Req. Status = 200) {// determines the status. 4 is sent and 200 is complete.
If (req. responsetext = 0 ){
View_name.style.color = 'green ';
View_name.innerhtml = 'this user name can be used properly ';
} Else if (req. responsetext = 1 ){
View_name.style.color = 'red ';
View_name.innerhtml = 'this user name has been used ';
} Else {
View_name.style.color = 'red ';
View_name.innerhtml = 'the user name contains invalid characters! ';
}
}
}
Req. Open ('post', path + '/ajax. Do'); // struts

// Req. Open ('post', path + '/ajax. servlet'); // Servlet

// Req. Open ('post', path + '/ajax. Action'); // webwork

Req. setRequestHeader ('content-type', 'application/X-WWW-form-urlencoded ');
Req. Send (""); // if the sending parameter is Req. Send ("username =" + user_name), use request to obtain
}
}
</SCRIPT>

The user name is not obtained on this JSP page. <Div> you can change the span parameter. For more information, see <div>.

The action that accepts the Ajax request.

Import org. Apache. Struts. Action .*;
Import javax. servlet. http .*;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import java. Io. printwriter;

/**
* <P> title: ajaxaction </P>
*/

Public class ajaxaction extends action {
Public actionforward execute (actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response)
Throws exception {
Printwriter out = response. getwriter ();
Out. Print (1); // Ajax outputs all characters. If the data volume is large, XML can be used for sending and receiving.
Return NULL;
}
}

Struts-config.xml

<Action type = "test. whw. Upload. ajaxaction" Validate = "false" Scope = "request" Path = "/ajax"/>
----------------------------

For Servlet

Web. xml

<Servlet>
<Servlet-Name> ajaxservlet </servlet-Name>
<Servlet-class> servlet. ajaxservlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> ajaxservlet </servlet-Name>
<URL-pattern>/ajaxservlet. servlet </url-pattern>
</Servlet-mapping>

Ajaxservlet. Java

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 ajaxservlet extends httpservlet {
Logger log = logger. getlogger (this. getclass ());
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. setcontenttype ("text/XML; charset = GBK ");
Printwriter out = response. getwriter ();
Out. Print (2 );
}
}
// 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 (){
}
}

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

For webwork

Xwork. xml

<Action name = "ajax" class = "com. whw. Upload. Action. webwork. ajaxaction" method = "ajax"/>

Ajaxaction. Java

Import java. Io. printwriter;

Public class ajexalbumaction extends actionsupport implements action {

Public void Ajax () throws ioexception {
Printwriter PW = servletactioncontext. getresponse (). getwriter ();
Servletactioncontext. getresponse (). setcontenttype ("text/html; charset = GBK ");
PW. Print (1 );
PW. Close ();
}

}

This example can be run in winxpsp2, jb9, eclipse3, j2sdk1.4.1, tomcat5, and tomcat4.1.

 

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.