久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

使用jquery的ajax與Javaservlet的交互代碼實例

這篇文章主要介紹了使用jquery 的ajax 與 Java servlet的交互代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)贛州免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了1000多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

由于是使用jquery的 所以別忘記導(dǎo)入jq

下面是jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Insert title here</title>
  <script src="js/jquery-3.3.1.js"></script>
  <script type="text/javascript">
    function ajaxFun(){
      $.ajax({
        type: "POST",              //傳數(shù)據(jù)的方式
        url: "user",               //servlet地址
 
        data: $('#form').serialize(),      //傳的數(shù)據(jù) form表單 里面的數(shù)據(jù)
        success: function(result){        //傳數(shù)據(jù)成功之后的操作  result是servlet傳過來的數(shù)據(jù) 這個函數(shù)對result進行處理,讓它顯示在 輸入框中
          $("#results").val(result);      //找到輸入框 并且將result的值 傳進去
        }
      });
    }
 
  </script>
</head>
<body>
<H1 align="center">請輸入兩個字符串</H1>
<div align="center">
  <form id="form">
    first:<input type="text" name="first" id="first">
    second:<input type="text" name="second" id="second"><br data-filtered="filtered">     //點擊按鈕的時候觸發(fā)屬性
    <button type="button" onclick="ajaxFun()">button</button> //提交 按鈕
  </form>
  <input type="text" id="results">
  // 用來 顯示 result
</div>
</body>
</html>

然后是 servlet 文件

package com.java.servlet;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * Ajax 訓(xùn)練
 * */
public class UserServlet extends javax.servlet.http.HttpServlet {
  protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
    this.doGet(request,response);
  } 
  protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("UTF-8");
    PrintWriter out = response.getWriter(); 
    String first = request.getParameter("first");      //從前端獲取數(shù)據(jù)first
    String second = request.getParameter("second");     //從前端獲取數(shù)據(jù)second
    String result=first+second;
    System.out.println(result);                   //用于測試 ,判斷是否成功獲取到數(shù)據(jù);
    out.println(result);                         //將數(shù)據(jù)傳到前端
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

新聞名稱:使用jquery的ajax與Javaservlet的交互代碼實例
網(wǎng)站網(wǎng)址:http://sd-ha.com/article8/ihhsip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈定制網(wǎng)站、品牌網(wǎng)站制作、軟件開發(fā)微信小程序、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站