Java Web解决解析乱码和响应乱码
package cn.edu.aynu.rjxy.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AServlet extends HttpServlet { /*get方式请求时调用的方法 * 什么是get请求? * 在地址栏输入用户名和密码、超链接、直接在method里面写上get * get没有请求体 无法设置请求体 * 首先需要获取到username和psw * 然后把字符串回退为字节数组 * 使用ISO-8859-1编码方式解码 * 把字节数组重新组装成字符串,使用UTF-8编码方式 * */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); //根据参数名获取参数值 String username = request.getParameter("username"); String psw = request.getParameter("psw"); //使用ISO-8859-1把username的值回退成字节数组 byte[] b = username.getBytes("ISO-8859-1"); //使用UTF-8把字节数组重新解码 username = new String(b,"UTF-8"); //响应输出流的编码方式是UTF-8 客户端、浏览器解码方式也为UTF-8 response.setContentType("text/html;charset=utf-8"); //输入到界面上 response.getWriter().print("username="+username); response.getWriter().print("psw="+psw); //输入到控制台上 System.out.println("username="+username); System.out.println("psw="+psw); } //post方式请求调用的方法 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /*post请求的解码处理 乱码出现的根源是:编码方式和解码方式不一致。 * tomcat服务器默认的解码方式是ISO-8859-1不支持中文。由于post请求 * 有请求体,请求参数是放在请求体中的,设置请求体的解码方式,需要调用 * 方法request.setCharacterEncoding("UTF-8");支持中文 */ request.setCharacterEncoding("UTF-8"); //根据参数名获取参数值 String username = request.getParameter("username"); String psw = request.getParameter("psw"); //根据参数名获取多个参数值
String[] hobby = request.getParameterValues("hobby"); //数组的遍历
for (int i = 0; i < hobby.length; i++) { System.out.println(hobby[i]); } //响应输出流的编码方式是UTF-8 客户端、浏览器解码方式也为UTF-8 response.setContentType("text/html;charset=utf-8"); //输入到界面上 response.getWriter().print("username="+username); response.getWriter().print("psw="+psw); //输入到控制台上 System.out.println("username="+username); System.out.println("psw="+psw); } }
AServlet.java
a.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 'a.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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action = "/day9/AServlet" method = "post"> 用户名:<input type = "text" name = "username"/><br/> 密 码:<input type = "password" name = "psw"/><br/> 爱好:<input type = "checkbox" name = "hobby" value ="Swimming">游泳 <input type = "checkbox" name = "hobby" value ="Sing">唱歌 <input type = "checkbox" name = "hobby" value ="Run">跑步 <input type = "checkbox" name = "hobby" value ="Read">阅读<br> <input type = "submit" value="提交"> </form> <hr> <a href="/day9/AServlet?username=李四&psw=123">get方式请求</a> </body> </html>
posted on 2016-09-26 18:25 kang_ya_ping 阅读(643) 评论(0) 编辑 收藏 举报