• Servlet系列教材 (二十二)- CRUD


     

    步骤1:先运行,看到效果,再学习
    步骤2:模仿和排错
    步骤3:修改HeroListServlet
    步骤4:配置web.xml
    步骤5:准备HeroEditServlet

    步骤 1 : 先运行,看到效果,再学习

    老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。

    步骤 2 : 模仿和排错

    在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 
    模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 
    采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 

    推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 
    这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 
    这里提供了绿色安装和使用教程:diffmerge 下载和使用教程

    步骤 3 : 修改HeroListServlet

    新增加一列 edit,里面放上指向 /editHero的超链

    修改HeroListServlet

    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'> ");

            sb.append("<tr><td>id</td><td>name</td><td>hp</td><td>damage</td><td>edit</td><td>delete</td></tr> ");

            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> ";

            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());

        }

    }

    步骤 4 : 配置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>

    步骤 5 : 准备HeroEditServlet

    HeroEditServlet 根据浏览器传过来的id获取一个hero对象

    然后根据这个hero对象,准备一个类似add.html的页面,不同之处在于每个输入框都是有值的。

    最后还会提供一个type="hidden"的input,用于提交id到路径/updateHero

    准备HeroEditServlet

    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);

        }

    }


    更多内容,点击了解: https://how2j.cn/k/servlet/servlet-edit/566.html

  • 相关阅读:
    08 正则表达式
    07 函数&对象
    06 Math&Date&Json
    05 数组&字符串
    04 循环控制
    03 流程控制
    02 数据类型&运算符
    大道至简
    Avg_row_length是怎么计算的?
    理解innodb buffer pool
  • 原文地址:https://www.cnblogs.com/Lanht/p/12615404.html
Copyright © 2020-2023  润新知