关闭

解决Post和Get乱码问题

301人阅读 评论(0) 收藏 举报
分类:

package com.clg.demo;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DemoServlet extends HttpServlet {

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  test1(request, response);
 }
 //post提交方式
 private void test1(HttpServletRequest request, HttpServletResponse response) {
  request.setCharacterEncoding("UTF-8");
  String username = request.getParameter("username");
  System.out.println(username);
 }
 //get提交方式
 private void test2(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
  String username = request.getParameter("username");
  username=new String(username.getBytes("iso8859-1"),"UTF-8");
  System.out.println(username);
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {}

}

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:107941次
    • 积分:1847
    • 等级:
    • 排名:千里之外
    • 原创:84篇
    • 转载:0篇
    • 译文:0篇
    • 评论:29条
    文章分类
    最新评论