質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
servlet

Servletとは、Webページの動的な生成やデータ処理などをサーバ上で実行するために、Javaで作成されたプログラムです。 ショッピングサイトやオンラインバンキングといった、動的なウェブサイトの構築に用いられています。

Q&A

解決済

1回答

5431閲覧

サーブレットを実行するとエラーがでてしまう。

退会済みユーザー

退会済みユーザー

総合スコア0

servlet

Servletとは、Webページの動的な生成やデータ処理などをサーバ上で実行するために、Javaで作成されたプログラムです。 ショッピングサイトやオンラインバンキングといった、動的なウェブサイトの構築に用いられています。

0グッド

0クリップ

投稿2019/08/18 05:02

編集2019/08/18 06:43

「はじめてのJSP&サーブレット」をEclipse4.6を使って勉強しています。
以下のコードを実行すると
「HTTPステータス 500 - javax.naming.NamingException: Cannot create PoolableConnectionFactory (Could not connect to address=(host=127.0.0.1)(port=3306)(type=master) : Connection refused: connect)」のエラーが出てうまくいきません。10章まで実行できていたのですが、途中からエラーが出始めました。teratailなどにも結構質問がでているのですが、うまくいきません。
ヒント、ご教授をよろしくお願いいたします。

例外レポート メッセージ javax.naming.NamingException: Cannot create PoolableConnectionFactory (Could not connect to address=(host=127.0.0.1)(port=3306)(type=master) : Connection refused: connect) 説明 The server encountered an internal error that prevented it from fulfilling this request. 例外 javax.servlet.ServletException: javax.naming.NamingException: Cannot create PoolableConnectionFactory (Could not connect to address=(host=127.0.0.1)(port=3306)(type=master) : Connection refused: connect) todo.web.SearchServlet.doGet(SearchServlet.java:33) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) todo.filter.TokenFilter.doFilter(TokenFilter.java:51) todo.filter.RequestEncodingFilter.doFilter(RequestEncodingFilter.java:41) 原因 javax.naming.NamingException: Cannot create PoolableConnectionFactory (Could not connect to address=(host=127.0.0.1)(port=3306)(type=master) : Connection refused: connect) org.apache.naming.NamingContext.lookup(NamingContext.java:864) org.apache.naming.NamingContext.lookup(NamingContext.java:158) org.apache.naming.NamingContext.lookup(NamingContext.java:835) org.apache.naming.NamingContext.lookup(NamingContext.java:158) org.apache.naming.NamingContext.lookup(NamingContext.java:835) org.apache.naming.NamingContext.lookup(NamingContext.java:158) org.apache.naming.NamingContext.lookup(NamingContext.java:835) org.apache.naming.NamingContext.lookup(NamingContext.java:172) org.apache.naming.SelectorContext.lookup(SelectorContext.java:157) javax.naming.InitialContext.lookup(InitialContext.java:417) todo.dao.DAO.getConnection(DAO.java:33) todo.dao.DAO.getPreparedStatement(DAO.java:84) todo.dao.TodoDAO.todoList(TodoDAO.java:22) todo.web.SearchServlet.doGet(SearchServlet.java:28) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) todo.filter.TokenFilter.doFilter(TokenFilter.java:51) todo.filter.RequestEncodingFilter.doFilter(RequestEncodingFilter.java:41)
//サーバーのserver.xmlとcontext.xml // server.xml少し省略 <?xml version="1.0" encoding="UTF-8"?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener"/> <!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> --> <!--APR library loader. Documentation at /docs/apr.html --> <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/> </GlobalNamingResources> <Service name="Catalina"> <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> --> <!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/> <Engine defaultHost="localhost" name="Catalina"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <!-- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase". Any edits that are performed against this UserDatabase are immediately available for use by the Realm. --> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt"/> <Context docBase="10_todo" path="/10_todo" reloadable="false" source="org.eclipse.jst.jee.server:10_todo"/></Host> </Engine> </Service> </Server> // context.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <Context> <!-- Default set of monitored resources. If one of these changes, the --> <!-- web application will be reloaded. --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> --> </Context>
// 10_todo/WebContent/MATA-INF/context.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <Context> <Resource driverClassName="org.mariadb.jdbc.Driver" auth="Container" initialSize="1" maxIdle="1" maxWaitMillis="-1" name="jdbc/localDB" type="javax.sql.DataSource" url="jdbc:mysql://127.0.0.1:3306/test" username="root" password="password" defaultAutoCommit="false" /> <ResourceLink name="jdbc/authDB" global="jdbc/authDB" type="javax.sql.DataSource" /> </Context>

