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
}


}

posted on 2015-04-23 20:24  anyuan9  阅读(140)  评论(0编辑  收藏  举报

导航