package cn.dragon.servlet; //下面是匯入相應的包 import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 這是第一個Servlet的例子 * @author cn.dragon */ public class ServletDemoFirst extends HttpServlet { //用於處理用戶端發送的GET請求 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=GB2312"); //這條語句指明了向用戶端發送的內容格式和採用的字元編碼. PrintWriter out = response.getWriter(); out.println(" 您好!"); //利用PrintWriter對象的方法將資料發送給用戶端 out.close(); } //用於處理用戶端發送的POST請求 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); //這條語句的作用是,當用戶端發送POST請求時,調用doGet()方法進行處理 } } |