【实例】ajax提交到java后台之后处理数据
2016-03-22 18:47
821 查看
环境: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">
<div id="msg"></div>
</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;
}
}
要实现的效果:点击按钮提交数据到后台之后回到前台显示出来数据
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">
<div id="msg"></div>
</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;
}
}
相关文章推荐
- JDK中的URLConnection参数详解
- Myeclipse添加mybatis generator插件的手动安装插件的方法
- RSA 解密的时候报错 而且有乱码:java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block
- java中路径的获取
- eclipse使用技巧 之 一行代码写出最好的单例模式
- dubbo+spring_maven 遇到的问题 dubbojar包版本和jdk问题
- java.io.File源码探索
- javax.servlet.ServletException: java.lang.NoClassDefFoundError: count/Counter即找不到JavaBean文件
- Java贪心算法: 田忌赛马
- Java --- HashMap的四种循环遍历方式及性能对比
- java多线程-笔记
- java集合-笔记
- Java --- 读写文件操作
- java中volatile与原子操作
- Lombok - 消除冗长的 java 代码
- java-synchronized
- java异常处理
- 在Eclipse中导入dtd和xsd文件,使XML自动提示 (转)
- Spring Security(13)——session管理
- [转]Spring的IOC原理[通俗解释一下]