⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guestbook.java

📁 This book code procedure according to the chapter arrangement, the 1st chapter of example under "1"
💻 JAVA
字号:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class GuestBook extends HttpServlet{
    public void doPost(HttpServletRequest request,HttpServletResponse response)
    throws ServletException, IOException
    {
      //首先设置响应的content type 头部
      response.setContentType("text/html");
      //得到响应的PrinterWriter以返回文本给客户端。
      PrintWriter toClient = response.getWriter();
      int isbad=0;
      String msg="";
      String sucmsg="Error";
      String name=request.getParameter("name");
      String note=request.getParameter("note");
      if("".equals(name)){
         isbad=1;
         msg="<li>姓名不能为空</li>";         
      }
      if("".equals(note)){
         isbad=1;
         msg=msg+"<li>留言不能为空</li>";
      }
      if(isbad==0){
         String mystr="";
         try{
           //打开一个文件写入留言。
           String resultDir="c:";
           //String resultDir=System.getProperty("user.dir");
           FileWriter resultFile = new FileWriter(resultDir+
               System.getProperty("file.separator")+ "guestbook.txt",true);
           PrintWriter toFile = new PrintWriter(resultFile);
           toFile.println("<hr>");
           toFile.println("Name:"+name+"<br>");
           toFile.println("Content:<p>"+note+"</p>");
           toFile.println("<hr>");
           sucmsg="Thank you for participating";
           resultFile.close();
         }catch(IOException ie){
           toClient.println("A problem occured.Please try again.");
         }catch(Exception e){
         }
      }
      //向留言用户返回信息
      toClient.println("<html>");
      toClient.println("<title>Thank you!</title>");
      toClient.println("<p>"+ sucmsg +"</p>");
      toClient.println("<p>"+ msg + "</p>");
      toClient.println("<p><a href='javascript:history.back(1);'>return</a></p>");
      toClient.println("<html>");
      toClient.close();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -