通过ajax的post方法向服务器发送参数
来源:互联网 发布:servlet接收ajax json 编辑:程序博客网 时间:2024/05/23 18:15
package liulixin;import java.io.*;import java.text.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;public class getname extends HttpServlet {public void init(ServletConfig config) throws ServletException {super.init(config);}public void destroy() {}public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {String name = null;System.out.println("arrived at doPost");PrintWriter out = response.getWriter();if (request.getParameter("txtname") != null) {name = request.getParameter("txtname");System.out.println(name);} else {name = "";}out.println("You have successfully made Ajax Call:" + name);}}
<html><head><title>JSP and Servlet using AJAX</title><script type="text/javascript">function getXMLObject() //XML OBJECT{var xmlHttp = false;try {xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); // For Old Microsoft Browsers} catch (e) {try {xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); // For Microsoft IE 6.0+} catch (e2) {xmlHttp = false; // No Browser accepts the XMLHTTP Object then false}}if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {xmlHttp = new XMLHttpRequest(); //For Mozilla, Opera Browsers}return xmlHttp; // Mandatory Statement returning the ajax object created}var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax objectfunction ajaxFunction() {alert("insiede Function");if (xmlhttp) {alert("inside if");var txtname = document.getElementById("txtname");xmlhttp.open("POST", "Messages", true); //getname will be the servlet namexmlhttp.onreadystatechange = handleServerResponse;xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');xmlhttp.send("txtname=" + txtname.value); //Posting txtname to Servlet}alert("after if");}function handleServerResponse() {alert("inside handleServerResponse");if (xmlhttp.readyState == 4) {if (xmlhttp.status == 200) {document.getElementById("message").innerHTML = xmlhttp.responseText; //Update the HTML Form element } else {alert("Error during AJAX call. Please try again");}}}</script><body><form name="myForm" method="POST" action=""><table><tr><td>Enter Name</td><td><input type="text" name="txtname" id="txtname" /></td></tr><tr><td colspan="2"><input type="button" value="Submit"onclick="ajaxFunction();" /></td></tr></table></form><div id="message"></div></body></head></html>
<servlet> <servlet-name>Messages</servlet-name> <servlet-class>liulixin.getname</servlet-class> </servlet> <servlet-mapping> <servlet-name>Messages</servlet-name> <url-pattern>/Messages</url-pattern> </servlet-mapping>
原文:http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=222
源代码:http://pan.baidu.com/share/link?shareid=486159&uk=3878681452
- 通过ajax的post方法向服务器发送参数
- 通过ajax引擎对象向服务器发送数据的2中方式:get、post
- echarts通过ajax向服务器发送post请求,servlet从数据库读取数据并返回前端
- 【AJAX】Ajax-向服务器发送请求何时用Get/Post
- 向指定 URL 发送GET,POST方法的请求(传入指定url和参数,服务器返回字符串数据)
- Post、Get方法向服务器传递参数
- 通过HttpWebRequest分别向服务器发送GET或POST请求
- Android客户端通过GET和POST向服务器发送数据
- Ajax通过post方法向Servlet提交信息
- 向指定服务器URL 发送POST方法的请求并用JSON表示
- Ajax通过POST方法发送中文信息乱码解决
- Ajax用POST方法传参数,参数提交不到服务器
- iOS 向服务器POST参数
- AJAX - 向服务器发送请求
- AJAX - 向服务器发送请求
- AJAX - 向服务器发送请求
- Ajax向服务器发送请求
- ajax向服务器发送消息
- 在Java中实现浮点数的精确计算
- JAVA实现单向链表反转
- MVC/Hibernate/Sturts and 2 特点
- [LeetCode] Recover Binary Search Tree
- ACM菜鸟-DSAL编程进阶笔记【1】
- 通过ajax的post方法向服务器发送参数
- 【OpenCV】访问Mat图像中每个像素的值
- openCv学习札记(二)—cv:Mat学习
- C++程序设计-第15周 数据结构扩展与GUI开发体验
- <c:if>标签中的test可以比较两个el表达式中的值
- servlet中web.xml的配置
- 熬之滴水穿石:JAVA的世界(9)
- 读取容器配置参数-context-param 和init-param
- WinForm开发框架--动态读取DLL模式