servlet學習(一)書寫步驟掌握
阿新 • • 發佈:2018-11-03
每寫一個servlet的時候,先寫註釋,後寫程式碼,寫註釋的步驟按照如下要求:
//1.設定請求編碼格式
req.setCharacterEncoding("utf-8");
//2.設定響應編碼格式
resp.setContentType("text/html;charset=utf-8");
//3.獲取請求資料
//4.處理請求資料(資料庫操作:MVC思想:dao+servlet+service+pojo)
//4.1建立業務邏輯層物件
//4.2處理,獲得返回結果
//5.應處理結果
//5.1直接響應
//5.2請求轉發
//5.3重定向
例項程式碼:
package com.linyu.mycode.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Servlet1 */ public class Servlet1 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //設定請求編碼格式 req.setCharacterEncoding("utf-8"); //設定響應編碼格式 resp.setContentType("text/html;charset=utf-8"); //獲取請求資料 //處理請求資料 //建立業務邏輯層物件 //處理,獲得返回結果 //響應處理結果 //直接響應 //請求轉發 //重定向 } }