1. 程式人生 > >解決客戶端向服務器寫中文時亂碼問題

解決客戶端向服務器寫中文時亂碼問題

ade prot 內容 tty protected wid extend odi inf

package com.hdh.content;

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 TextServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //設置response的查詢碼表 //response.setCharacterEncoding("Utf-8"); //告知客戶端我的編碼 並使用相同的編碼進行解碼 //response.setHeader("Content-Type","text/html;charset=UTF-8");

//設置相應內容的類型為"UTF-8" response.setContentType("text/html;charset=UTF-8"); PrintWriter writer = response.getWriter(); writer.write("中國"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
技術分享圖片

通過設置response的查詢碼表,並且告知客戶端我的編碼(utf-8)並使用相同的編碼進行解碼來解決向服務器寫入中文時亂碼現象



解決客戶端向服務器寫中文時亂碼問題