• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

实例详解ajax提交到java后台之后处理数据方法

php 搞代码 3年前 (2022-01-22) 10次浏览 已收录 0个评论

本文主要为大家带来一篇ajax提交到java后台之后处理数据的实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助到大家。

环境:eclipse+struts

要实现的效果:点击按钮提交数据到后台之后回到前台显示出来数据

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");     Print<em>¥本文来%源[email protected]搞@^&代*@码)网5</em><strong>搞gaodaima代码</strong>Writer out = response.getWriter();     out.write(request.getParameter("username"));     out.flush();     out.close();     return SUCCESS;   } }

相关推荐:

php 事务处理数据示例代码

关于处理数据库的详细介绍

PHP 事务处理数据实现代码_PHP教程

以上就是实例详解ajax提交到java后台之后处理数据方法的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:实例详解ajax提交到java后台之后处理数据方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址