Example of Ajax servlet call

Source: Internet
Author: User

You need to write an example for your work to call Servlet and run it,

An index. jsp page, a getandpostexample servlet background, and a web. xml configuration file

 

 

Index. jsp page

 

Bytes -------------------------------------------------------------------------------------------------------

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" gb2312 "%> <br/> <% request. setcharacterencoding ("gb2312"); %> <br/> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312 "/> <br/> <title> Ajax test </title> <br/> <MCE: script language =" JavaScript "> <! -- <Br/> var XMLHTTP; <br/> // create XMLHTTP <br/> function createxmlhttprequest () <br/>{< br/> If (window. activexobject) <br/>{< br/> XMLHTTP = new activexobject ("Microsoft. XMLHTTP "); <br/>}< br/> else if (window. XMLHttpRequest) <br/>{< br/> XMLHTTP = new XMLHttpRequest (); <br/>}</P> <p> // name data to be sent <br/> function createquerystring () <br/>{< br/> var firstname = document. getelementbyid ("firstname "). value; <Br/> var middlename = document. getelementbyid ("middlename "). value; <br/> var Birthday = document. getelementbyid ("Birthday "). value; </P> <p> var querystring = "firstname =" + firstname + "& middlename =" + middlename + "& Birthday =" + birthday; <br/> return querystring; <br/>}</P> <p> // send in get mode <br/> function dorequestusingget () <br/>{< br/> createxmlhttprequest (); <br/> var querystring = ". /getandpostexample? "; <Br/> querystring = querystring + createquerystring () +" & timestamp = "+ new date (). gettime (); <br/> XMLHTTP. onreadystatechange = handlestatechange; <br/> XMLHTTP. open ("get", querystring, true); <br/> XMLHTTP. send (null); <br/>}</P> <p> // send in post mode <br/> function dorequestusingpost () <br/>{< br/> createxmlhttprequest (); <br/> var url = ". /getandpostexample? Timestamp = "+ new date (). gettime (); <br/> var querystring = createquerystring (); <br/> XMLHTTP. open ("Post", URL, true); <br/> XMLHTTP. onreadystatechange = handlestatechange; <br/> XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded"); <br/> XMLHTTP. send (querystring); <br/>}</P> <p> function handlestatechange () <br/>{< br/> If (XMLHTTP. readystate = 4) <br/>{< br/> If (XMLHTTP. status = 200) <br/>{< br/> parseresults (); <br/>}</P> <p> // parse the return value <br/> function parseresults () <br/>{< br/> var responsediv = document. getelementbyid ("serverresponse"); <br/> If (responsediv. haschildnodes () <br/>{< br/> responsediv. removechild (responsediv. childnodes [0]); <br/>}< br/> var responsetext = document. createtextnode (XMLHTTP. responsetext); <br/> alert ("returned value in the background:" + XMLHTTP. responsetext); <br/> responsediv. appendchild (responsetext); <br/>}< br/> // --> </MCE: SCRIPT> <br/> </pead> </P> <p> <body> <br/> <Form ID = "form1" name = "form1" method = "Post "Action =" # "> <br/> <p> <br/> Name: <input name = "firstname" type = "text" id = "firstname"/> <br/> </P> <br/> <p> <br/> <label> <br/> Name: <input type = "text" name = "middlename" id = "middlename"/> <br/> </label> <br/> </P> <br/> <p> <br/> birthday: <input name = "Birthday" type = "text" id = "Birthday"/> <br/> </P> <br/> <p> </P> <br /> <p> <br/> <input type = "button" name = "Submit" value = "get" onclick = "dorequestusingget (); "/> <br/> <input type =" button "name =" submit2 "value =" Post "onclick =" dorequestusingpost (); "/> <br/> </P> <p> <Div id =" serverresponse "> </div> <br/> </form> </ p> <p> </body> <br/> </ptml> </P> <p>

Bytes -------------------------------------------------------------------------------------------------------

 

 

Getandpostexample

Bytes -------------------------------------------------------------------------------------------------------

Package temp; </P> <p> Import Java. io. ioexception; <br/> Import Java. io. printwriter; </P> <p> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> public class getandpostexample extends httpservlet {</P> <p>/** <br/> * constructor of the object. <br/> */<br/> Public getandpostexample () {<br/> super (); <br/>}</P> <p>/** <br/> * destruction of the servlet. <br> <br/> */<br/> Public void destroy () {<br/> super. destroy (); // just puts "Destroy" string in log <br/> // put your code here <br/>}</P> <p>/** <br/> * the doget method of the servlet. <br> <br/> * this method is called when a form has its tag Value Method equals to get. <br/> * @ Param request <br/> * The request send by the client to the server <br/> * @ Param response <br/> * the response send by the server to the client <br/> * @ throws servletexception <br/> * if an error occurred <br/> * @ throws ioexception <br/> * If an error occurred <br/> */<br/> Public void doget (httpservletrequest request, httpservletresponse response) <br/> throws servletexception, ioexception {</P> <p> dopost (request, response ); <br/>}</P> <p>/** <br/> * The dopost method of the servlet. <br> <br/> * this method is called when a form has its tag Value Method equals to <br/> * post. <br/> * @ Param request <br/> * The request send by the client to the server <br/> * @ Param response <br/> * the response send by the server to the client <br/> * @ throws servletexception <br/> * if an error occurred <br/> * @ throws ioexception <br/> * If an error occurred <br/> */<br/> Public void dopost (httpservletrequest request, httpservletresponse response) <br/> throws servletexception, ioexception {</P> <p> string data = ""; <br/> string temp = ""; </P> <p> temp = (string) request. getparameter ("firstname"); <br/> DATA = Data + "first name" + temp; <br/> temp = (string) request. getparameter ("middlename"); <br/> DATA = Data + "Intermediate name" + temp; <br/> temp = (string) request. getparameter ("Birthday"); <br/> DATA = Data + "Birthday" + temp; <br/> temp = (string) request. getparameter ("timestamp"); <br/> DATA = Data + "call time" + temp; </P> <p> system. out. println ("obtained data" + data); </P> <p> response. setcontenttype ("text/html; charset = gb2312"); </P> <p> printwriter out = response. getwriter (); </P> <p> out. println (data); <br/> out. flush (); <br/> out. close (); <br/>}</P> <p>/** <br/> * initialization of the servlet. <br> <br/> * @ throws servletexception <br/> * if an error occurs <br/> */<br/> Public void Init () throws servletexception {<br/> // put your code here <br/>}</P> <p >}< br/>

Bytes -------------------------------------------------------------------------------------------------------

 

 

Web. xml

Bytes -------------------------------------------------------------------------------------------------------

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <web-app version = "2.4" <br/> xmlns = "http://java.sun.com/xml/ns/j2ee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee <br/> http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <br/> <servlet> <br/> <description> This is the description of my J2EE component </description> <br/> <display-Name> This is the display name of my J2EE component </display-Name> <br/> <servlet-Name> getandpostexample </servlet-Name> <br/> <servlet-class> temp. getandpostexample </servlet-class> <br/> </servlet> </P> <p> <servlet-mapping> <br/> <servlet-Name> getandpostexample </Servlet -Name> <br/> <URL-pattern>/getandpostexample </url-pattern> <br/> </servlet-mapping> <br/> <welcome-file-list> <br/> <welcome-File> index. JSP </welcome-File> <br/> </welcome-file-List> <br/> </Web-app> <br/>

Bytes -------------------------------------------------------------------------------------------------------

 

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.