基本的JSP中调用Ajax与Servlet进行数据交互

首先创建jsp页面中  然后在页面中写入js代码!
  1. <span style=“font-size:18px;”><%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8” contentType=“text/html; charset=UTF-8”%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN”>  
  8. <html>  
  9.   <head>  
  10.     <base href=“<%=basePath%>”>  
  11.       
  12.     <title>My JSP ‘index.jsp’ starting page</title>  
  13.     <meta http-equiv=“pragma” content=“no-cache”>  
  14.     <meta http-equiv=“cache-control” content=“no-cache”>  
  15.     <meta http-equiv=“expires” content=“0”>      
  16.     <meta http-equiv=“keywords” content=“keyword1,keyword2,keyword3”>  
  17.     <meta http-equiv=“description” content=“This is my page”>  
  18.     <!– 
  19.     <link rel=”stylesheet” type=”text/css” href=”styles.css”> 
  20.     –>  
  21.     <script type=“text/javascript” src=“<%=path%>/js/jquery-2.1.1.min.js” ></script>  
  22.     <script type=“text/javascript”>  
  23.      function  sub(){  
  24.        .ajax({&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type:"GET",&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url:"/Ajaxex/testServlet",&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data:{username:("#name").val()},&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;statusCode:&nbsp;{404:&nbsp;function()&nbsp;{&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert('page&nbsp;not&nbsp;found');&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;success:function(data,textStatus){&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(“#sp”).html(data);  
  25.        }  
  26.        });  
  27.      }  
  28.     </script>  
  29.   </head>  
  30.   <body>  
  31.     This is my JSP page. <br>  
  32.        <input type=“text” name=“username” id=“name”>  
  33.        <br>  
  34.        <input type=“submit” id=“btn” onclick=“sub()”>  
  35.     <br>  
  36.     result : <span id=“sp”></span>  
  37.   </body>  
  38. </html>  
  39.   
  40. 写一个Servlet类   
  41.   
  42. package com.hal.servlet;  
  43.   
  44. import java.io.IOException;  
  45. import java.io.PrintWriter;  
  46.   
  47. import javax.servlet.ServletException;  
  48. import javax.servlet.http.HttpServlet;  
  49. import javax.servlet.http.HttpServletRequest;  
  50. import javax.servlet.http.HttpServletResponse;  
  51. import javax.xml.ws.Dispatch;  
  52.   
  53. public class testServlet extends HttpServlet {  
  54.   
  55.     /**  
  56.      *   
  57.      */  
  58.     private static final long serialVersionUID = 1L;  
  59.   
  60.     /**  
  61.      * Constructor of the object.  
  62.      */  
  63.     public testServlet() {  
  64.         super();  
  65.     }  
  66.   
  67.     /**  
  68.      * Destruction of the servlet. <br>  
  69.      */  
  70.     public void destroy() {  
  71.         super.destroy(); // Just puts “destroy” string in log  
  72.         // Put your code here  
  73.     }  
  74.   
  75.     /**  
  76.      * The doGet method of the servlet. <br>  
  77.      *  
  78.      * This method is called when a form has its tag value method equals to get.  
  79.      *   
  80.      * @param request the request send by the client to the server  
  81.      * @param response the response send by the server to the client  
  82.      * @throws ServletException if an error occurred  
  83.      * @throws IOException if an error occurred  
  84.      */  
  85.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  86.             throws ServletException, IOException {  
  87.   
  88.         response.setContentType(“text”);  
  89.         String   username = request.getParameter(“username”);  
  90.         System.out.println(username);  
  91.         PrintWriter out = response.getWriter();  
  92.         out.write(username);  
  93.         request.getRequestDispatcher(“index.jsp”).forward(request, response);  
  94.         out.flush();  
  95.         out.close();  
  96.     }  
  97.   
  98.     /**  
  99.      * The doPost method of the servlet. <br>  
  100.      *  
  101.      * This method is called when a form has its tag value method equals to post.  
  102.      *   
  103.      * @param request the request send by the client to the server  
  104.      * @param response the response send by the server to the client  
  105.      * @throws ServletException if an error occurred  
  106.      * @throws IOException if an error occurred  
  107.      */  
  108.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  109.             throws ServletException, IOException {  
  110.   
  111.         response.setContentType(“text”);  
  112.         PrintWriter out = response.getWriter();  
  113.         out.flush();  
  114.         out.close();  
  115.     }  
  116.   
  117.     /**  
  118.      * Initialization of the servlet. <br>  
  119.      *  
  120.      * @throws ServletException if an error occurs  
  121.      */  
  122.     public void init() throws ServletException {  
  123.         // Put your code here  
  124.     }  
  125.   
  126. }  
  127.   
  128. 配置web.xml文件  
  129.   
  130. <?xml version=“1.0” encoding=“UTF-8”?>  
  131. <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” id=“WebApp_ID” version=“3.0”>  
  132.   <display-name>Ajaxex</display-name>  
  133.   <servlet>  
  134.     <description>This is the description of my J2EE component</description>  
  135.     <display-name>This is the display name of my J2EE component</display-name>  
  136.     <servlet-name>AjaxexServlet</servlet-name>  
  137.     <servlet-class>com.hal.servlet.AjaxexServlet</servlet-class>  
  138.   </servlet>  
  139.   <servlet>  
  140.     <description>This is the description of my J2EE component</description>  
  141.     <display-name>This is the display name of my J2EE component</display-name>  
  142.     <servlet-name>testServlet</servlet-name>  
  143.     <servlet-class>com.hal.servlet.testServlet</servlet-class>  
  144.   </servlet>  
  145.   
  146.   
  147.   <servlet-mapping>  
  148.     <servlet-name>AjaxexServlet</servlet-name>  
  149.     <url-pattern>/AjaxexServlet</url-pattern>  
  150.   </servlet-mapping>  
  151.   <servlet-mapping>  
  152.     <servlet-name>testServlet</servlet-name>  
  153.     <url-pattern>/testServlet</url-pattern>  
  154.   </servlet-mapping>  
  155.   <welcome-file-list>  
  156.     <welcome-file>index.html</welcome-file>  
  157.     <welcome-file>index.htm</welcome-file>  
  158.     <welcome-file>index.jsp</welcome-file>  
  159.     <welcome-file>default.html</welcome-file>  
  160.     <welcome-file>default.htm</welcome-file>  
  161.     <welcome-file>default.jsp</welcome-file>  
  162.   </welcome-file-list>  
  163. </web-app>  
  164. </span>  
<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <script type="text/javascript" src="<%=path%>/js/jquery-2.1.1.min.js" ></script>
    <script type="text/javascript">
     function  sub(){
       $.ajax({
       type:"GET",
       url:"/Ajaxex/testServlet",
       data:{username:$("#name").val()},
       statusCode: {404: function() {
            alert('page not found'); }
         },    
       success:function(data,textStatus){
       $("#sp").html(data);
       }
       });
     }
    </script>
  </head>
  <body>
    This is my JSP page. <br>
       <input type="text" name="username" id="name">
       <br>
       <input type="submit" id="btn" οnclick="sub()">
    <br>
    result : <span id="sp"></span>
  </body>
</html>

写一个Servlet类 

package com.hal.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Dispatch;

public class testServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * Constructor of the object.
     */
    public testServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text");
        String   username = request.getParameter("username");
        System.out.println(username);
        PrintWriter out = response.getWriter();
        out.write(username);
        request.getRequestDispatcher("index.jsp").forward(request, response);
        out.flush();
        out.close();
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text");
        PrintWriter out = response.getWriter();
        out.flush();
        out.close();
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

配置web.xml文件

<?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" id="WebApp_ID" version="3.0">
  <display-name>Ajaxex</display-name>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>AjaxexServlet</servlet-name>
    <servlet-class>com.hal.servlet.AjaxexServlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>com.hal.servlet.testServlet</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>AjaxexServlet</servlet-name>
    <url-pattern>/AjaxexServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern>/testServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值