如果未满足条件,则禁止注销

时间:2016-07-01 07:53:11

标签: java mysql jsp

我创建status.jsp用于在数据库中插入文件的状态数据。状态有两个选项(IN或OUT)。提交它会转到inserted.jsp,表示已插入数据,并为status.jsp提供goback链接以插入更多或注销。 我希望用户无法注销,除非他选择了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(仅限正文)

<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>      

1 个答案:

答案 0 :(得分:0)

使用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>
<%
    }
%>

注意:我没有测试过这个,我只是在Notepad ++中编写代码

如果您需要更多帮助,请发表评论