代码改错:@WebServlet("/update_teacher") public class update_teacher extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); TeacherDao teacherD = new TeacherDao(); String uid = request.getParameter("uid"); String name = request.getParameter("name"); String sex = request.getParameter("sex"); String email = request.getParameter("email"); String password = request.getParameter("password"); try { Teacher teacher = teacherD.updateTeacher(uid, name, sex, email, password); session.setAttribute("info", teacher); out.print("<script>alert(\"保存成功!\");location.href = \"teacher/personal.jsp\";</script>"); } catch (Exception e) { out.print(e); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
时间: 2024-03-06 16:50:45 浏览: 172
这段代码看起来没有明显的错误,但是建议做一些改进:
1. 类名应该以大写字母开头,所以建议将类名 `update_teacher` 改为 `UpdateTeacher`。
2. 在 `catch` 块中,如果出现异常应该将信息打印到日志中而不是直接输出到页面,因为这样可以避免暴露敏感信息。
3. 如果更新教师信息失败,应该给出友好的提示信息。
下面是改进后的代码:
```
@WebServlet("/update_teacher")
public class UpdateTeacher extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
TeacherDao teacherDao = new TeacherDao();
String uid = request.getParameter("uid");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String email = request.getParameter("email");
String password = request.getParameter("password");
try {
Teacher teacher = teacherDao.updateTeacher(uid, name, sex, email, password);
session.setAttribute("info", teacher);
out.print("<script>alert(\"保存成功!\");location.href = \"teacher/personal.jsp\";</script>");
} catch (Exception e) {
Logger.getLogger(UpdateTeacher.class.getName()).log(Level.SEVERE, null, e);
out.print("<script>alert(\"保存失败,请稍后重试!\");location.href = \"teacher/personal.jsp\";</script>");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
```
阅读全文