2016-07-01 13 views
0

データベースのファイルのステータスデータを挿入するために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(本体のみ)

答えて

0
<body style="background-color:lightsteelblue;"> 
    <header>Data inserted!!</header> 
    <a href="fileStatus.jsp"><font color="black"> goback</font></a> 
    <form action=" LogoutServlet" method="post"> 
     <input type="submit" value="Logout" > 
    </form> 
    <footer> 
     Copyright 2008 NSIC. All right reserved.        
    </footer> 
</body>  

HttpSessionの前の選択を覚えて

// add following lines in 'doPost' method after session initialization in statusServlet 
String input = request.getParameter("input"); // you have wrote this one already 

if(input != null && (input.equals("IN") || input.equals("OUT"))){ 
    session.setAttribute("last-value", input); 
} 

と...

//wrap logout form in status.jsp like following: 

<% 
    String lastValue = session.getAttribute("last-value"); 
    if(lastValue != null && lastValue.equals("IN")){ 
%> 
     <form action=" LogoutServlet" method="post"> 
      <input type="submit" value="Logout" > 
     </form> 
<% 
    } 
%> 

を使用する:++私はこれをテストしていませんが、私はメモ帳でコードを書いた

をしてください他のヘルプが必要な場合はコメント

関連する問題