码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet

时间:2017-06-27 19:52:17      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:char   exce   cep   简单   public   jsp   charset   xtend   import   

  自己写了一个很简单的servlet。method使用的post。一个简单的登陆页面,在login.jsp页面输入”username“和"password"就会自动再将用户名和密码打印到另外一个页面上显示。

  下面是代码:

  

package taobao.service;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        System.out.println(username);
//        response.setCharacterEncoding("utf-8");
        response.getWriter().println("登陆用户名:     "+username);
        response.getWriter().println("登陆密码 :   "+password);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

  在控制台打印可以正常显示的代码,在页面上却显示乱码。这说明request使用“UTF-8”来解码是正确的。

  JSP页面的编码

  

   contentType="text/html; charset=UTF-8"//服务器发送给客户端时的内容编码
    pageEncoding="UTF-8"//jsp文件本身的编码

  按道理response使用一下代码是OK的。但是不管使用

 

  

response.setCharacterEncoding("utf-8");

还是

  

response.setContentType("text/html;charset=utf-8");

页面显示依旧是乱码。抓耳挠腮了半天,脑袋都开始发热了。

最后突然发现了创建servlet的时候doget方法里带的第一行代码。

response.getWriter().append("Served at: ").append(request.getContextPath());

哇,真的是坑爹啊。折腾了我一下午,各种百度各种查,都要怀疑人生了。

原来只要把这行代码放到

response.setContentType("text/html;charset=utf-8");

后面就可以了。

  总结:要让浏览器最先知道是使用何种编码来解码。否则的话就不管用了。

  延伸:JSP页面中的pageEncoding和contentType两种属性的区别

  1、pageEncoding="UTF-8"的作用是设置JSP编译成Servlet时使用的编码。就是告诉JSP编译器在将JSP编译成servlet时使用的编码。 

  2、contentType="text/html;charset=UTF-8"的作用是指定对服务器响应进行重新编码的编码。 在不使用response.setCharacterEncoding方法时,用该参数指定对服务器响应进         行重新编码的编码。

  3、request.setCharacterEncoding("UTF-8")的作用是设置对客户端请求进行重新编码的编码。

       该方法用来指定对浏览器发送来的数据进行重新编码(或者称为解码)时,使用的编码。只对post方法有用。

  4、response.setCharacterEncoding("UTF-8")的作用是指定对服务器响应进行重新编码的编码。 
      服务器在将数据发送到浏览器前,对数据进行重新编码时,使用的就是该编码。

Servlet

标签:char   exce   cep   简单   public   jsp   charset   xtend   import   

原文地址:http://www.cnblogs.com/tomatozero/p/7086502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!