来老师的专栏

学海无涯,一天天好起来...

servlet中读写中文的办法

 

网页部分:

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

阅读更多
个人分类: J2EE
上一篇安装软件时,反复要求重新启动(如安装SQL或VS2003)
下一篇jsp内建对象及其方法
想对作者说点什么? 我来说一句

如何用csharp读写INI文件的办法

2010年02月10日 51KB 下载

没有更多推荐了,返回首页

关闭
关闭