###前提・実現したいこと
JSPとServletを使いシステムを制作しています。
登録しているユーザの情報を削除できるページがあるのですが、
現在ログインしているユーザは削除できないようにさせたいです。
しかしなんと記述していいかわかりません。。。
Sessionを使えばできそうなんですが。。。
なのでどなたかご指摘をお願いいたします。
###JSP
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*,javax.naming.*,javax.sql.*,java.text.*"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> <sql:setDataSource var="db" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/Library" user="root" password="" /> <html> <head> <link rel="stylesheet" type="text/css" href="css/Library.css"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>ユーザ管理画面</title> </head> <body> <h1> <p class="login" style="text-align: center"> <img src="img/reading_boy.png" allign="center" width="80" height="80"> ユーザー管理画面 <img src="img/reading_girl.png" allign="center" width="80" height="80"> </p> </h1> <br> <form method="POST" action="user_tsuika"> <h2>新規ユーザ追加</h2> 名前(姓) <input type="text" size="15" name="last_name" size="30"> 名前(名) <input type="text" size="15" name="first_name" size="30"> <br> <br> カナ(姓) <input type="text" size="15" name="last_name_kana" size="30"> カナ(名) <input type="text" size="15" name="first_name_kana" size="30"> <br> <br> ID <input type="text" size="15" name="user_id" size="30"> パスワード <input type="password" size="15" name="password" size="30"> <br> 管理者 or 利用者 <input name="admin" type="radio" value="1">管理者</input> <input name="admin" type="radio" value="2">ユーザ</input> <br> <br> <input style="color: white; WIDTH: 60px; HEIGHT: 30px; background-color: D57200;" type="submit" value="登録" /> </form> <c:if test="${!empty requestScope['errorMessage']}"> <div>${requestScope['errorMessage']}</div> </c:if> <h3>ユーザリスト</h3> <table border="1" class="tbl_01"> <tr> <th>ユーザNo</th> <th>名前(姓)</th> <th>名前(名)</th> <th>カナ(姓)</th> <th>カナ(名)</th> <th>ID</th> <th>パスワード</th> <th>権限</th> <th>削除</th> <% Connection con = null; PreparedStatement ps = null; String sql = null; ResultSet rs = null; try { InitialContext initContext = new InitialContext(); DataSource ds = (DataSource) initContext .lookup("java:comp/env/jdbc/Library"); con = ds.getConnection(); sql = "SELECT user_no,first_name,last_name,first_name_kana,last_name_kana,user_id,password,admin" + " FROM user where delete_flg = 0"; // SELECT命令の準備 ps = con.prepareStatement(sql); // UPDATE命令を実行 rs = ps.executeQuery(); // 結果セットの内容を順に出力 while (rs.next()) { %> </tr> <tr> <td height=70><%=rs.getString("user_no")%></td> <td height=70><%=rs.getString("last_name")%></td> <td height=70><%=rs.getString("first_name")%></td> <td height=70><%=rs.getString("last_name_kana")%></td> <td height=70><%=rs.getString("first_name_kana")%></td> <td height=70><%=rs.getString("user_id")%></td> <td height=70><%=rs.getString("password")%></td> <td height=70> <% if ("1".equals(rs.getString("admin"))) { %>管理者<% } else { %>ユーザー<% } %> </td> <td height=70><form method="POST" action="user_delete"> <input name="user_no" type="hidden" value=<%=rs.getString("user_no")%>> <input name="user_edit" type="hidden" value=<%=rs.getString("user_id")%>> <button class="button" type="submit">削除</button> </form></td> <% } } catch (Exception e) { throw new ServletException(e); } finally { try { if (rs != null) { rs.close(); } if (rs != null) { ps.close(); } if (rs != null) { con.close(); } } catch (Exception e) { } } %> </table> <div class="bb"> <form action="user_edit.jsp" style="display: inline"> <input style="color: white; WIDTH: 60px; HEIGHT: 30px; background-color: D57200;" type="submit" value="編集" /> </form> <form action="user_resurrection.jsp" style="display: inline"> <input style="color: white; WIDTH: 120px; HEIGHT: 30px; background-color: D57200;" type="submit" value="削除済ユーザ編集" /> </form> <form action="kanri_mypage.jsp" style="display: inline"> <input style="color: white; WIDTH: 100px; HEIGHT: 30px; background-color: D57200;" type="submit" value="TOPページ" /> </form> </div> </body> </html>
###Servlet
import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import javax.naming.InitialContext; 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.sql.DataSource; @WebServlet("/user_delete") public class user_delete extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection con = null; PreparedStatement ps = null; String sql = null; try { // 確立 InitialContext initContext = new InitialContext(); DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/Library"); con = ds.getConnection(); // sql文の発行 sql = "UPDATE user SET delete_flg = (CASE WHEN delete_flg = 0 THEN 1 WHEN delete_flg = 1 THEN 0 END) WHERE user_id=?"; ps = con.prepareStatement(sql); ps.setString(1, request.getParameter("user_edit")); ps.executeUpdate(); } catch (Exception e) { throw new ServletException(e); } finally { try { if (con != null) { con.close(); } if (ps != null) { ps.close(); } } catch (Exception e) { } } response.sendRedirect("user_kanri.jsp"); } }
