1. 程式人生 > >myeclipse實現Servlet例項(3) 通過繼承HttpServlet介面實現

myeclipse實現Servlet例項(3) 通過繼承HttpServlet介面實現

(1) 在軟體公司 90%都是通過該方法開發.
//在HttpServlet 中,設計者對post 提交和 get提交分別處理 
 //回憶 <form action="提交給?" method="post|get"/>,預設是get
(2)小結 get 提交 和 post的提交的區別 
① 從安全看 get<post 因為get 會把提交的資訊顯示到位址列 (提交密碼時建議使用post)
② 從提交內容看 get<post get 一般不要大於2k, post理論上無限制,但是在實際開發中,建議不要大於64k 
③ 從速度看 get>post 
④ Get可以保留uri中的引數,利於收藏 
package com.tsinghua;


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;


public class HelloHttp extends HttpServlet {


/* Constructor of the object. */
public HelloHttp() {
super();
}


/* Destruction of the servlet. <br>*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


/**
* The doGet method of the servlet. <br>
* This method is called when a form has its tag value method equals to get.
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();

}


/**
* The doPost method of the servlet. <br>
* This method is called when a form has its tag value method equals to post.
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
try{
PrintWriter out = response.getWriter();
out.println("Hello,Liu.http");
}
catch(Exception e){
e.printStackTrace();
}
}


/*Initialization of the servlet. <br>*/
public void init() throws ServletException {
// Put your code here
}


}