关闭

servlet中读写中文的办法

标签: servletimporthtmlnullserviceaction
868人阅读 评论(0) 收藏 举报
分类:
 

网页部分:

<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>");
 }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:564791次
    • 积分:9094
    • 等级:
    • 排名:第2149名
    • 原创:189篇
    • 转载:85篇
    • 译文:0篇
    • 评论:850条
    最新评论