java サーブレット jspで会員登録画面を作成しているのですが、入力した情報をデータベースにinsertできません。
insert.jspの会員登録をクリックすると、DB内のloginという名前のテーブルのid,name,pwdカラムにそれぞれinsertされるようにしたいです。
よろしくお願いいたします。
insert.jsp
<div class="wrap"> <div class="loginP"> <h2>会員登録</h2> <form action="insert.do"> <div class="login_id"> <h4>ID</h4> <input type="text" name="id" id="" placeholder="ID"> </div> <div class="login_name"> <h4>Name</h4> <input type="text" name="name" id="" placeholder="Name"> </div> <div class="login_pw"> <h4>Password</h4> <input type="password" name="pwd" id="pw1" placeholder="Password"> </div> <div class="login_pw2"> <h4>PasswordCheck</h4> <input type="password" name="pwd" id="pw2" placeholder="Password"> </div> <div class="submit"> <input class="inbu" type="submit" value="会員登録" /> </div> </form> </div> </div>
context.xml(META-INF配下)
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --><!-- The contents of this file will be loaded for each web application --><Context> <!-- Default set of monitored resources. If one of these changes, the --> <!-- web application will be reloaded. --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource> <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <Resource name="jdbc/jsp" auth="Container" type="javax.sql.DataSource" factory = "org.apache.tomcat.jdbc.pool.DataSourceFactory" driverClassName = "com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:8889/DB名 username="root" password="パスワード" maxActive="50" maxIdle="20" maxWait="10000" logAbandoned="false" /> </Context>
LoginDao.java
package cs.dit.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; import cs.dit.dto.LoginDto; public class LoginDao { private java.sql.Connection getConnection() throws Exception { Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/DB名"); DataSource ds = (DataSource) envCtx.lookup("jdbc/jsp"); java.sql.Connection con = (java.sql.Connection) ds.getConnection(); return con; } public ArrayList<LoginDto> list() { String sql = "select id, name, pwd from login"; ArrayList<LoginDto> dtos = new ArrayList<LoginDto>(); try (java.sql.Connection con = getConnection(); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql); ) { while (rs.next()) { LoginDto dto = new LoginDto(); dto.setId(rs.getString("id")); dto.setName(rs.getString("name")); dto.setPwd(rs.getString("pwd")); dtos.add(dto); } } catch (Exception e) { e.printStackTrace(); } return dtos; } public void insert(LoginDto dto) { String sql = "insert into login(id, name, pwd) values(?, ?, ?)"; try (java.sql.Connection con = getConnection(); PreparedStatement pstmt = con.prepareStatement(sql);) { pstmt.setString(1, dto.getId()); pstmt.setString(2, dto.getName()); pstmt.setString(3, dto.getPwd()); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } public int loginCheck(String id, String pwd) { String sql = "SELECT pwd FROM login WHERE id =? and pwd = ?"; int flag = 0; try ( Connection con = getConnection(); PreparedStatement pstmt = con.prepareStatement(sql); ) { pstmt.setString(1, id); pstmt.setString(2, pwd); try(ResultSet rs = pstmt.executeQuery();) { if(rs.next()) { flag = 1; }else { flag = 0; } }catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } return flag; } public void update(LoginDto dto) { String sql = "UPDATE login SET name = ?, pwd = ? WHERE id =?"; try ( Connection con = getConnection(); PreparedStatement pstmt = con.prepareStatement(sql); ) { pstmt.setString(1, dto.getName()); pstmt.setString(2, dto.getPwd()); pstmt.setString(3, dto.getId()); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } public void delete(String id) { String sql = "DELETE FROM login WHERE id =?"; try ( Connection con = getConnection(); PreparedStatement pstmt = con.prepareStatement(sql); ) { pstmt.setString(1, id); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } }
LoginInsertService.java
package cs.dit.service; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cs.dit.dao.LoginDao; import cs.dit.dto.LoginDto; public class LoginInsertService implements LoginService { @Override public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String id = request.getParameter("id"); String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); LoginDto dto = new LoginDto(id, name, pwd); LoginDao dao = new LoginDao(); dao.insert(dto); } }
lController.java(サーブレット)
package cs.dit.controller; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import cs.dit.dao.LoginDao; import cs.dit.dto.LoginDto; import cs.dit.service.*; @WebServlet("*.do") public class lController extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=utf-8"); PrintWriter out = response.getWriter(); String uri = request.getRequestURI(); String com = uri.substring(uri.lastIndexOf("/")+1, uri.lastIndexOf(".do")); String page = null; if(com != null && com.trim().equals("insert")) { LoginInsertService service = new LoginInsertService(); service.execute(request, response); page = "login.jsp"; }else if(com != null && com.trim().equals("login")) { page = "login.jsp"; }else if(com != null && com.trim().equals("main")) { page = "main.jsp"; }else if(com != null && com.trim().equals("loginIn")) { }else if(com != null && com.trim().equals("delete")) { DeleteService service = new DeleteService(); } RequestDispatcher rd = request.getRequestDispatcher(page); rd.forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
あなたの回答
tips
プレビュー