JSP学习笔记(8)_Servlet

时间:2022-06-16 21:03:27

JSP学习笔记(8)_Servlet(1)


1.结构体系

HttpServlet <---(子类)--- GenericServlet(抽象类) ---(实现)---> (1)(2)(3)

(1)Servlet            (interfance) 

(2)ServletConfig (interfance) 

(3)Serializable     (interfance) 序列化接口

2.特点

跨平台,可移植性

3.效率

比JSP效率高

4.简单实现

public class Test01 implements Servlet{

public void destroy() {
// TODO Auto-generated method stub
System.out.println("killed!!!");
}

public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}

public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}

public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("出现了!!");
}

public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
// TODO Auto-generated method stub
//System.out.println("服务中...");
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
String username = request.getParameter("username");
PrintWriter out = response.getWriter();
out.println("<html><head></head><body>");
out.println("<h1>hello "+username+"!</h1>");
out.println("</body></html>");
}

}
方法以及作用:
public void destroy()// 当服务器内存不足而导致servlet对象被调度或者关闭服务器时,serlet对象销毁前执行

public ServletConfig getServletConfig()// 用于获取config对象
public String getServletInfo()// 用于获取servlet的说明信息
public void init(ServletConfig config) throws ServletException// 用于执行对象创建后{构造器执行后}的初始化操作,在整个Servlet对象的生命周期中运行且只运行一次
public void service(ServletRequest req, ServletResponse res)throws ServletException, IOException// 用于对外提供服务,响应客户请求[URL],该方法以多线程的方式对外提供服务,服务完毕则常驻内存