1. 程式人生 > >servlet開發三種,get請求和post請求的區別

servlet開發三種,get請求和post請求的區別

開發servlet三種方法

1.實現servlet介面

2.繼承GenericServlet

3.繼承HttpServlet


需求如下  請使用實現介面的方式,來開發一個Servelt,要求該Servlet可以顯示hello,同時顯示當前時間

步驟:

1.建立一個WEB應用web1

2.在web1下建立WEB-INF/web.xml (從ROOTWEB-INF/web.xml 下複製)

3.在WEB-INF下建立classes  lib目錄

4.在WEB-INF/classes/建立一個servlet.java

Servlet  API資料下載

Destroy()

getServletConfig()

package com.hsp;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
 
class Myfirestservlet  implements Servlet
{
//該函式用於初始化Servlet,就是把該servlet裝載到內容中
//該函式只會被呼叫一次
public void init(ServletConfig  config)
throws  ServletException{
        }
//得到ServletConfig物件
public ServletConfig getServletConfig(){
return null;
}
//該函該數是服務函式,我們的業務邏輯程式碼就是寫在這裡
//該函式每次都會被呼叫
public void  service(ServletRequest req,
ServletResponse  res)
 throws  ServletException ,
        java.io.IOException{
        }
//該函式得到servlet配置資訊
public java.lang.String   getServletInfo(){
return null;
}
//銷燬該servlet,從記憶體
public void destroy(){
}
}

法二:

繼承GenericServlet的方法開發servlet(一般不用)

public class HttpExample extends GenericServlet{
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
arg1.getWriter().println("hello world");
}
}


法三:

繼承httpServlet的方法來開發Servlet

(1)在軟體公司90%都是通過該方法開發

(2)舉例說明,還是顯示hello world當前日期

package com.xiyou.geng;
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class MyGenricServlet
 */
 
@WebServlet("/MyGenricServlet")
public class HttpExample extends HttpServlet {
private static final long serialVersionUID = 1L;
    public HttpExample() {
        super();
         }
//在httpServlet中,設計者對POST提交和GET提交分別處理
    //回憶<from action="提交給?" method="post|get"/>預設是get
    //doget  dopost最終也呼叫了service方法
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     response.getWriter().println("i am doget");
    }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("i am dopost"+request.getParameter("username"));//username是在login.html中
}
 
}
 


還有一個login.html檔案

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/web_01/http"  method="post">
u:<input type="text" name="username">
<input type="submit" value="login"/>
</body>
</html>


通過HttpServlet去開發Servlet,需要重寫doGet,doPost方法。這是目前用的最多的一種方法

表單提交資料get請求和post請求的區別

(1)從安全性看get 小於post,get提交的資料會在瀏覽器的位址列顯示

(2)從提交的內容大小看get小於post,get提交的資料不能大於2K,而破殺他提交的資料理論上不受限制,但是實際程式設計中建議不要大於64K

(3)從請求響應速度看:get 大於post,get要求伺服器立即處理請求,而post請求可能形成一個佇列請求