Servlet之初识

时间:2023-03-09 08:05:29
Servlet之初识

doHeader 用于处理HEADER请求
doGet 用于处理GET请求,也可以自动的支持HEADER请求
doPost 用于处理POST请求
doPut 用于处理PUT请求
doDelete 用于处理DELETE请求

1 package org.caiduping.Servlet;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5
6 import javax.servlet.ServletException;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 public class Servlet extends HttpServlet {
12
13 /**
14 * Constructor of the object.
15 */
16 public Servlet() {
17 super();
18 }
19
20 /**
21 * Destruction of the servlet. <br>
22 */
23 public void destroy() {
24 super.destroy(); // Just puts "destroy" string in log
25 // Put your code here
26 }
27
28 /**
29 * The doGet method of the servlet. <br>
30 *
31 * This method is called when a form has its tag value method equals to get.
32 *
33 * @param request the request send by the client to the server
34 * @param response the response send by the server to the client
35 * @throws ServletException if an error occurred
36 * @throws IOException if an error occurred
37 */
38 public void doGet(HttpServletRequest request, HttpServletResponse response)
39 throws ServletException, IOException {
40
41 response.setContentType("text/html");
42 PrintWriter out = response.getWriter();
43 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
44 out.println("<HTML>");
45 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
46 out.println(" <BODY>");
47 out.print(" This is ");
48 out.print(this.getClass());
49 out.println(", using the GET method");
50 out.println(" </BODY>");
51 out.println("</HTML>");
52 out.flush();
53 out.close();
54 }
55
56 /**
57 * The doPost method of the servlet. <br>
58 *
59 * This method is called when a form has its tag value method equals to post.
60 *
61 * @param request the request send by the client to the server
62 * @param response the response send by the server to the client
63 * @throws ServletException if an error occurred
64 * @throws IOException if an error occurred
65 */
66 public void doPost(HttpServletRequest request, HttpServletResponse response)
67 throws ServletException, IOException {
68
69 response.setContentType("text/html");
70 PrintWriter out = response.getWriter();
71 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
72 out.println("<HTML>");
73 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
74 out.println(" <BODY>");
75 out.print(" This is ");
76 out.print(this.getClass());
77 out.println(", using the POST method");
78 out.println(" </BODY>");
79 out.println("</HTML>");
80 out.flush();
81 out.close();
82 }
83
84 /**
85 * Initialization of the servlet. <br>
86 *
87 * @throws ServletException if an error occurs
88 */
89 public void init() throws ServletException {
90 // Put your code here
91 }
92
93 }