Use Ajax get and post to invoke a simple example of the servlet.

Source: Internet
Author: User
Tags date string window
Ajax|servlet| sample

Ajax Web page Code ****************

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Untitled Document </title>
<script language= "JavaScript" >
var xmlHttp;
function Createxmlhttprequest ()
{
if (window. ActiveXObject)
{
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}
else if (window. XMLHttpRequest)
{
Xmlhttp=new XMLHttpRequest ();
}
}
function createquerystring ()
{
var Firstname=document.getelementbyid ("FirstName"). Value;
var Middlename=document.getelementbyid ("MiddleName"). Value;
var Birthday=document.getelementbyid ("Birthday"). Value;
var querystring= "Firstname=" + firstName + "&middlename=" + middlename + "&birthday=" + birthday;
return querystring;
}
function Dorequestusingget ()
{
Createxmlhttprequest ();
var querystring= ". /getandpost? ";
Querystring=querystring+createquerystring () + "&timestamp=" + New Date (). GetTime ();
Xmlhttp.onreadystatechange=handlestatechange;
Xmlhttp.open ("Get", querystring,true);
Xmlhttp.send (NULL);
}
function Dorequestusingpost ()
{
Createxmlhttprequest ();
var url= ". /getandpost?timestamp= "+ New Date (). GetTime ();
var querystring=createquerystring ();
Xmlhttp.open ("POST", url,true);
Xmlhttp.onreadystatechange=handlestatechange;
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.send (querystring);
}
function Handlestatechange ()
{
if (xmlhttp.readystate==4)
{
if (xmlhttp.status==200)
{
Parseresults ();
}
}
}
function Parseresults ()
{
var Responsediv=document.getelementbyid ("Serverresponse");
if (Responsediv.haschildnodes ())
{
Responsediv.removechild (Responsediv.childnodes[0]);
}
var responsetext=document.createtextnode (Xmlhttp.responsetext);
Responsediv.appendchild (ResponseText);
}
</script>

<body>
<form id= "Form1" Name= "Form1" method= "Post" action= "#"
  <p><br/>
    <br/>
    <input name= "FirstName" type= "text" id= "FirstName"/>
</p>
  <p>
    <label>
    <input type= "text" name = "MiddleName" id= "MiddleName"  />
    </label>
</p>
  <p>
    <input name= "Birthday" type= "text" id= "Birthday"/>
  </p>
  <p > </p>
  <p>
    <input type= "button" Name= "Submit" value= "get"  />
                &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP
 <input type= "button" Name= "Submit2" value= "POST"  /
  </p>

<div id= "Serverresponse" ></div>
</form>

</body>

Servlet Code *****************

Package temp;

Import java.io.*;
Import java.net.*;
Import javax.servlet.*;
Import javax.servlet.http.*;

public class Getandpostexample extends httpservlet{
protected void ProcessRequest (HttpServletRequest request,httpservletresponse response,string method) throws Servletexception,ioexception
{
Response.setcontenttype ("Text/xml");

String firstname=request.getparameter ("FirstName");
String middlename=request.getparameter ("MiddleName");
String birthday=request.getparameter ("Birthday");

String responsetext= "Hello:" + firstName + "-" + MiddleName + ". Your birthday is "+ Birthday +". "+" [Method: ' + method + '] ';

PrintWriter Out=response.getwriter ();
Out.println (ResponseText);

Out.close ();
}

protected void doget (HttpServletRequest request,httpservletresponse response) throws Servletexception,ioexception
{
ProcessRequest (Request,response, "get");
}

protected void DoPost (HttpServletRequest request,httpservletresponse response) throws Servletexception,ioexception
{
ProcessRequest (Request,response, "POST");
}

}
**************************



Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.