我有以下JSP页面,该页面查看servlet,获取用户名并将其返回到另一个jsp页面(Result.jsp)
<html>
<body>
<table>
<form name="setRuns" action="/servlet/setUserRuns" method="POST">
<tr>
<td>
<p>Username:</p>
<input type="text" placeholder="Enter Username.." name="user">
<button type="submit" id="myButton">Submit </button>
</form>
<br><br>
</td>
</tr>
</table>
</body>
</html>
以下是Result.jsp的代码
<html>
<body>
<table border="0" cellpadding="3" cellspacing="3" width="100%">
<form name="setRuns" action="/servlet/setUserRuns" method="POST">
<thead>
<tr style="background-color: #79b7e7;">
<th>UserName</th>
<th>End Date</th>
<th>Saving</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<%=session.getAttribute("userName")%>
</td>
<td><input type="date" name="EndDate"></td>
<td><input type="submit" name="save" value="Save"></td>
</tr>
</tbody>
</form>
</table>
</body>
</html>
以下代码用于servlet:
public class setUserRuns extends HttpServlet {
static Logger log = Logger.getLogger(OnAccountUsers.class.getName());
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
log.debug("doGet");
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// start db connection
Connection con = SubscribeConnectionFactory.getInstance().getConnection();
String clientName = request.getParameter("user");
ResultSet rs = null;
PreparedStatement stmt = null;
try {
String sql = "SELECT * FROM userdatabase.account inner join userdatabase.users on account.user = users.userName where userName = ?";
stmt = con.prepareStatement(sql);
stmt.setString(1, clientName); // setting user =?
rs = stmt.executeQuery(); // execute the query, and get a result set
boolean entryFound = false;
// iterate through the java result set
while (rs.next()) {
entryFound = true;
session.setAttribute("userName", clientName);
response.sendRedirect("/jsp/Result.jsp");
}
} catch (Exception e) {
// error
System.out.println("Error: " + e.getMessage());
} finally {
// close result set
SubscribeConnectionFactory.getInstance().closeResultSetAndStatement(rs, stmt);
// close connection
SubscribeConnectionFactory.getInstance().closeConnection(con);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
public void destroy() {
PooledDataSource.destroyPooledDataSources();
}
}
从servlet重定向页面后,我需要从Result.jsp中的日期持有者中选择一个日期,并在提交“保存”后将其更新到数据库中 并更新jsp页面中的日期。 无论如何,有没有办法通过相同的doGet做到这一点?这是我第一次使用servlet。任何帮助表示赞赏。谢谢。