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

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

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

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Tomcat

TomcatはApache Software Foundation (ASF)で開発されたオープンソースのWebコンテナです。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

2453閲覧

JavaサーブレットからMysqlのコネクションが取得できない

kar

総合スコア10

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Tomcat

TomcatはApache Software Foundation (ASF)で開発されたオープンソースのWebコンテナです。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2019/01/17 10:12

編集2019/01/17 10:15

環境

AWSのEC2インスタンス
・tomcat8
・mysql8

ローカル環境
・tomcat8
・mysql8
・eclipse

発生している問題・エラーメッセージ

ローカル環境で実行するとmysqlに接続ができデータソースを取得できるコードがあります。sample.jsp→サーブレット→next.jsp(フォワード先で取得できたと表示できます)
以下にサーブレットのコードの一部を抜粋します。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { DataSource dataSource = null; Context con = new InitialContext(); dataSource = (DataSource)con.lookup("java:comp/env/db_shift"); if(dataSource!=null) { String aaa = "取得できた"; request.setAttribute("aaa",aaa); }else { String aaa = "むりぼ"; request.setAttribute("aaa",aaa); } ServletContext context = getServletContext(); RequestDispatcher rd = context.getRequestDispatcher("/jsp/next.jsp"); rd.forward(request, response); }catch(Exception ex) { ex.printStackTrace(); } }

このコードをec2上のtomcatにtomcat managerを使ってデプロイをして、server.xmlとcontext.xmlをjdbcに関するところを書き換え実行したところ、エラーメッセージ(404.505)などが全く出ずに白い画面のままになってしまいます。(エラーログが全く吐き出されないためピンポイントの原因がわかりません)

試したこと

・上記のサーブレットのコードをフォワードだけする記述に変更→ページの遷移はできるのでデータベース関係がエラーとみています
・ローカル環境からEC2上のmysqlに接続する→接続ができてデータソースも取得できる

以上からec2上のtomcatのconf配下にあるserver.xmlとcontext.xmlの記述が怪しいのではと考えているのですが、自力ではこれ以上わからなかったため質問いたしました。

server.xml

