"Servlet" Servlet3.0 interacts with pure JavaScript through Ajax

Source: Internet
Author: User

It's a cliché, and it should be easy for a lot of people. But it is also written to facilitate the Ajax learning of the later. Although js.html is a purely static page, the following programs must be hung on the Tomcat server in order to do Ajax interaction, otherwise it will not see the effect. Eclipse for Java EE takes care to hook up a good project on Tomcat before running Tomcat. This project, in addition to the JSP must be the servlet package, no need to introduce other things. Actually want to directly use a JSP page to complete the project, but now do JSP, basically no one directly in the. jsp file to write something? The background action is thrown into the. java.


I. BASIC OBJECTIVES

The Front js.html input box input things, passed to the background name of Ajaxrequest, address/ajaxrequest Servlet.java. Servlet.java back to the back of the corresponding information to the foreground js.html,js.html not refresh no jump, instant response.



Second, the basic idea

The directory structure of the Javaweb project is as follows.


Because it is Servlet3.0, can be written in the form of annotations servlet,web.xml do not write anything, directly let Eclipse generation, see "Javaweb" Eclipse for Java EE New Web project automatically generated XML (Click to open link)

Just leave the following content:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "version=" 3.0 "></web-app>

Third, the production process

1, first write Servlet.java and js.html which are not so-called, anyway Ajax interaction, these two are one, can not be fragmented.

First look at the js.html,html Layout section is very simple, even the form is not, there are only two input boxes.

Then, when creating Ajax object XMLHttpRequest, be careful not to use the XMLHttpRequest keyword as the name of the Ajax object XMLHttpRequest, or some browsers will not be able to handle it.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">2, after the Servlet.java, in fact Doget and Dopost are on the page to print things, but took this different form. The PrintStream is the output stream from the previous JDK, and PrintWriter appears to be the output stream after JDK1.4. Please refer to "Java" Print stream and buffer reader to complete input and output to file operation (click Open link) and "Java" input and output and JDK1.5 after the new string StringBuilder "(Click to open the link). But this part is too simple, the input and output stream, is the Java compulsory?

Js.html param1 and param2 to this servlet.java, wait for the Servlet.java to print out the corresponding things, and then in the foreground directly through the Xmlhttprequest1.responsetext variable read out.

Package Jsservletajax;import java.io.*;  Import javax.servlet.*;  Import javax.servlet.http.*;    Import javax.servlet.annotation.*; Indicates that the servlet is not a serial number @SuppressWarnings ("Serial")//Description The servlet name is ajaxrequest and its address is/ajaxrequest// This is the same as setting in Web. Xml @WebServlet (name = "Ajaxrequest", Urlpatterns = {"/ajaxrequest"}) public class Servlet extends HttpS Ervlet {///drop user access this servlet protected void doget (HttpServletRequest request, HTTPSERVL, by directly entering the address in the browser) Etresponse response) throws Servletexception, IOException {printstream out = new PrintStream (response.getoutputs          Tream ());          Response.setcontenttype ("Text/html;charset=utf-8");    Out.print ("Please open this page normally"); } protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexcept Ion, IOException {response.setcontenttype ("text/html;    Charset=utf-8 ");    PrintWriter pw = Response.getwriter ();    Request.setcharacterencoding ("Utf-8"); String param1=request.getparameter ("param1");        String param2=request.getparameter ("param2");    Pw.print ("The receptionist came up with parameters: param1=" +param1+ ", param2=" +param2);p W.flush ();p w.close ();   }  }

Iv. Summary

Above, took the pure JavaScript to complete Ajax. Servlet.java just passed a string to js.html!

In fact, you can use jquery to make the foreground code more concise, see "Servlet" in the Servlet3.0 using Json+ajax database query data to the foreground display, no additional JSON parsing package (click Open link), if using SSH, You can refer to the "Struts2" using jquery to implement STRUTS2 AJAX features "(Click to open the link)

"Servlet" Servlet3.0 interacts with pure JavaScript through Ajax

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.