servlet如何接收ajax里传过来的data

$.ajax({type:"POST",url:"http:localhost:8080/Servlet/Login"data:stringsuccess:functio... $.ajax({
type:"POST",
url:"http:localhost:8080/Servlet/Login"
data: string
success:function(returnData){
alert(name)
}
});
});
这里想传一个字符串到服务器端,服务器段应该如何接收呢
展开
 我来答
久会深园心0C
推荐于2016-04-26 · 超过42用户采纳过TA的回答
知道小有建树答主
回答量:113
采纳率:0%
帮助的人:105万
展开全部
ajax的传值类似用java写窗口应用程序,通过按钮=》触发器=》接收函数来完成

1、jsp页面部分ajax传值
function liuyan_chafenyeshu(meiyetiaoshu){

$.post("<%=request.getContextPath()%>/LiuYanAction",{ method:"fenyeshu", tiaoshu:meiyetiaoshu },function (data){
$("#fenyeshu").html(data);
},"text");
}
原理解释:
这个是JS事件挂AJAX发送post方式的json数据。
这里是你需要的:json是通用的数据传递格式,JSON数据使用{}扩起来,里面使用","逗号把每个数据成员分开,然后每个数据成员都是key:val形式。

比如我上面的例子中有method:"fenyeshu"那么在servlet中的service可以使用String method = request.getParameter("method")接到method:"fenyeshu"中的 fenyeshu。后面的tiaoshu等也是同样可接接到。

2、servlet 中web.xml设置(不设置这个没办法传值)
<servlet>
<description></description>
<display-name>LiuYanAction</display-name>
<servlet-name>LiuYanAction</servlet-name>
<servlet-class>xxxxxxxxxx.LiuYanAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LiuYanAction</servlet-name>
<url-pattern>/LiuYanAction</url-pattern>
</servlet-mapping>
原理解释:
jsp页面post的方法"<%=request.getContextPath()%>/LiuYanAction"对应<url-pattern>/LiuYanAction</url-pattern>
然后在对应<servlet-name>LiuYanAction</servlet-name> 找到<display-name>LiuYanAction</display-name>传给执行的类xxxxxxxxxx.LiuYanAction

3、xxxxxxxxxx.LiuYanAction中接收ajax传过来的参数
public class LiuYanAction extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String method = request.getParameter("method");

System.out.println("留言servlet接收到method信息" + method);
原理解释:
String method = request.getParameter("method"); 接可以接到method值
流风子云51d37d
2013-09-03 · TA获得超过498个赞
知道小有建树答主
回答量:810
采纳率:100%
帮助的人:388万
展开全部
对应的servlet获取参数应该是
String string = request.getParameter("string");前一个string随便取名,后面的string必须与data: string中的string一致
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-08-31
展开全部
在servlet的post方法中接收,当一般的JSP的form接收就好

String value=request.getParameter("<Name>");
追问
这里的""要跟哪里对应,能说具体点么
追答
data: string
这是递交中的写法。其实,string是 name=value的写法的,那这个name就递交前的name了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
du瓶邪
推荐于2016-08-09 · TA获得超过2.4万个赞
知道大有可为答主
回答量:1.7万
采纳率:100%
帮助的人:2738万
展开全部
如下:
if (val != "") {
var url = "${pageContext.request.contextPath}/json";
$.post(url, {"name":"Lanny","age":25,"location:"China"}, function(data) {
$("#message").html(data);
});
}
String name =request.getParameter("name");
String age=request.getParameter("age");
String location=request.getParameter("location");
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式