2016-09-30 8 views
1

Webアプリケーションのコンテキストパラメータに問題があります。私のメソッドweb.xmlからデータベースパラメータを初期化するサーブレット

getServletContext().getInitParameter("pass"); 

ヌルが返されます。 は、ここに私のweb.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
<display-name>Log4jWebDemo1</display-name> 
<context-param> 
    <param-name>log4j-config-location</param-name> 
    <param-value>WEB-INF/log4j.properties</param-value> 
</context-param> 
    <context-param> 
     <param-name>url</param-name> 
     <param-value>dbc:postgresql://localhost/OlapTest</param-value> 
    </context-param> 
    <context-param> 
     <param-name>name</param-name> 
     <param-value>org.postgresql.Driver</param-value> 
    </context-param> 
    <context-param> 
     <param-name>usr</param-name> 
     <param-value>postgres</param-value> 
    </context-param> 
    <context-param> 
     <param-name>pass</param-name> 
     <param-value>admin</param-value> 
    </context-param> 
    <servlet> 
    <servlet-name>Hello World</servlet-name> 
    <servlet-class>pl.javastart.servlets.HelloWorldServlet</servlet-class> 

    </servlet> 
    <servlet-mapping> 
    <servlet-name>Hello World</servlet-name> 
    <url-pattern>/test</url-pattern> 
    </servlet-mapping> 
</web-app> 

そして、私のサーブレットです:それはスロー私のアプリケーションを実行した後

public class HelloWorldServlet extends HttpServlet { 
     private static final long serialVersionUID = 1L; 
     private String url2 = ""; 
     private String driver = ""; 
     private String username = ""; 
     private String password = ""; 

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 


     PrintWriter out = response.getWriter(); 
     url2 = getServletContext().getInitParameter("url"); 
     driver = getServletContext().getInitParameter("name"); 
     username = getServletContext().getInitParameter("usr"); 
     password = getServletContext().getInitParameter("pass"); 

     DatabaseHandling d1 = new DatabaseHandling(driver,url2,username,password); 

     d1.createConnection(out); 

     } 
} 

java.lang.NullPointerExceptionが

+1

完全な例外スタックトレースを貼り付けることはできますか? –

+0

すでに私の友人でした – LucasPG

+0

DatabaseHandlingクラスコード –

答えて

1

ありますすることができますNullPointerException exceにつながる可能性は低いption

  1. PostgresSQLのドライバ(jarファイル)ライブラリがクラスパスに追加されません。
  2. DBに接続するために記述されたコードをチェックするために、DatabaseHandlingクラスを共有する必要があります。
  3. 変化として以下のweb.xml:

    <context-param> <param-name>url</param-name> <param-value>jdbc:postgresql://localhost/OlapTest</param-value> </context-param>

Jが欠落しています。エラー番号dbcを確認してください。

+0

私はすでにそれを見ました。 :) ご協力ありがとうございます。 – LucasPG

関連する問題