「はじめての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 "%r" %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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/08/18 06:06
退会済みユーザー
2019/08/18 06:48
退会済みユーザー
2019/08/18 06:55
2019/08/18 09:56
退会済みユーザー
2019/08/18 10:38