今天就跟大家聊聊有关怎么在ajax中利用Servlet处理数据并显示,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。一、写前台jsp页面index.jsp<%@ pag
今天就跟大家聊聊有关怎么在ajax中利用Servlet处理数据并显示,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
一、写前台jsp页面index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My jsP 'index.jsp' starting page</title> <script type="text/javascript"> var xmlHttp; function createXMLHttpRequest(){ //1建立XmlHttpRequest对象 if(window.ActiveXObject){ try{ xmlHttp = new ActiveXObject("Microsoft.XMLHttp"); }catch(e){ alert("Error!!!"); } }else{ xmlHttp = new XMLHttpRequest(); } } function showMes(){ //2设置回调函数 if(xmlHttp.readyState==4){ //数据接收完成并可以使用 if(xmlHttp.status==200){ //http状态OK //5、在回调函数中针对不同响应状态进行处理 document.getElementById("sp").innerHTML = xmlHttp.responseText; //服务器的响应内容 }else{ alert("出错:"+xmlHttp.statusText); //HTTP状态码对应的文本 } } } function postMes(){ createXMLHttpRequest(); var txt = document.getElementById("txt").value; var url = "servlet/AjaxServlet"; var params = "username="+txt; // alert(params); xmlHttp.open("POST",url,true); xmlHttp.setRequestHeader("Content-Type","application/x-www-fORM-urlencoded;charset=UTF-8"); xmlHttp.send(params); xmlHttp.onreadystatechange = showMes; } </script> </head> <body> <input type="text" id="txt"/> <input type="button" value="query" onclick="postMes()" /> <span id="sp"></span> </body></html>
二、写后台Servlet加random随机数,关键代码如下:
public void doGet(httpservletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //用utf-8转换获得传输过来的码 response.setContentType("text/html"); PrintWriter out = response.getWriter(); String txt = request.getParameter("txt");// String tx = new String(txt.getBytes("iso-8859"),"utf-8"); out.print("txt="+txt+Math.random()); out.flush(); out.close();}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String username = request.getParameter("username");// String txt = new String(username.getBytes("ISO-8859-1"),"UTF-8"); String txt = new String(username); out.print("txt="+txt+":"+Math.random()); out.flush(); out.close();}
看完上述内容,你们对怎么在AJAX中利用Servlet处理数据并显示有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网精选频道,感谢大家的支持。
--结束END--
本文标题: 怎么在AJAX中利用Servlet处理数据并显示
本文链接: https://www.lsjlt.com/news/252373.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
一口价域名售卖能注册吗?域名是网站的标识,简短且易于记忆,为在线用户提供了访问我们网站的简单路径。一口价是在域名交易中一种常见的模式,而这种通常是针对已经被注册的域名转售给其他人的一种方式。
一口价域名买卖的过程通常包括以下几个步骤:
1.寻找:买家需要在域名售卖平台上找到心仪的一口价域名。平台通常会为每个可售的域名提供详细的描述,包括价格、年龄、流
443px" 443px) https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294.jpg https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294-768x413.jpg 域名售卖 域名一口价售卖 游戏音频 赋值/切片 框架优势 评估指南 项目规模
0