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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Servlet,可以通过使用Java的文件操作API读写文件。 首先,要读取文件内容,可以使用FileInputStream或BufferedReader类。FileInputStream类用于从文件读取字节,而BufferedReader类可用于按行读取文件。 例如,要读取文本文件的内容,可以使用如下代码片段: ``` try { String filePath = "/path/to/file.txt"; // 文件路径 File file = new File(filePath); BufferedReader reader = new BufferedReader(new FileReader(file)); String line; StringBuilder fileContent = new StringBuilder(); while ((line = reader.readLine()) != null) { fileContent.append(line); } reader.close(); // 对读取到的文件内容进行处理 String content = fileContent.toString(); // ... } catch (IOException ex) { ex.printStackTrace(); } ``` 接下来,如果需要在Servlet入文件,可以使用FileOutputStream或PrintWriter类。FileOutputStream类用于将字节入文件,而PrintWriter类可用于入文本文件。 例如,要入文本文件,可以使用如下代码片段: ``` try { String filePath = "/path/to/file.txt"; // 文件路径 File file = new File(filePath); PrintWriter writer = new PrintWriter(new FileWriter(file)); writer.println("Hello, World!"); writer.println("This is a sample text."); writer.close(); // 文件入成功 } catch (IOException ex) { ex.printStackTrace(); } ``` 需要注意的是,在使用这些类进行文件读写操作时,应该捕获和处理可能产生的IOException异常,以便在发生错误时能够进行适当的处理和报告。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值