Servlet----------在使用doGet()和doPost()是如何處理中文亂碼
阿新 • • 發佈:2017-07-19
tps .com 添加 java img print imp 跳轉 中文亂碼
如果在登錄界面跳轉到時servlet中使用doGet()方法顯示中文出現亂碼時,可以添加這兩行代碼來解決,但這不是唯一的方法
request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK")
如我的
1 package cn.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 importjavax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class Login extends HttpServlet { 12 public void doGet(HttpServletRequest request, HttpServletResponse response) 13 throws ServletException, IOException {14 request.setCharacterEncoding("GBK");//解決亂碼 15 response.setContentType("text/html;charset=GBK");//解決亂碼 16 17 String name=request.getParameter("login"); 18 String password=request.getParameter("password"); 19 response.setCharacterEncoding("GBK"); 20 PrintWriter out = response.getWriter();21 out.println("歡迎-- "+name+" --登錄!"+"</br>"); 22 out.print("你的登錄密碼是:"+password); 23 } 24 public void doPost(HttpServletRequest request, HttpServletResponse response) 25 throws ServletException, IOException { 26 this.doGet(request, response); 27 } 28 }
Servlet----------在使用doGet()和doPost()是如何處理中文亂碼