解决Post和Get乱码问题

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 {}

}

 

阅读更多
个人分类: javaWeb
上一篇Power Designer使用技巧
下一篇代码优化篇
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