ajax提交到java后臺實現(xiàn)處理數(shù)據(jù)的方法是什么-創(chuàng)新互聯(lián)

小編給大家分享一下ajax提交到java后臺實現(xiàn)處理數(shù)據(jù)的方法是什么,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)建站于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目成都網(wǎng)站建設、網(wǎng)站設計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元郎溪做網(wǎng)站,已為上家服務,為郎溪各地企業(yè)和個人服務,聯(lián)系電話:18982081108

環(huán)境:eclipse+struts

要實現(xiàn)的效果:點擊按鈕提交數(shù)據(jù)到后臺之后回到前臺顯示出來數(shù)據(jù)

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
  pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/tupian/20230522/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
<input type="text" id="userinput"> 
<input type="button" id="submit"> 
<p id="msg"></p> 
</body> 
<script type="text/javascript" src="jquery-2.1.0.js"></script> 
<script type="text/javascript"> 
window.onload = function() { 
  document.getElementById("submit").onclick = test; 
} 
function test(){ 
  var userinput = document.getElementById("userinput"); 
  $.post("http://localhost:8080/TestSpring/TestAction",{username:userinput.value}, 
  function(data, textStatus){ 
    document.getElementById("msg").innerHTML = data; 
  });  
} 
</script> 
</html>

struts.xml

<action name="TestAction" class="com.action.Test"> 
  <result>index.jsp</result> 
</action>

Test.java

package com.action; 
 
import java.io.PrintWriter; 
import java.util.Map; 
 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import org.apache.struts2.ServletActionContext; 
 
import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionSupport; 
 
public class Test extends ActionSupport { 
  @Override 
  public String execute() throws Exception { 
    // TODO Auto-generated method stub 
    HttpServletRequest request = org.apache.struts2.ServletActionContext.getRequest(); 
    HttpServletResponse response = ServletActionContext.getResponse(); 
    request.setCharacterEncoding("utf-8"); 
    response.setCharacterEncoding("utf-8"); 
    PrintWriter out = response.getWriter(); 
    out.write(request.getParameter("username")); 
    out.flush(); 
    out.close(); 
    return SUCCESS; 
  } 
}

看完了這篇文章,相信你對ajax提交到java后臺實現(xiàn)處理數(shù)據(jù)的方法是什么有了一定的了解,想了解更多相關知識,歡迎關注創(chuàng)新互聯(lián)-成都網(wǎng)站建設公司行業(yè)資訊頻道,感謝各位的閱讀!

新聞標題:ajax提交到java后臺實現(xiàn)處理數(shù)據(jù)的方法是什么-創(chuàng)新互聯(lián)
URL分享:http://bm7419.com/article24/hssje.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計、小程序開發(fā)、網(wǎng)頁設計公司ChatGPT、品牌網(wǎng)站建設、App設計

廣告

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

商城網(wǎng)站建設