1. 程式人生 > >JavaWeb基礎 繼承GenericServlet 簡單示例

JavaWeb基礎 繼承GenericServlet 簡單示例

tro cep link class exceptio app_id pts ase import

禮悟:
   好好學習多思考,尊師重道存感恩。葉見尋根三二一,江河湖海同一體。
虛懷若谷良心主,願行無悔給最苦。讀書鍛煉強身心,誠勸且行且珍惜。




javaEE:7
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的簡單介紹

網址:http://www.cnblogs.com/btdxqz/p/6806372.html


學習資源:itcast和itheima視頻庫。如果您有公開的資源,可以分享給我的話,用您的資源學習也可以。
博文是觀看視頻後,融入思考寫成的。博文好,是老師講得好。博文壞,是 給最苦 沒認真。

JavaWeb基礎 繼承GenericServlet 簡單示例