解决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)