Ajax Simple Example (JSP)

Source: Internet
Author: User
Tags http post return window
Ajax|js First is the JSP page and the script, in order to conveniently write in one inside

This is a very common feature that detects whether a user name exists

This is with struts.

<%@ page contenttype= "text/html; CHARSET=GBK "%>
<title>
Ajax
</title>
<body bgcolor= "#ffffff" >
<input name= "username" type= "text" maxlength= "/>"
<input id= "chk-name-btn" type= "button" value= "Detect account"/>
<div id= "View_name" ></div>
</body>
<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 XMLHttpRequest Object

function TestName (path) {

Path is to get system paths
var View_name=document.getelementbyid ("View_name");
var req=new xmlhttprequest ();
if (req) {
Req.onreadystatechange=function () {
if (req.readystate==4 && req.status==200) {//Judgment status, 4 is sent, 200 completed
if (req.responsetext==0) {
View_name.style.color= ' green ';
View_name.innerhtml= ' The user name can be used normally ';
}else if (req.responsetext==1) {
View_name.style.color= ' Red ';
View_name.innerhtml= ' The user name has already been used ';
}else{
View_name.style.color= ' Red ';
View_name.innerhtml= ' The username contains illegal 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 ("")//Send parameter If there is a parameter req.send ("Username=" +user_name);
}
}
</script>

This JSP page does not get user name, is demo. There are <div> can change span, specific role to ask Art bar.

An action that accepts an 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 get all the output of the character. If the volume of data is large, you can also use XML to send and accept
return null;
}
}

Struts-config.xml

<action type= "Test.whw.upload.AjaxAction" validate= "false" scope= "request" path= "/ajax"/>
----------------------------

If this is a servlet

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 () {
}
}

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

If it's 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, 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.