本帖最后由 lyg2013 于 2013-4-11 17:25 编辑
- package web;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class SaveCookie extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- Cookie cookie =new Cookie("username","王武");//换成中文后就错了,怎么改变编码呢?
- Cookie cookie2 =new Cookie("password","test");
- cookie2.setMaxAge(40);
- response.addCookie(cookie);
- response.addCookie(cookie2);
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- out.println("<h1>add cookie success<h2>");
- out.close();
- }
- }
- 如何中文也能被保存?
复制代码 |