import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class Update extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { HttpSession session = request.getSession(true) ; DBSession dbs = (DBSession) session.getAttribute("dbs") ; Student curr = (Student) session.getAttribute("curr") ; if(dbs == null || curr == null) { response.sendError(HttpServletResponse.SC_OK, "Session timed out. Please select again.") ; return ; } // Update record database String login = request.getParameter("login") ; String lastname = request.getParameter("lastname") ; String firstnames = request.getParameter("firstnames") ; String email = request.getParameter("email") ; String dept = request.getParameter("dept") ; dbs.stat.executeUpdate("UPDATE " + DBConstants.table + " SET " + "login='" + login + "', " + "lastname='" + lastname + "', " + "firstnames='" + firstnames + "', " + "email='" + email + "', " + "dept='" + dept + "' " + "WHERE login='" + curr.login + "'") ; // Build a command to undo the successful update, and // add it to the undo list. String sql = "UPDATE " + DBConstants.table + " SET " + "login='" + curr.login + "', " + "lastname='" + curr.lastname + "', " + "firstnames='" + curr.firstnames + "', " + "email='" + curr.email + "', " + "dept='" + curr.dept + "' " + "WHERE login='" + login + "'" ; Vector undoList = (Vector) session.getAttribute("undoList") ; if(undoList == null) { undoList = new Vector() ; session.setAttribute("undoList", undoList) ; } undoList.addElement(sql) ; // Redirect browser back to selection page response.sendRedirect( response.encodeRedirectURL("/students/servlet/Select")); } catch (SQLException e) { response.sendError(HttpServletResponse.SC_OK, "SQL error: " + e.getMessage()) ; } } }