@WebServlet(name = "C_AddServlet", value = "/C_AddServlet") public class C_AddServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String id_clazz = request.getParameter("id_clazz"); String college = request.getParameter("college"); String major = request.getParameter("major"); Integer grade = Integer.parseInt(request.getParameter("grade")); Integer no = Integer.parseInt(request.getParameter("no")); String id_teacher = request.getParameter("id_teacher"); Clazz clazz = new Clazz(id_clazz,college,major,grade,no,id_teacher); ClazzService clazzService = new ClazzService(); try { clazzService.save(clazz); } catch (Exception e) { if (e.getClass().equals(SQLIntegrityConstraintViolationException.class)){ System.out.println("该班级号已存在!"); }else { e.printStackTrace(); } } response.sendRedirect("ListServlet"); } }
时间: 2024-04-01 13:38:14 浏览: 162
这段代码是一个Java Servlet,用来处理添加班级的请求。具体来说,代码中的@WebServlet注解用来指定Servlet的名称和URL,这里的名称为C_AddServlet,URL为/C_AddServlet。
代码中的doGet()方法和doPost()方法分别用来处理GET请求和POST请求。这里的doGet()方法直接调用doPost()方法进行处理,因此这个Servlet只接受POST请求。
在doPost()方法中,代码首先设置请求的字符编码为utf-8,然后通过request.getParameter()方法获取表单中提交的各个参数,包括id_clazz(班级ID)、college(学院)、major(专业)、grade(年级)、no(班级号)和id_teacher(班主任ID)。
接着,代码将这些参数封装成一个Clazz对象,并将其保存到数据库中,具体的保存操作由ClazzService类的save()方法实现。
如果保存过程中出现异常,代码会判断异常的类型,如果是SQLIntegrityConstraintViolationException类型,说明班级号已经存在,代码会输出提示信息。否则,代码会打印异常的堆栈信息。
最后,代码通过response.sendRedirect()方法将请求重定向到ListServlet,从而显示班级列表。
相关问题
@WebServlet(name = "deleteServlet", value = "/deleteServlet") public class deleteServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request,response); } }在这个页面填好
这个 Servlet 是一个空实现,需要根据具体需求进行修改。以下是一个简单的删除操作的示例:
```java
@WebServlet(name = "deleteServlet", value = "/deleteServlet")
public class deleteServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
// 根据具体需求进行删除操作,以下是一个示例代码
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
String sql = "DELETE FROM user WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, id);
ps.executeUpdate();
response.sendRedirect("userList.jsp");
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, null);
}
}
}
```
该 Servlet 接收一个 POST 请求,从请求参数中获取一个 `id` 值,然后连接数据库,执行 SQL 语句删除对应的行,并重定向到用户列表页。需要注意的是,这里使用了一个 DBUtil 类来获取数据库连接和关闭资源,可以根据自己的情况进行修改。另外,需要将 SQL 语句中的 `user` 替换成实际的表名。
import jakarta.servlet.*; import jakarta.servlet.http.*; import jakarta.servlet.annotation.*; import java.io.IOException; @WebServlet(name = "ForwordServlet", value = "/ForwordServlet") public class ForwordServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setAttribute("usernamer","张三"); RequsetDispatcher dispatcher = request.getRequestDispatcher("/ResultServlet"); dispatcher.forward(request,response); } @Override protected void doPost这段代码的RequestDispacher错误怎么解决(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
这段代码中的错误在于"RequsetDispatcher"拼写错误,应为"RequestDispatcher"。正确的代码应该是:
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "ForwordServlet", value = "/ForwordServlet")
public class ForwordServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setAttribute("usernamer","张三");
RequestDispatcher dispatcher = request.getRequestDispatcher("/ResultServlet");
dispatcher.forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
阅读全文