我创建了 status.jsp
用于在数据库中插入文件的状态数据。状态有两个选项(IN 或 OUT)。提交后,它会转到 inserted.jsp
,其中显示已插入数据,并提供指向 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(仅正文)
<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>
<%
}
%>
注意:我没有对此进行测试,我只是在 Notepad++ 中编写了代码
如果您需要更多帮助,请评论
关于java - 如果不满足条件,则防止注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38139976/