// 獲取servlet初始化參數 String enconding = this.getInitParameter("enconding"); System.out.println("servlet初始化參數:" + enconding);
今日在編寫一個servlet類時,結果在運行的時候老是出現空指針異常,結果弄得很是焦做的說。java
type Exception reportweb
message apache
description The server encountered an internal error that prevented it from fulfilling this request.tomcat
exceptionwebsocket
java.lang.NullPointerException javax.servlet.GenericServlet.getInitParameter(GenericServlet.java:80) com.servlet.LoginServlet.doGet(LoginServlet.java:39) com.servlet.LoginServlet.doPost(LoginServlet.java:74) javax.servlet.http.HttpServlet.service(HttpServlet.java:648) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.36 logs.socket
super.init(config);
若是沒有這一行代碼,doGet() 和doPost()方法中出現的空指針異常將不會被處理而致使空指針異常。其實也能夠發現init(ServletConfig config)方法是調用了父類的ide
public void init(ServletConfig config) throws ServletException { this.config = config; init(); } public void init() throws ServletException { }
而在這裏其對異常作出了處理。因此,總結一下:若是創建一個類繼承了HttpServlet類而且重寫了該類的init()方法,那麼必需要保留this
super.init(config);
這一行代碼,不然會出現空指針異常。spa
以上僅爲我的看法,若有失誤,歡迎指出! ^ ^!
指針