响应编码

产生原因以及解决办法:

示例:

 package cn.yzu;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("张三");
response.getWriter().println("李四");
}
}

 运行截图:

请求编码

 POST请求:

GET请求(conf/server.xml):

示例:

index.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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">
</head>
<body>
<form action="AServlet" method="post">
<input type="text" value="张三" name="zs">
<input type="submit" value="post请求">
</form><hr>
<form action="AServlet" method="get">
<input type="text" value="李四" name="ls">
<input type="submit" value="get请求">
</form>
</body>
</html>

AServlet

 package cn.yzu;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//处理get请求
System.out.println(request.getParameter("ls")); //不处理
String name = request.getParameter("ls");
byte[] b = name.getBytes("iso-8859-1"); //回退
name = new String(b, "utf-8"); //重编
System.out.println(name); }
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//处理post请求
request.setCharacterEncoding("utf-8");
String username = request.getParameter("zs");
System.out.println(username);
}
}

页面:

输出结果:

URL编码:

URL编码解码示例:

 import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import org.junit.Test;
public class Demo {
@Test
public void fun1() throws UnsupportedEncodingException {
String name = "张三";
String s = URLEncoder.encode(name, "UTF-8");//编码
System.out.println(s);
s = URLDecoder.decode(s, "utf-8"); //解码
System.out.println(s);
}
}

结果:

最新文章

  1. ToolStrip控件在窗体没有焦点的情况下,需要单击二次才能够激发事件的解决办法
  2. AR播放器
  3. js控制手机号码中间用星号代替
  4. React Native 开发。
  5. [CODEVS2603]公路修建
  6. 【POJ1182】 食物链 (带权并查集)
  7. HDU 5933 ArcSoft&#39;s Office Rearrangement 【模拟】(2016年中国大学生程序设计竞赛(杭州))
  8. 【Win7激活工具2013版下载】适用于旗舰版、家庭高级版等所有版本32/64位 OEM激活
  9. 安卓中onBackPressed ()方法的使用
  10. ym——android源代码大放送(实战开发必备)
  11. jquery easyui的datagrid在初始化的时候会请求两次URL?
  12. PHP 合并有序数组
  13. Win10上启动UICrawler自动遍历时报 &quot;org.openqa.selenium.WebDriverException: An unknown server-side error occur red while processing the command. Original error: Could not sign with default certifi cate.&quot;
  14. CefSharp 封装自己的简单H5浏览器 详细配置
  15. Vue通过build打包后 打开index.html页面是空白的
  16. Calling async method synchronously
  17. NetBeans 打开项目中文乱码最简单的解决办法
  18. Gift动图分解小工具
  19. iOS进阶指南试读之UI篇
  20. slf4j+log4j2 pom配置

热门文章

  1. web开发架构设计
  2. C#有关日期的使用方法
  3. DB2 SQLCODE 大全
  4. Dynamic Invok Webservice
  5. C# 对象深度拷贝
  6. php简易灌水
  7. mac os 下打开FTP服务器
  8. Qt Creator 中关于调试器的设置
  9. IIS 发布mvc 403.14
  10. ssh配置免密码登录