Jquery + ajax + JAVA (servlet) Implement Asynchronous value of the drop-down menu, jqueryajax

Source: Internet
Author: User

Jquery + ajax + JAVA (servlet) Implement Asynchronous value of the drop-down menu, jqueryajax

First, let's look at the engineering structure:

 

The packages required by the project are as follows:

JSP code:

1 <% @ page language = "java" import = "java. util. * "pageEncoding =" GBK "%> 2 <% 3 String path = request. getContextPath (); 4 String basePath = request. getScheme () + ": //" 5 + request. getServerName () + ":" + request. getServerPort () 6 + path + "/"; 7 System. out. println ("basePath =" + basepath); 8%> 9 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 10 
JAVA servlet code:
 1 package servlet; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5  6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 import net.sf.json.JSONArray;11 12 public class ValueGetController extends HttpServlet {13     14     private static final long serialVersionUID = -1293610555518403602L;15 16 17     /**18      * Constructor of the object.19      */20     public ValueGetController() {21         super();22     }23 24     /**25      * Destruction of the servlet. <br>26      */27     public void destroy() {28         super.destroy(); // Just puts "destroy" string in log29         // Put your code here30     }31 32     /**33      * The doGet method of the servlet. <br>34      *35      * This method is called when a form has its tag value method equals to get.36      * 37      * @param request the request send by the client to the server38      * @param response the response send by the server to the client39      * @throws ServletException if an error occurred40      * @throws IOException if an error occurred41      */42     public void doGet(HttpServletRequest request, HttpServletResponse response)43             throws ServletException, IOException {44 45         doPost(request, response);46         47 48     }49 50     /**51      * The doPost method of the servlet. <br>52      *53      * This method is called when a form has its tag value method equals to post.54      * 55      * @param request the request send by the client to the server56      * @param response the response send by the server to the client57      * @throws ServletException if an error occurred58      * @throws IOException if an error occurred59      */60     public void doPost(HttpServletRequest request, HttpServletResponse response)61             throws ServletException, IOException {62 63         request.setCharacterEncoding("GBK");64           response.setContentType("text/html;charset=gbk");65           PrintWriter out = response.getWriter();66           String action = request.getParameter("action");67           System.out.println("action"+action);68           if (action.equals("GetFirstFloorValue")) {69                           String[] str = GetFirstFloorValue();70                           JSONArray ja = JSONArray.fromObject(str);71                           String json = ja.toString();72                            out.print(json);73                           System.out.println(json);74                           out.flush();75                            out.close();76                            return;77                       }78                        out.flush();79                        out.close();80     }81 82     private String[] GetFirstFloorValue() {83           String[] str = new String[4];84                   str[0] = "test1";85                   str[1] = "test2";86                   str[2] = "test3";87                   str[3] = "test4";88                   return str;89 90     }91 92     93 94 }

Servlet web. xml configuration:

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5"  3     xmlns="http://java.sun.com/xml/ns/javaee"  4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7   <servlet> 8     <description>This is the description of my J2EE component</description> 9     <display-name>This is the display name of my J2EE component</display-name>10     <servlet-name>ValueGetController</servlet-name>11     <servlet-class>servlet.ValueGetController</servlet-class>12   </servlet>13 14   <servlet-mapping>15     <servlet-name>ValueGetController</servlet-name>16     <url-pattern>/ValueGetController</url-pattern>17   </servlet-mapping>18   19   20     21   <welcome-file-list>22     <welcome-file>index.jsp</welcome-file>23   </welcome-file-list>24 </web-app>

 

 

 

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.