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


     

    步骤1:先运行,看到效果,再学习
    步骤2:模仿和排错
    步骤3:编写 HeroUpdateServlet
    步骤4:配置web.xml

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

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

    步骤 2 : 模仿和排错

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

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

    步骤 3 : 编写 HeroUpdateServlet

    HeroUpdateServlet
    根据浏览器提交的id name hp damage创建一个Hero对象
    然后调用HeroDAO的update,进行更新

    更新结束后,客户端跳转到 /listHero

    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 HeroUpdateServlet extends HttpServlet {

        protected void service(HttpServletRequest request, HttpServletResponse response)

                throws ServletException, IOException {

            request.setCharacterEncoding("UTF-8");

            Hero hero = new Hero();

            hero.setId(Integer.parseInt(request.getParameter("id")));

            hero.setName(request.getParameter("name"));

            hero.setHp(Float.parseFloat(request.getParameter("hp")));

            hero.setDamage(Integer.parseInt(request.getParameter("damage")));

            new HeroDAO().update(hero);

            response.sendRedirect("/listHero");

        }

    }

    步骤 4 : 配置web.xml

    新增对路径 /updateHero的映射

    <?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>

        <servlet>

            <servlet-name>HeroUpdateServlet</servlet-name>

            <servlet-class>servlet.HeroUpdateServlet</servlet-class>

        </servlet>

        <servlet-mapping>

            <servlet-name>HeroUpdateServlet</servlet-name>

            <url-pattern>/updateHero</url-pattern>

        </servlet-mapping>

    </web-app>


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

  • 相关阅读:
    Cocos Creator JSZip压缩
    手游游戏资源提取 (破解、AssetStudio、VGMToolbox、disunity、Il2CppDumper、 .NET Reflector)
    Cocos Creator Cannot read property 'load' of null
    BOX2D物理游戏编程初学者指南+源码+FlashPlayer播放器
    [已解决]报错:XGBoostError: XGBoost Library (libxgboost.dylib) could not be loaded.
    [已解决]报错:pyecharts绘制图片时显示空白
    [未解决]yarn安装报错网络问题解决
    Mac Homebrew安装
    mac下docker镜像加速
    [已解决]报错:python3 geohash 导入错误
  • 原文地址:https://www.cnblogs.com/Lanht/p/12615403.html
Copyright © 2020-2023  润新知