Examples of interaction between Servlet3.0 and pure javascript through Ajax: servlet3.0ajax

Source: Internet
Author: User

Examples of interaction between Servlet3.0 and pure javascript through Ajax: servlet3.0ajax

It should be very simple for many people. However, it is still written to facilitate the development of Ajax.

Although js.html is a pure static page, the following programs must be mounted on the Tomcat server to achieve Ajax interaction. Otherwise, the effect cannot be seen.

Eclipse for javaee pay attention to the installation of a good project on Tomcat to run Tomcat.

In addition to the Servlet package required by JSP, this project does not need to introduce anything else. In fact, I want to directly use a JSP page to complete this project, but now I am engaged in JSP, basically no one has written anything directly in the. jsp file, right? Background actions are all thrown into. java.

I. Basic Objectives

Pass the content entered in the js.html input box to Servlet. java with the background name ajaxRequest and address/ajaxRequest. After servlet.java, the corresponding information is returned to the frontend's js.html. js.html does not refresh and does not redirect, so it responds instantly.

Ii. Basic Ideas

Because it is Servlet3.0, Servlet can be written using annotations. web. xml does not need to write anything, so Eclipse can directly generate

You only need to 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> 

Iii. Production Process

1. At the beginning, I wrote servlet.javaand js.html, which are not called. In Ajax interaction, the two are integrated and cannot be separated.

First, let's look at js.html. The HTML layout is simple, and even there are no forms. There are only two input boxes.

When creating an Ajax object XMLHttpRequest, be sure not to use the XMLHttpRequest keyword as the name of the Ajax object XMLHttpRequest. Otherwise, some browsers cannot handle it.

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

2. Servlet. java is followed. In fact, doGet and doPost both print things on the page, but they adopt different forms. PrintStream is the output stream of the previous JDK, and PrintWriter is the output stream after JDK1.4. However, this part is too simple. The input and output streams are all required courses in Java?
After passing param1 and param2 to the Servlet. java in js.html, wait for the Servlet. java to print out the corresponding content and then read it directly through the XMLHttpRequest1.responseText variable at the front-end.

Package jsServletAjax; import java. io. *; import javax. servlet. *; import javax. servlet. http. *; import javax. servlet. annotation. *; // indicates that this Servlet has no serial number @ SuppressWarnings ("serial") // indicates that the Servlet name is ajaxRequest, and its address is/ajaxRequest // This corresponds to the web. @ WebServlet (name = "ajaxRequest", urlPatterns = {"/ajaxRequest "}) public class Servlet extends HttpServlet {// users can access this servlet protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException by entering the address in the browser, IOException {PrintStream out = new PrintStream (response. getOutputStream (); response. setContentType ("text/html; charsets = UTF-8"); out. print ("please open this page normally");} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, 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 ("front-end parameters: param1 =" + param1 + ", param2 =" + param2); pw. flush (); pw. close ();}}

Iv. Summary
The above uses pure javascript to complete Ajax. Servlet.java.pdf transmits a string to js.html!

In fact, jQuery can be used to make the front-end code shorter.

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.