- 修改HeroListServlet
新增加一列 edit,里面放上指向 /editHero的超链
package servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bean.Hero; import dao.HeroDAO; public class HeroListServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); List<Hero> heros = new HeroDAO().list(); StringBuffer sb = new StringBuffer(); sb.append("<table align='center' border='1' cellspacing='0'>\r\n"); sb.append("<tr><td>id</td><td>name</td><td>hp</td><td>damage</td><td>edit</td><td>delete</td></tr>\r\n"); String trFormat = "<tr><td>%d</td><td>%s</td><td>%f</td><td>%d</td><td><a href='editHero?id=%d'>edit</a></td><td><a href='deleteHero?id=%d'>delete</a></td></tr>\r\n"; for (Hero hero : heros) { String tr = String.format(trFormat, hero.getId(), hero.getName(), hero.getHp(), hero.getDamage(),hero.getId(),hero.getId()); sb.append(tr); } sb.append("</table>"); response.getWriter().write(sb.toString()); } }
- 配置web.xml
在web.xml中把 /editHero路径映射到 HeroEditServlet类上
<?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>HelloServlet</servlet-class> <load-on-startup>10</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <servlet> <servlet-name>RegisterServlet</servlet-name> <servlet-class>RegisterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RegisterServlet</servlet-name> <url-pattern>/register</url-pattern> </servlet-mapping> <servlet> <servlet-name>HeroListServlet</servlet-name> <servlet-class>servlet.HeroListServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HeroListServlet</servlet-name> <url-pattern>/listHero</url-pattern> </servlet-mapping> <servlet> <servlet-name>HeroAddServlet</servlet-name> <servlet-class>servlet.HeroAddServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HeroAddServlet</servlet-name> <url-pattern>/addHero</url-pattern> </servlet-mapping> <servlet> <servlet-name>HeroDeleteServlet</servlet-name> <servlet-class>servlet.HeroDeleteServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HeroDeleteServlet</servlet-name> <url-pattern>/deleteHero</url-pattern> </servlet-mapping> <servlet> <servlet-name>HeroEditServlet</servlet-name> <servlet-class>servlet.HeroEditServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HeroEditServlet</servlet-name> <url-pattern>/editHero</url-pattern> </servlet-mapping> </web-app>
- 准备HeroEditServlet
HeroEditServlet 根据浏览器传过来的id获取一个hero对象
然后根据这个hero对象,准备一个类似add.html的页面,不同之处在于每个输入框都是有值的。
最后还会提供一个type="hidden"的input,用于提交id到路径/updateHero
package servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bean.Hero; import dao.HeroDAO; public class HeroEditServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id = Integer.parseInt(request.getParameter("id")); Hero hero = new HeroDAO().get(id); StringBuffer format = new StringBuffer(); response.setContentType("text/html; charset=UTF-8"); format.append("<!DOCTYPE html>"); format.append("<form action='updateHero' method='post'>"); format.append("名字 : <input type='text' name='name' value='%s' > <br>"); format.append("血量 : <input type='text' name='hp' value='%f' > <br>"); format.append("伤害: <input type='text' name='damage' value='%d' > <br>"); format.append("<input type='hidden' name='id' value='%d'>"); format.append("<input type='submit' value='更新'>"); format.append("</form>"); String html = String.format(format.toString(), hero.getName(), hero.getHp(), hero.getDamage(), hero.getId()); response.getWriter().write(html); } }
Servlet CRUD-Servlet提供编辑功能
最新推荐文章于 2023-01-12 13:01:18 发布