servlet中读写中文的办法

来源:互联网 发布:javac java heap space 编辑:程序博客网 时间:2024/06/05 04:32
 

网页部分:

<html>
<body bgcolor="pink">
 <center>
 <form method="post" action=http://127.0.0.1:8000/Gift/Gift>
 <table>
  <tr>
   <td>Enter Login Name:</td>
   <td><input type="text" name="loginid"></td>
  </tr>
  <tr>
   <td>Enter password:</td>
   <td><input type="password" name="passwd"></td>
  </tr>
 </table>
 <input type="submit" value="submit">
 </form>
 </center>
</body>
</html>

Servlet部分:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class GiftServlet extends HttpServlet
{
 public void service(HttpServletRequest req,HttpServletResponse res) throws IOException
 {
  boolean cookieFound = false;
  Cookie myCookie = null;
  Cookie[] cookieset = req.getCookies();
  res.setContentType("text/html;charset=gb2312");
  PrintWriter pw = res.getWriter();
  pw.println("<html>");
  pw.println("<body>");
  pw.println("这里是河北软件职业技术学院");
  pw.println("<br>");
  req.setCharacterEncoding("gb2312");
  pw.println("你好:" + req.getParameter("loginid"));
  if(cookieset != null)
  {
   for(int i = 0;i < cookieset.length;i++)
   {
    if(cookieset[i].getName().equals("logincount"))
    { 
     myCookie = cookieset[i];
     cookieFound = true;
    }
   }
  }
  if(cookieFound)
  { 
   int temp = Integer.parseInt(myCookie.getValue());
   temp ++;
   pw.println("Congratulations!!!!!!!!, a gift is waiting you");
   pw.println("The number of times you have logged in is " + String.valueOf(temp));
   myCookie.setValue(String.valueOf(temp));
   int age = 60*60*24*30;
   myCookie.setMaxAge(age);
   res.addCookie(myCookie);
   cookieFound = false;
  }
  else
  {
   int temp = 1;
   pw.println("This is the first time you have logged on to the server");
   myCookie = new Cookie("logincount",String.valueOf(temp));
   int age = 60*60*24;
   myCookie.setMaxAge(age);
   res.addCookie(myCookie);
  }
  pw.println("</body>");
  pw.println("</html>");
 }
}

原创粉丝点击