溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ajax提交到j(luò)ava后臺(tái)實(shí)現(xiàn)處理數(shù)據(jù)的方法是什么

發(fā)布時(shí)間:2020-08-26 11:55:21 來源:億速云 閱讀:161 作者:小新 欄目:編程語言

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

環(huán)境:eclipse+struts

要實(shí)現(xiàn)的效果:點(diǎn)擊按鈕提交數(shù)據(jù)到后臺(tái)之后回到前臺(tái)顯示出來數(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" "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> 
</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; 
  } 
}

看完了這篇文章,相信你對(duì)ajax提交到j(luò)ava后臺(tái)實(shí)現(xiàn)處理數(shù)據(jù)的方法是什么有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI