init方法被设计成只调用一次
它在第一次创建Servlet时被调用,在后续每次调用时不在调用。
因此,它是用于一次性初始化。
Servlet容器(即Web服务器)调用service()方法来处理客户端(浏览器)的请求,判断请求是post还是get,如果是post,则调用doPost()方法处理请求;如果是get,则调用doGet()方法进行请求处理,并把格式化的响应返回给客户端。
destroy() 方法只会被调用一次,在 Servlet 生命周期结束时被调用。
destroy() 方法可以让您的 Servlet 关闭数据库连接、停止后台线程、把 Cookie 列表或点击计数器写入到磁盘,并执行其他类似的清理活动。
在调用 destroy() 方法之后,servlet 对象被标记为垃圾回收。
注意:无参构造方法和init()方法只有在servlet首次被调用的时候才会执行,后续再次请求的时候不会被执行。
GET请求来自于一个URL的正常请求,或者来自于一个未指定method的HTML表单,它由doGet()方法处理。
POST请求来自于一个特别指定了method为post的HTML表单,它由doPost()方法处理。
建议:
get方式的安全性较Post方式要差些,包含机密信息的话,建议用post数据提交方式。
在做数据查询时,建议用Get方式;而在做数据添加、修改或删除时,建议用Post方式。
get方式会将用户信息显示在地址栏中(效率高但不安全,不多说)
post方式会将信息封装到浏览器中(如下图)
package com.ebuy.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(name = "HelloServlet") public class HelloServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
package com.ebuy.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class HelloServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>HelloServlet</servlet-name> //HelloServlet类的路径 <servlet-class>com.ebuy.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> //浏览器访问入口url <url-pattern>/HelloServlet</url-pattern> </servlet-mapping> </web-app>
package com.ebuy.servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class HelloServlet extends HttpServlet { /** * 实例化方法 */ public HelloServlet(){ System.out.println("调用HelloServlet无参构造方法(实例化)"); } /** * 初始化方法 * @param config * @throws ServletException */ @Override public void init(ServletConfig config) throws ServletException { super.init(config); System.out.println("init初始化方法"); } /** * 请求处理方法 * @param req * @param resp * @throws ServletException * @throws IOException */ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("调用service处理请求方法"); super.service(req, resp); } /** * 终止服务方法(销毁servlet) */ @Override public void destroy() { super.destroy(); System.out.println("正在销毁servlet"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("正在调用doPost方法"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("正在调用doGet方法"); } }
由此可知在访问HelloServlet时:
点击停止服务器查看控制台(调用destroy()销毁方法):
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--全局参数,当前工程中所有的servlet都可以访问到--> <context-param> <param-name>devtools</param-name> <param-value>idea</param-value> </context-param> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>com.ebuy.servlet.HelloServlet</servlet-class> <!--局部参数,当前配置的dbtype参数只能在HelloServlet中可以访问到,其他servlet不可以访问到--> <init-param> <param-name>dbtype</param-name> <param-value>oracle</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/HelloServlet</url-pattern> </servlet-mapping> </web-app>
/** * 初始化方法 * @param config * @throws ServletException */ @Override public void init(ServletConfig config) throws ServletException { super.init(config); System.out.println("init初始化方法"); System.out.println("局部变量dbtype---" + config.getInitParameter("dbtype")); System.out.println("全局变量devtools---" + this.getServletContext().getInitParameter("devtools")); }
package com.ebuy.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(name = "RoleServlet",urlPatterns = {"/RoleServlet","/bbb","/ccc"}) public class RoleServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("我是dpPost方法"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("我是doGet方法"); } }
此处可以写称集合的形式,每个名字都可以访问,urlPatterns = {"/RoleServlet","/bbb","/ccc"})