1<?xml version="1.0" encoding="UTF-8"?> 2<!-- 3 Licensed to the Apache Software Foundation (ASF) under one or more 4 contributor license agreements. See the NOTICE file distributed with 5 this work for additional information regarding copyright ownership. 6 The ASF licenses this file to You under the Apache License, Version 2.0 7 (the "License"); you may not use this file except in compliance with 8 the License. You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17--> 18<!-- Note: A "Server" is not itself a "Container", so you may not 19 define subcomponents such as "Valves" at this level. 20 Documentation at /docs/config/server.html 21 --> 22<Server port="8005" shutdown="SHUTDOWN"> 23 <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> 24 <!-- Security listener. Documentation at /docs/config/listeners.html 25 <Listener className="org.apache.catalina.security.SecurityListener" /> 26 --> 27 <!--APR library loader. Documentation at /docs/apr.html --> 28 <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> 29 <!-- Prevent memory leaks due to use of particular java/javax APIs--> 30 <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> 31 <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> 32 <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> 33 34 <!-- Global JNDI resources 35 Documentation at /docs/jndi-resources-howto.html 36 --> 37 <GlobalNamingResources> 38 <!-- Editable user database that can also be used by 39 UserDatabaseRealm to authenticate users 40 --> 41 <Resource name="UserDatabase" auth="Container" 42 type="org.apache.catalina.UserDatabase" 43 description="User database that can be updated and saved" 44 factory="org.apache.catalina.users.MemoryUserDatabaseFactory" 45 pathname="conf/tomcat-users.xml" /> 46<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" initialSize="5" maxIdle="5" maxTotal="5" maxWaitMillis="5000" name="jdbc/db_shift" password="m9wim6YM9wim6y-" type="javax.sql.DataSource" url="jdbc:mysql://13.250.157.201:3306/db_shift?serverTimezone=JST" username="admin"/> 47 </GlobalNamingResources> 48 49 <!-- A "Service" is a collection of one or more "Connectors" that share 50 a single "Container" Note: A "Service" is not itself a "Container", 51 so you may not define subcomponents such as "Valves" at this level. 52 Documentation at /docs/config/service.html 53 --> 54 <Service name="Catalina"> 55 56 <!--The connectors can use a shared executor, you can define one or more named thread pools--> 57 <!-- 58 <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" 59 maxThreads="150" minSpareThreads="4"/> 60 --> 61 62 63 <!-- A "Connector" represents an endpoint by which requests are received 64 and responses are returned. Documentation at : 65 Java HTTP Connector: /docs/config/http.html 66 Java AJP Connector: /docs/config/ajp.html 67 APR (HTTP/AJP) Connector: /docs/apr.html 68 Define a non-SSL/TLS HTTP/1.1 Connector on port 8080 69 --> 70 <Connector port="8080" protocol="HTTP/1.1" 71 connectionTimeout="20000" 72 redirectPort="8443" /> 73 <!-- A "Connector" using the shared thread pool--> 74 <!-- 75 <Connector executor="tomcatThreadPool" 76 port="8080" protocol="HTTP/1.1" 77 connectionTimeout="20000" 78 redirectPort="8443" /> 79 --> 80 <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 81 This connector uses the NIO implementation. The default 82 SSLImplementation will depend on the presence of the APR/native 83 library and the useOpenSSL attribute of the 84 AprLifecycleListener. 85 Either JSSE or OpenSSL style configuration may be used regardless of 86 the SSLImplementation selected. JSSE style configuration is used below. 87 --> 88 <!-- 89 <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" 90 maxThreads="150" SSLEnabled="true"> 91 <SSLHostConfig> 92 <Certificate certificateKeystoreFile="conf/localhost-rsa.jks" 93 type="RSA" /> 94 </SSLHostConfig> 95 </Connector> 96 --> 97 <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2 98 99 This connector uses the APR/native implementation which always uses 100 OpenSSL for TLS. 101 Either JSSE or OpenSSL style configuration may be used. OpenSSL style 102 configuration is used below. 103 --> 104 <!-- 105 <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol" 106 maxThreads="150" SSLEnabled="true" > 107 <UpgradeProtocol className="org.apache.coyote.http4.Http2Protocol" /> 108 <SSLHostConfig> 109 <Certificate certificateKeyFile="conf/localhost-rsa-key.pem" 110 certificateFile="conf/localhost-rsa-cert.pem" 111 certificateChainFile="conf/localhost-rsa-chain.pem" 112 type="RSA" /> 113 </SSLHostConfig> 114 </Connector> 115 --> 116 117 <!-- Define an AJP 1.3 Connector on port 8009 --> 118 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> 119 120 121 <!-- An Engine represents the entry point (within Catalina) that processes 122 every request. The Engine implementation for Tomcat stand alone 123 analyzes the HTTP headers included with the request, and passes them 124 on to the appropriate Host (virtual host). 125 Documentation at /docs/config/engine.html --> 126 127 <!-- You should set jvmRoute to support load-balancing via AJP ie : 128 <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> 129 --> 130 <Engine name="Catalina" defaultHost="localhost"> 131 132 <!--For clustering, please take a look at documentation at: 133 /docs/cluster-howto.html (simple how to) 134 /docs/config/cluster.html (reference documentation) --> 135 <!-- 136 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> 137 --> 138 139 <!-- Use the LockOutRealm to prevent attempts to guess user passwords 140 via a brute-force attack --> 141 <Realm className="org.apache.catalina.realm.LockOutRealm"> 142 <!-- This Realm uses the UserDatabase configured in the global JNDI 143 resources under the key "UserDatabase". Any edits 144 that are performed against this UserDatabase are immediately 145 available for use by the Realm. --> 146 <Realm className="org.apache.catalina.realm.UserDatabaseRealm" 147 resourceName="UserDatabase"/> 148 </Realm> 149 150 <Host name="localhost" appBase="webapps" 151 unpackWARs="true" autoDeploy="true"> 152 153 <!-- SingleSignOn valve, share authentication between web applications 154 Documentation at: /docs/config/valve.html --> 155 <!-- 156 <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> 157 --> 158 159 <!-- Access log processes all example. 160 Documentation at: /docs/config/valve.html 161 Note: The pattern used is equivalent to using pattern="common" --> 162 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" 163 prefix="localhost_access_log" suffix=".txt" 164 pattern="%h %l %u %t &quot;%r&quot; %s %b" /> 165 166 </Host> 167 </Engine> 168 </Service> 169</Server>

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

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

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

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

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

guest

回答1

0

自己解決

java.sql.DriverManagerクラスを使うことでmysqlと接続することができました。
このことからserver.xmlやcontext.xmlの設定が効いていないと思われるので、配置場所や記述内容をいろいろ試すのがよいと思います。
私はいろいろ試した結果、どれもだめだったので推奨されていないjava.sql.DriverManagerクラスの接続で妥協することにしました。

投稿2019/01/18 14:34

kar

総合スコア10

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問