データベースのファイルのステータスデータを挿入するためにstatus.jsp
を作成しました。ステータス(INまたはOUT)には2つのオプションがあります。提出すると、データが挿入されたことを示すinserted.jsp
に行き、moreまたはlogoutを挿入するためにゴーリンクをstatus.jsp
に送ります。 オプションINを選択しない限り、ユーザーがログアウトできないようにします。 例えば、ユーザがファイル番号を入力した後にステータスが入力された場合、inserted.jsp
ページからstatus.jsp
ページに戻ってログアウトしようとしたが、同じファイル番号のステータスINに入る前にログアウトしようとしている私はこれをどうしたらいいですか? 誰かがいくつかの同様の例のリンクまたは類似のものも参考になります説明されているサイトを与えることができれば条件が満たされない場合のログアウトを防止する
status.jsp(本体のみ)
<body style="background-color:lightsteelblue;">
<%
String userName = null;
String sessionID = null;
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("user")) {
userName = cookie.getValue();
}
}
}
%>
<header>
<h3>File Tracking System</h3>
<div><span style="float:right">Hi <%=userName%></span></div>
<br>
</header>
<a href="create1.jsp"><font color="black">back</font></a>
<form action=" LogoutServlet" method="post">
<input type="submit" value="Logout" >
</form>
<nav>
<h3>Change Status</h3>
<form action="statusServlet" method="post">
<table>
<tbody>
<tr>
<td>
File Number :<select name="files">
<%
try {
String sql = "select * from files";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login",
"root", "root");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
%>
<option value="<%=rs.getString("fileno")%>"><%=rs.getString("fileno")%></option>
<%}
rs.close();
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</select></td>
</tr>
<tr>
<td>
File Department :<select name="departments">
<%
try {
String sql = "select * from department";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login",
"root", "root");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
%>
<option value="<%=rs.getString("departmentname")%>"><%=rs.getString("departmentname")%></option>
<%}
rs.close();
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</select></td>
</tr>
<tr>
<td>
File Status:
<br>
<select name="input">
<option>IN</option>
<option>OUT</option>
</select></td>
</tr>
<tr>
<td>
<input type="submit" value="submit" name="submit" />
</td>
</tr>
</tbody>
</table>
</form>
</nav>
<section><img src="css/NSIC-logo1.png" width="537" height="267" alt="NSIC-logo1"/>
</section>
<footer>
Copyright 2016 NSIC. All right reserved.
</footer>
</body>
statusServlet.java(のみ挿入する値)
public class statusServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("JSESSIONID")) {
System.out.println("JSESSIONID=" + cookie.getValue());
break;
}
}
}
HttpSession session = request.getSession(false);
System.out.println("User=" + session.getAttribute("user"));
if (session != null && session.getAttribute("user") != null) {
String user = (String) session.getAttribute("user");
boolean status = false;
try {
String fname = request.getParameter("files");
String departments = request.getParameter("departments");
String input = request.getParameter("input");
int i;
if (input.equals("IN")) {
i = 1;
} else {
i = 0;
}
Connection con = ConnectionProvider.getCon();
String sql = "insert into status(fname,fstatus,department) values (?,?,?) ";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, fname);
pstmt.setInt(2, i);
pstmt.setString(3, departments);
int rs = pstmt.executeUpdate();
if (rs > 0) {
if (input.equals("IN")) {
String sql1 = "update files set location='" + departments + "' where fileno='" + fname + "'";
PreparedStatement st = con.prepareStatement(sql1);
int rs1 = st.executeUpdate();
}
status = true;
}
} catch (Exception e) {
}
if (status) {
response.sendRedirect("inserted.jsp");
PrintWriter out = response.getWriter();
out.println("Values have been inserted," + user);
//out.flush();
} else {
PrintWriter out = response.getWriter();
out.println("failed");
response.sendRedirect("notinserted.jsp");
}
} else {
RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.html");
PrintWriter out = response.getWriter();
out.println("<font color=red>Either user name or password is wrong.</font>");
rd.include(request, response);
}
}
}
inserted.jsp(本体のみ)