码迷,mamicode.com
首页 > 编程语言 > 详细

前端form标签发送post请求,服务器用Java类继承HttpServlet,并返回数据

时间:2015-08-27 02:05:44      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

1、form表单提交:     

 <form action="http://localhost:8080/test/Login" method="post">

用户:<input type="text" name="username"/><br/>

密码:<input type="password" name="password"/><br/>

<input type="submit" value="登录"/>

</form>

2、js提交:
        (1)添加:<script type="text/javascript" src="assets/jquery-2.1.1.js"></script>,可以用其他的,注意要有$.post,有的版本没    有;

(2)var test={

name:"me",

password:"password" 

};

$.post("http://localhost:8080/test/Login",

test, function (text, status) { alert(text); });

3、form+js提交:

        form标签里的按钮一定要添加:type="button",后面和上面2中的一样;

4、服务器:

继承httpservlet的java类中的dopost()方法:

request.setCharacterEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");

System.out.println("连接到服务器"+request.getParameter("name")+","+request.getParameter("password"));

//添加数据库操作数据库

PrintWriter out = response.getWriter();

out.write("link server success!");//返回数据给页面

out.close();

前端form标签发送post请求,服务器用Java类继承HttpServlet,并返回数据

标签:

原文地址:http://www.cnblogs.com/shuishoulan/p/4762086.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!