前提・実現したいこと
学校でTomcat、MySQLを用いてWebアプリを作ったので、自宅でも表示させたいと思ったのでEclipseを用いて色々と構成をしていたのですが、表示がされない様なので今回質問をさせて頂きました。
先ず環境ですが
・Windows 10
・Eclipse 2018 12(All in oneの64bit版Java Full Edition(C:pleiadesにあります))
・Apache Tomcat/8.5.37(C:Tomcat8.5にあります)
・jdk1.8.0_77
・MySQL 8.0
になります。
下記の図が、表示が出来た時のTomcatの構成です。
Eclipseでは、上記のファイルを用いて構成を行いました。
下記は、Eclipseのファイル構成になります。
発生している問題・エラーメッセージ
TomcatのサーバーでGachaServlet.javaを右クリック→サーバで実行をしましたところ、下記の様なエラーが出ました。
ソースコード
以下が、今回で使うファイルのソースコードです。
- GachaServlet.java
import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import gacha.*; import java.util.Random; import java.sql.*; public class GachaServlet extends HttpServlet { private static String DB_NAME = "gacha_db"; private static String DB_USER = "root"; private static String DB_PASS = "root"; private static String LOGIN_JSP = "WEB-INF/Login.jsp"; private static String START_JSP = "WEB-INF/Start.jsp"; private static String MISS_JSP = "WEB-INF/Miss.jsp"; private static String HIT_JSP = "WEB-INF/Hit.jsp"; private static String LOG_JSP = "WEB-INF/log.jsp"; private static String RANKING_JSP = "WEB-INF/ranking.jsp"; public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doProcess(req, res); } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doProcess(req, res); } protected void doProcess(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String nextView = ""; DatabaseConnector dc = null; UserManager um = null; RankingManager rm = null; Random r = new Random(); int N = r.nextInt(100); try { dc = new DatabaseConnector(DB_NAME, DB_USER, DB_PASS); dc.openConnection(); um = new UserManager(dc); rm = new RankingManager(dc); req.setCharacterEncoding("UTF-8"); res.setContentType("text/html; charset=UTF-8"); PrintWriter out = res.getWriter(); String action = req.getParameter("action"); if(action == null) { action = ""; } if(action.equals("") || action.equals("loginPage")) { nextView = LOGIN_JSP; } else if(action.equals("login")) { if(login(um, req)) { nextView = START_JSP; } else { nextView = LOGIN_JSP; } } else if(action.equals("logout")) { nextView = logout(req); } else if(action.equals("registration")) { nextView = registration(um, req); } else if(action.equals("gacha_result")) { if(N >= 0 && 95 > N){ nextView = miss(req); } else { Connection con=null; Statement st=null; String login="root"; String passwd="root"; ResultSet rs = null; nextView = hit(req, rm); } } else if(action.equals("start")) { nextView = START_JSP; } else if(action.equals("log")) { nextView = LOG_JSP; } else if(action.equals("log_reset")) { Connection con=null; Statement st=null; String login="root"; String passwd="root"; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/gacha_db" + "?useUnicode=true&characterEncoding=UTF-8" + "&autoReconnect=true"; con = DriverManager.getConnection(url, login, passwd); st = con.createStatement(); String sql = "truncate table log_tbl"; rs = st.executeQuery(sql); } catch (Exception e) { } finally { try { rs.close(); con.close(); st.close(); } catch (Exception e) { } } nextView = LOG_JSP; } else if(action.equals("ranking")) { nextView = RANKING_JSP;//showRanking(rm, req); } if(nextView.equals("")) { req.setAttribute("message", "不正なアクションが要求されました(" + req.getParameter("action") + ")"); nextView = LOGIN_JSP; } dc.closeConnetion(); } catch(Exception e) { e.printStackTrace(); req.setAttribute("message", "例外が発生しました:" + e.toString()); nextView = LOGIN_JSP; } finally { req.getRequestDispatcher(nextView).forward(req, res); } } private boolean login(UserManager um, HttpServletRequest req) throws Exception { String userName = req.getParameter("uname"); String password = req.getParameter("pass"); if(!isValid(userName) || !isValid(password)) { req.setAttribute("message", "記入漏れがあります"); return false; } else if(um.authenticate(userName, password) == false) { req.setAttribute("message", "ユーザ名またはパスワードが違います"); return false; } else { HttpSession session = req.getSession(true); int add = 0; session.setAttribute("userName", userName); session.setAttribute("add", add); req.setAttribute("message", "認証に成功しました"); } return true; } private String registration(UserManager um, HttpServletRequest req) throws Exception { String userName = req.getParameter("uname"); String password = req.getParameter("pass"); String password2 = req.getParameter("pass2"); if(!isValid(userName) || !isValid(password) || !isValid(password2)) { req.setAttribute("message", "記入漏れがあります"); } else if(!password.equals(password2)) { req.setAttribute("message", "パスワードが確認用と一致しません"); } else if(um.registration(userName, password)) { req.setAttribute("message", "登録に成功しました"); } else { req.setAttribute("message", "すでに登録されています"); } return LOGIN_JSP; } private String logout(HttpServletRequest req) { HttpSession session = req.getSession(false); if(session != null) { session.invalidate(); req.setAttribute("message", "ログアウトしました"); } return LOGIN_JSP; } private String miss(HttpServletRequest req){ HttpSession session = req.getSession(true); int add = (Integer)session.getAttribute("add"); add += 100; session.setAttribute("add", add); return MISS_JSP; } private String hit(HttpServletRequest req, RankingManager rm) { HttpSession session = req.getSession(true); int add = (Integer)session.getAttribute("add"); add += 100; session.setAttribute("add", add); int MONEY = add; String UNAME = (String)session.getAttribute("userName"); rm.RankingSet(MONEY, UNAME); session.setAttribute("add", 0); return HIT_JSP; } protected boolean isValid(String str) { if(str != null && !str.equals("")) { return true; } else { return false; } } }
試したこと
今回、環境を整えるにあたり、行ったこと、確認したことを書きます。
- ウィンドウ→設定で、一般→ワークスペース→UTF-8を確認。
- Java→インストール済みのJRE→C:\Program Files\Java\jdk1.8.0_77に設定。
コンパイラー準拠レベルは1.8。
- Tomcatをインストール後、EclipseのプラグインでTomcatをインストールしました。
また、ウィンドウ→設定のTomcatでTomcatバージョンを8.xで選択、TomcatホームをC:Tomcat8.5に選択、コンテキスト宣言モードをコンテキスト・ファイルに選択。
JVM設定は、Javaのjdk1.8.0_77を選択。
- サーバー→ランタイム環境のディレクトリをC:Tomcat8.5、JREをjdk1.8.0_77に選択。
- 本プロジェクのを実行は、GachaServlet.javaを右クリック→実行→サーバーで実行。
新規サーバーは、Tomcat v8.5 サーバーで、ランタイム環境は上記のTomcat8.5に選択。
- MySQLをインストールし、データベースとテーブルを作成後、EclipseのパースペクティブでDB Viewerを選択。登録→JDBCドライバをC:\Program Files (x86)\MySQL\Connector J 8.0/mysql-connector-java-8.0.13.jarに選択。→JDBC Driverをcom.mysql.jdbc.Driver、Type4、接続文字列をjdbc:mysql://localhost:3306?characterEncoding=UTF-8&serverTimezone=JST、ユーザ名とパスワードを入力後、接続。
今回、初めて質問をさせて頂いたのですが、見辛いかと思いましたら大変申し訳御座いません。
どうぞ、宜しくお願い致します。
また、文字数制限がある様なので、本アプリで使いますソースコードを後に載せさせて頂きます。
あなたの回答
tips
プレビュー