ajax jsp请求乱码
2016-06-15 00:00
381 查看
jsp页面(Ajax)
servlet处理页
后台输出:
doPost HELLO AjAX! QQ群:Java技术之信仰 557254530
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'ajax1.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="ajax_createXMLHttpRequest/createXMLHttpRequest.js"></script> <script type="text/javascript"> //文档加载完毕后执行 window.onload = function() { var btn = document.getElementById("btn"); //给按钮的点击事件注册监听 btn.onclick = function() { /* * ajax四步操作,得到服务器的响应 * 把响应结果显示到h1元素中 */ //1. 得到异步对象 var xmlHttp = createXMLHttpRequest(); /* 2. 打开与服务器的连接 * 指定请求方式 * 指定请求的URL * 指定是否为异步请求 */ /************修改open方法,指定请求方式为POST**************/ xmlHttp.open("POST", "<c:url value='/Servlet'/>", true); /************设置请求头:Content-Type************/ xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); /* * 3. 发送请求 * GET请求没有请求体,但也要给出null,不然FireFox可能会不能发送! */ /**********发送时指定请求体***********/ xmlHttp.send("username=<span style="font-size:18px;">QQ群:Java技术之信仰 557254530</span>&password=123"); /* * 4. 给异步对象的onreadystatechange事件注册监听器 * */ //当xmlHttp的状态发生变化时执行 xmlHttp.onreadystatechange = function() { // 双重判断:xmlHttp的状态为4(服务器响应结束),以及服务器响应的状态码为200(响应成功) if(xmlHttp.readyState == 4 && xmlHttp.status == 200) { // 获取服务器的响应结束 var text = xmlHttp.responseText; // 获取h1元素 var h1 = document.getElementById("h1"); h1.innerHTML = text; } }; }; }; </script> </head> <body> <% request.setCharacterEncoding("utf-8"); %> <button id="btn">点击这里</button> <h1 id="h1"></h1> </body> </html>
servlet处理页
package com.ajax.data; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Servlet * 响应text文本:方式POST * 解决中文乱码 * */ @WebServlet("/Servlet") public class Servlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); String name = request.getParameter("username"); //解决中文乱码 String strNmae = new String(name.getBytes("ISO-8859-1"),"UTF-8"); // byte[] n = name.getBytes("ISO-8859-1"); // String strNmae = new String(n,"UTF-8"); System.out.println("doPost HELLO AjAX!"+strNmae); response.getWriter().print("doPost hello ajax!!!"+strNmae); } }
后台输出:
doPost HELLO AjAX! QQ群:Java技术之信仰 557254530
相关文章推荐
- JSTL标签库:Remove标签Catch标签
- JSON 使用Gson转换成Json的字符串
- JSTL标签库:out标签
- JSP 九大内置对象
- JSTL标签库:if标签
- JSON 和 List、Map集合结合使用
- JSON 在javascript中使用json
- javascript 简单的选项卡
- javascript:系统学习知识点——定时器(看注释)
- JS函数定义特性摘录
- JS请求时间过长的解决思路
- String to Integer (atoi)
- ajax使用之验证用户名是否存在
- 不需要更多JS框架了
- js的 image 属性 和一个预加载模块
- js获取当前时间
- BZOJ_1012_[JSOI2008]最大数maxnumber_暴力?(线段树)
- json字符串和字典类型的相互转换
- Eclipse去除js(JavaScript)验证错误
- JavaScript闭包--简介