2017/5/8

Servlet 亂碼紀錄

還沒時間做整理因此把重點資料放在這
http://bit.ly/2pr6BoA
http://bit.ly/2qJdHcc
http://bit.ly/2pmQI1Q
從網址列下請求,在doGet回應,不會亂碼的寫法:
//Servlet 在網址傳參數不會亂碼的寫法
package newpackage;

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 NewServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8"); 
        PrintWriter out = response.getWriter();
        String UserID=new String(request.getParameter("UserID").getBytes("UTF-8"));
        out.print(UserID);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }
}

請求這樣下
http://localhost:8101/WebApplicationTest/NewServlet?UserID=Xanxus

沒有留言:

張貼留言