JavaWeb基础 继承GenericServlet 简单示例
2017-10-21 23:19
543 查看
礼悟:
公恒学思合行悟,尊师重道存感恩。叶见寻根三返一,江河湖海同一体。
虚怀若谷良心主,愿行无悔给最苦。读书锻炼养身心,诚劝且行且珍惜。
web.xml
继承GenericServlet的类的代码
效果
如上浏览器访问指定页面 一次,控制台输出信息发生如下变化
扩展
为什么要重写 无参数的init()方法?请看源代码
关于serialVersionUID的简单介绍
学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。
公恒学思合行悟,尊师重道存感恩。叶见寻根三返一,江河湖海同一体。
虚怀若谷良心主,愿行无悔给最苦。读书锻炼养身心,诚劝且行且珍惜。
javaEE:7
javaSE:1.8
JSTL:1.2.2
server:tomcat 8.5
explorer:Firefox
os:windows7 x64
ide:MyEclipse 2017
javaSE:1.8
JSTL:1.2.2
server:tomcat 8.5
explorer:Firefox
os:windows7 x64
ide:MyEclipse 2017
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <servlet> <servlet-name>myGenericServlet</servlet-name> <servlet-class>jizuiku.web.servlet.MyGenericServlet</servlet-class><!-- 执行的是这个类 --> <init-param> <param-name>minNum</param-name> <param-value>10</param-value> </init-param> <init-param> <param-name>maxNum</param-name> <param-value>100</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>myGenericServlet</servlet-name> <!-- 两个name 相应就好 --> <url-pattern>/MyGenericServletDemo</url-pattern><!-- 访问的是这个路径 --> </servlet-mapping> </web-app>
继承GenericServlet的类的代码
package jizuiku.web.servlet; import java.io.IOException; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * * * @author 给最苦 * @version V17.10.20 */ public class MyGenericServlet extends GenericServlet{ /** * */ private static final long serialVersionUID = 1L; @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("执行service()..."); } // 根据源代码可以推出 ,要重写的方法一定是 init() @Override public void init() throws ServletException { // TODO Auto-generated method stub System.out.println("你好,执行初始化程序"); } @Override public void destroy() { // TODO Auto-generated method stub System.out.println("执行destroy()..."); } }
效果
如上浏览器访问指定页面 一次,控制台输出信息发生如下变化
扩展
为什么要重写 无参数的init()方法?请看源代码
/** * Called by the servlet container to indicate to a servlet that the servlet * is being placed into service. See {@link Servlet#init}. * <p> * This implementation stores the {@link ServletConfig} object it receives * from the servlet container for later use. When overriding this form of * the method, call <code>super.init(config)</code>. * * @param config * the <code>ServletConfig</code> object that contains * configuration information for this servlet * @exception ServletException * if an exception occurs that interrupts the servlet's * normal operation * @see UnavailableException */ @Override public void init(ServletConfig config) throws ServletException { this.config = config; this.init(); }
关于serialVersionUID的简单介绍
学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。
相关文章推荐
- 【Servlet学习】继承GenericServlet简单示例
- 【Servlet学习】继承GenericServlet简单示例
- JavaWeb基础之Servlet简单实现用户登陆
- JavaSE8基础 继承一个抽象类的简单示例
- 【Servlet学习】继承servlet接口简单示例
- JavaWeb基础 jsp+servlet分工合作的简单示例
- 【Servlet学习】继承HttpServlet简单示例
- 【Servlet学习】继承servlet接口简单示例
- JavaSE8基础 extends 子类继承父类 简单示例
- 【Servlet学习】继承HttpServlet简单示例
- 用AJAX的Get和Post调用Servlet的简单示例
- JavaWeb基础---从HTTP到Servlet到Jfinal
- JSP+Servlet+mysql简单示例【图文教程】
- JavaSE8基础 死锁之同步嵌套 简单示例
- JSTL-1.2基础 c:choose 简单示例
- JavaWeb基础--HttpServletResponse
- JavaWeb基础--HttpServletRequest
- JavaScript中数组继承的简单示例
- java注解入门的简单介绍与示例(框架基础)
- AngularJS基础 ng-srcset 指令简单示例