- 什么是Servlet?
① Servlet就是JAVA 类
② Servlet是一个继承HttpServlet类的类
③ 这个在服务器端运行,用以处理客户端的请求 - Servlet相关包的介绍
--javax.servlet.* :存放与HTTP 协议无关的一般性Servlet 类;
--javax.servlet.http.* :除了继承javax.servlet.* 之外,并且还增加与HTTP协议有关的功能。
(注意:大家有必要学习一下HTTP协议,因为WEB开发都会涉及到)
所有的Servlet 都必须实现javax.servlet.Servlet 接口(Interface)。
若Servlet程序和HTTP 协议无关,那么必须继承javax.servlet.GenericServlet类;
若Servlet程序和HTTP 协议有关,那么必须继承javax.servlet.http.HttpServlet 类。
--HttpServlet :提供了一个抽象类用来创建Http Servlet。
public void doGet()方法:用来处理客户端发出的 GET 请求
public void doPost()方法:用来处理 POST请求
还有几个方法大家自己去查阅API帮助文件
--javax.servlet包的接口:
ServletConfig接口:在初始化的过程中由Servlet容器使用
ServletContext接口:定义Servlet用于获取来自其容器的信息的方法
ServletRequest接口:向服务器请求信息
ServletResponse接口:响应客户端请求
Filter接口:
--javax.servlet包的类:
ServletInputStream类:用于从客户端读取二进制数据
ServletOutputStream类:用于将二进制数据发送到客户端
--javax.servlet.http包的接口:
HttpServletRequest接口:提供Http请求信息
HttpServletResponse接口:提供Http响应 - Servlet生命周期
--Servlet生命周期就是指创建Servlet实例后,存在的时间以及何时销毁的整个过程.
--Servlet生命周期有三个方法
init()方法:
service()方法:Dispatches client requests to the protectedservice
method
destroy()方法:Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
--Servlet生命周期的各个阶段
----实例化:Servlet容器创建Servlet实例
----初始化:调用init()方法
----服务:如果有请求,调用service()方法
----销毁:销毁实例前调用destroy()方法
----垃圾收集:销毁实例 - Servlet的基本结构
package web01; 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 FristServlet extends HttpServlet { /** * Constructor of the object. */ public FristServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { System.out.println("销毁了servlet!"); } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response);//调用doPost } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ 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("<br/>"); out.println("<hr/>"); out.println("<h1>Hello world!</h1>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here System.out.println("我是做初始化工作的!"); } }
5. Servlet的部署
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <servlet> <servlet-name>FristServlet</servlet-name> <servlet-class>web01.FristServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FristServlet</servlet-name> <url-pattern>/servlet/FristServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
【注意】
① 上面的两个<servlet-name>必须相同
② <servlet-class>后面指在对应的类上面. 技巧:你可以直接在你的servlet类中复制过来,这样可以避免出错!
③ <url-pattern> 必须是/servlet 再加servlet名字.大家现在就这么记.6.Servlet实例演示
package mybao; 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; import bean.User; public class OrderServlet extends HttpServlet { /** * Constructor of the object. */ public OrderServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { System.out.println("我是init()方法!用来进行初始化工作"); } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); 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 GET 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. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // 设置响应给用户的内容类型和字符集 response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); // 使用表单元素的name属性获取用户输入的表单数据 String userName = request.getParameter("username"); String password = request.getParameter("pwd"); User user = new User(); user.setUserName(userName); user.setPassword(password); //request.setAttribute("ukey", user); if (userName.equals("admin") && password.equals("123456")) { // 请求分发器实现页面转发 request.getRequestDispatcher("success.jsp").forward(request,response); } else { // 请求重定向实现页面转向 response.sendRedirect("failure.jsp"); // 使用相对路径 } out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { System.out.println("我是destroy()方法!用来进行销毁实例的工作"); // Put your code here } }
web.xml文件
<servlet>
<servlet-name>OrderServlet</servlet-name>
<servlet-class>mybao.OrderServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OrderServlet</servlet-name>
<url-pattern>/OrderServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>