项目基础——增删改查---删

项目基础——增删改查—删

在这里插入图片描述

package com.cvs.contoroller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cvs.dao.HonorDao;

public class DeleteHonorServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public DeleteHonorServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id=request.getParameter("id");
		HonorDao dao=new HonorDao();
		int id_del =Integer.parseInt(id); 
		dao.delHonor(id_del);
		response.sendRedirect("HonorServlet");
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
		}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

package com.cvs.contoroller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cvs.dao.HonorDao;
import com.cvs.model.Honor;

public class HonorServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public HonorServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HonorDao honorDao=new HonorDao();
		List<Honor> list= honorDao.findAll();
		request.setAttribute("list", list);
		request.getRequestDispatcher("honors.jsp").forward(request, response);
		
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

<%@ page language="java" pageEncoding="utf-8"%>
<%@page import="java.util.List"%>
<%@page import="com.cvs.model.Honor"%>
<%@taglib prefix="sx" uri="http://java.sun.com/jsp/jstl/core"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	</head>

	<body>

		<%
			List<Honor> list = (List<Honor>) request.getAttribute("list");
		%>
		<table>
			<caption>
				英雄管理系统
			</caption>

			<tr>
				<th>
					编号
				</th>
				<th>
					名称
				</th>
				<th>
					皮肤
				</th>
				<th>
					技能
				</th>
				<th>
					攻击力
				</th>
				<th>
					操作
				</th>
				<th></th>
				<th></th>
			</tr>

			<sx:forEach items="${list}" var="one">

				<tr>
					<td>
						${one.id}
					</td>
					<td>
						${one.name}
					</td>
					<td>
						${one.pifu}
					</td>
					<td>
						${one.jineng}
					</td>
					<td>
						${one.gongjili}
					</td>
					<td>
						${one.caozuo}
					</td>
					<td></td>
					<td></td>
					<td>
						<a href="DeleteHonorServlet?id=${one.id}">删除</a>
					</td>
				</tr>
			</sx:forEach>



		</table>



	</body>
</html>

发布了72 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/BOGEWING/article/details/103345369