1、Servlet简介

 1 package bilibili.servlet.study;
 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 /**
12  * Servlet implementation class ServletDemo1
13  */
14 public class ServletDemo1 extends HttpServlet {
15     private static final long serialVersionUID = 1L;
16     /**
17      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
18      *      response)
19      */
20     protected void doGet(HttpServletRequest request, HttpServletResponse response)
21             throws ServletException, IOException {
22         // TODO Auto-generated method stub
23         response.setContentType("text/html");
24         response.setCharacterEncoding("UTF-8");
25         PrintWriter out = response.getWriter();
26         // println中的""需要用/转义下
27         out.println(
28                 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Strict//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">");
29         out.println("<html>");
30         out.println("<head>");
31         out.println("<title>ServletDemo1</title>");
32         out.println("</head>");
33         out.println("<body>");
34         out.println("<h1>");
35         out.println(this.getClass());
36         out.println("</h1>");
37         out.println("<body>");
38         out.println("</html>");
39         out.close();
40         out.flush();
41     }
42     /**
43      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
44      *      response)
45      */
46     protected void doPost(HttpServletRequest request, HttpServletResponse response)
47             throws ServletException, IOException {
48         // TODO Auto-generated method stub
49         doGet(request, response);
50     }
51 }

doGet()和doPost()方法的区别:

doGet():1.常用来查询信息 2.提交的查询信息显示在浏览器的地址栏中 3.以get方式提交的网址有长度限制

doPost():1提交大量信息 2.以post方式提交数据是,数据信息不会显示在浏览器的地址栏中

posted @ 2015-12-10 16:56  Akke  阅读(197)  评论(0编辑  收藏  举报