servlet

1//SearchServlet.java 2package todo.web; 3 4import java.io.IOException; 5import java.util.List; 6 7import javax.servlet.RequestDispatcher; 8import javax.servlet.ServletException; 9import javax.servlet.annotation.WebServlet; 10import javax.servlet.http.HttpServlet; 11import javax.servlet.http.HttpServletRequest; 12import javax.servlet.http.HttpServletResponse; 13 14import todo.dao.TodoDAO; 15import todo.dto.Todo; 16 17@WebServlet( urlPatterns={"/todo/search"}) 18public class SearchServlet extends HttpServlet { 19 20 private static final long serialVersionUID = 1L; 21 22 protected void doGet(HttpServletRequest request, 23 HttpServletResponse response) throws ServletException, IOException { 24 25 // DAOの取得 26 try(TodoDAO dao = new TodoDAO()) { 27 28 // タスクのリストを一覧で取得し、リクエスト属性へ格納する 29 List<Todo> list = dao.todoList(); 30 31 request.setAttribute("todoList", list); 32 33 } catch (Exception e) { 34 throw new ServletException(e); 35 } 36 37 // 検索一覧を表示する 38 RequestDispatcher rd = request.getRequestDispatcher("/search.jsp"); 39 rd.forward(request, response); 40 } 41 42 @Override 43 protected void doPost(HttpServletRequest request, HttpServletResponse response) 44 throws ServletException, IOException { 45 doGet(request, response); 46 } 47} 48

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

スタックトレースを見た限りでは、実際に例外が発生しているのが

javax.naming.InitialContext.lookup(InitialContext.java:417) todo.dao.DAO.getConnection(DAO.java:33)

であり、原因が

Cannot create PoolableConnectionFactory (Could not connect to address=(host=127.0.0.1)(port=3306)(type=master)

なので、tomcatのserver.xmlやcontext.xml、ないしはWebアプリケーションの META-INF/context.xml に、MySQLの接続設定が書かれていないか、合っていないからではないでしょうか。

投稿2019/08/18 06:01

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/08/18 06:06

ありがとうございます。確認します。
退会済みユーザー

退会済みユーザー

2019/08/18 06:48

EclipseのServers.server.xmlとServers.context.xmlとWebContent.META-INF.context.xmlを追加しましたので、見ていただけませんか。Servers.server.xmlはコメントのところを省略してあげています。
退会済みユーザー

退会済みユーザー

2019/08/18 06:55

MariaDBとHeidiSQLソフトを使っています。HeidiSQLからMariaDBにせつぞくできていましたが、それもいまつながらない状態です。パスワードはpassword,rootの2つしか使っていないのですが、どちらでも 接続できません。 こんがらがってどこを見ればいいかもわからない状態です。 ゴールデンウイークもあと1日明日までに片を付けたいので、よろしくお願いいたします。
A-pZ

2019/08/18 09:56

「HeidiSQLからMariaDBにせつぞくできていましたが、それもいまつながらない状態」ということは、MariaDBが起動していないのではないでしょうか。
退会済みユーザー

退会済みユーザー

2019/08/18 10:38

MariaDBとHeidiSQLは入れ替えて今接続できています。 Eclipseも入れ替えていまはつながっているんですが、新たな問題にぶつかっています。違う質問なので、今から上げようと思っています。 有り難うございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問