这段代码使用 @WebServlet 注释来将该 Servlet 映射到“/welcome”路径。在 doPost() 方法中,根据请求参数“action”的值,执行不同的操作,分别对应添加、删除和修改机器人信息。

代码首先通过 HttpServletRequest 对象获取请求参数,包括机器人名称、重量、尺寸等。然后,使用 DBConnected 类连接数据库,并使用 PreparedStatement 对象预编译 SQL 语句。根据不同的操作,执行不同的 SQL 语句,包括插入数据、删除数据和更新数据。

最后,代码使用 HttpServletResponse 对象重定向到 welcome.jsp 页面。

这段代码运用了 doPost() 方法,以及通过 HttpServletRequest 和 HttpServletResponse 对象获取参数和重定向页面的方法。还有一些其他的方法,例如 getConnection() 方法用于建立数据库连接,prepareStatement() 方法用于预编译 SQL 语句,executeUpdate() 方法用于执行更新操作,close() 方法用于关闭数据库连接等。

代码示例:

@WebServlet({"/welcome"})
public class Robot extends HttpServlet {
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		String action = req.getParameter("action");

		if ("addRobot".equals(action)) {
			
			RobotInfo robot =new RobotInfo();
			robot.setNAME(req.getParameter("robotname"));
			robot.setweight(req.getParameter("robotweight"));
			robot.setsize(req.getParameter("robotsize"));
			PreparedStatement pstmt = null ;   
			DBConnected dbc = null;
			String sql ="INSERT INTO `javawebdb`.`robot` (`ID`, `NAME`, `WEIGHT`, `SIZE`, `IMAGE`) VALUES (NULL,?,?,?,NULL)";
			try{   
				
				dbc = new DBConnected() ;   
				pstmt = dbc.getConnection().prepareStatement(sql) ; 
				pstmt.setString(1,robot.getNAME());
				pstmt.setString(2,robot.getweight());
				pstmt.setString(3,robot.getsize());
				
				int rs = pstmt.executeUpdate();
				  
				pstmt.close() ;
				res.sendRedirect("./welcome.jsp");
			}catch (SQLException e){   
				System.out.println(e.getMessage());   
			}finally{   
				
				dbc.close() ;   
			}
		} else if ("deleteRobot".equals(action)) {
			 
			RobotInfo robot =new RobotInfo();
			String ID =req.getParameter("robotID");
			PreparedStatement pstmt = null ;   
			DBConnected dbc = null;
			String sql ="DELETE FROM Robot WHERE id = ?";
			;
			try{   
				 
				dbc = new DBConnected() ;   
				pstmt = dbc.getConnection().prepareStatement(sql) ; 
				pstmt.setString(1,ID);
				
				int rs = pstmt.executeUpdate();
				  
				pstmt.close() ; 
				res.sendRedirect("./welcome.jsp");
			}catch (SQLException e){   
				System.out.println(e.getMessage());   
			}finally{   
				 
				dbc.close() ;   
			}
		} else if ("changeRobot".equals(action)) {
			RobotInfo robot =new RobotInfo();
			String ID =req.getParameter("robotID1");
			robot.setNAME(req.getParameter("robotname1"));
			robot.setweight(req.getParameter("robotweight1"));
			robot.setsize(req.getParameter("robotsize1"));
			PreparedStatement pstmt = null ;   
			DBConnected dbc = null;
			String sql ="UPDATE Robot SET NAME = ?, WEIGHT = ?, SIZE=? WHERE id =?";
			;
			try{   
				  
				dbc = new DBConnected() ;   
				pstmt = dbc.getConnection().prepareStatement(sql) ; 
				pstmt.setString(1,robot.getNAME());
				pstmt.setString(2,robot.getweight());
				pstmt.setString(3,robot.getsize());
				pstmt.setString(4, ID);
				
				int rs = pstmt.executeUpdate();
				pstmt.close() ; 
				res.sendRedirect("./welcome.jsp");
			}catch (SQLException e){   
				System.out.println(e.getMessage());   
			}finally{   
				
				dbc.close() ;
			
			}
			 }
	}
}
Java Web Servlet - 添加、删除和修改机器人信息

原文地址: https://www.cveoy.top/t/topic/pde9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录