JavaWeb、JSP

本文深入介绍了JSP的概念、快速入门、工作原理及脚本使用,展示了如何动态显示网页内容。同时,讨论了JSP的缺点,详细讲解了EL表达式和JSTL标签,特别是`foreach`标签的运用。接着,文章探讨了MVC模式和三层架构,并提供了一个使用三层架构实现增删改查功能的案例,涵盖了Dao/Mapper、Service和Web层的具体实现。
摘要由CSDN通过智能技术生成

目录

一、JSP概念

二、JSP快速入门

三、JSP原理

四、JSP脚本

使用JSP脚本动态显示网页

五、JSP的缺点

六、EL表达式

七、JSTL标签

 1、JSTL快速入门

 2、foreach标签

八、MVC模式和三层架构

九、案例:使用三层架构模式完成增删改查

1、准备环境

2、查询所有

1、 Dao/Mapper层

2、Service层

3、Web层

3、添加数据

1、Dao/Mapper层

2、Service层

3、Servlet层

4、修改数据

设置数据回显

1、Dao/Mapper层

2、Service层

3、Servlet层

4、点击修改获取商品id的方法

修改数据

1、Dao/Mapper层

2、Service层

3、Servlet层

5、删除数据

1、Dao/Mapper层

2、Service层

3、Servlet层


一、JSP概念

二、JSP快速入门

三、JSP原理

四、JSP脚本

<%
        /*底层直接写到_jspService方法之中 方法中能写什么就能写什么*/
        System.out.println("hello jsp");
        int i = 3;
    %>

    <%=
        /*作为out.print的参数,直接输出*/
        "hello"
    %>
    <%=i%>

    <%!
        /*底层写到_jspService方法之外,被类直接包含,类中能写什么就能写什么*/
        String name = "zhangsan";
        void show(){}
    %>

使用JSP脚本动态显示网页

<%@ page import="com.itheima.pojo.Brand" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Arrays" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    //准备数据
    //集合第二个<>内必须写类型否则报错
    List<Brand> brandLists = new ArrayList<Brand>();
    brandLists.add(new Brand(0,"三只松鼠","三只松鼠有限公司",100,"好吃不上火",1));
    brandLists.add(new Brand(1,"华为","华为有限公司",5,"中国好品牌",0));
    brandLists.add(new Brand(2,"小米","小米手机有限公司",10,"小米为发烧而生",1));

%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="button" value="新增"><br>
<hr>
<table border="1" cellspacing="0" width="800">
    <tr>
        <th>序号</th>
        <th>品牌名称</th>
        <th>企业名称</th>
        <th>排序</th>
        <th>品牌介绍</th>
        <th>状态</th>
        <th>操作</th>
    </tr>
    <%--使用循环将数据库中的数据展示--%>
    <%
        for (int i = 0; i < brandLists.size(); i++) {
            Brand brand = brandLists.get(i);
    %>
    <%--将java代码截断,中间写html--%>
    <tr align="center">
        <td><%=brand.getId()%></td>
        <td><%=brand.getBrandName()%></td>
        <td><%=brand.getCompanyName()%></td>
        <td><%=brand.getOrdered()%></td>
        <td><%=brand.getDescription()%></td>

        <%
            if (brand.getStatus() == 1){
                //启用
        %>
        <td><%="启用"%></td>
        <%
            }else {
                //禁用
        %>
        <td><%="禁用"%></td>
        <%
            }
        %>
        <td><a href="#">修改</a> <a href="#">删除</a></td>
    </tr>
    <%
        }
    %>

</table>
</body>
</html>

五、JSP的缺点

六、EL表达式

<% EL表达式一定要在这里声明isELIgnored="false" %>
<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%--接收数据--%>
    ${brandLists}
</body>
</html>
@WebServlet("/demo1")
public class ServletDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取数据
        List<Brand> brandLists = new ArrayList<Brand>();
        brandLists.add(new Brand(0,"三只松鼠","三只松鼠有限公司",100,"好吃不上火",1));
        brandLists.add(new Brand(1,"华为","华为有限公司",5,"中国好品牌",0));
        brandLists.add(new Brand(2,"小米","小米手机有限公司",10,"小米为发烧而生",1));

        //将获取到的数据存入requst域中
        req.setAttribute("brandLists",brandLists);

        //转发到动态页面
        req.getRequestDispatcher("/el-demo.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

七、JSTL标签

 1、JSTL快速入门

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <%--c:if 判断语句--%>
    <c:if test="true">
        <h1>TRUE</h1>
    </c:if>

</body>
</html>

 2、foreach标签

<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="button" value="新增"><br>
<hr>
<table border="1" cellspacing="0" width="800">
    <tr>
        <th>序号</th>
        <th>品牌名称</th>
        <th>企业名称</th>
        <th>排序</th>
        <th>品牌介绍</th>
        <th>状态</th>
        <th>操作</th>
    </tr>

    <%--foreach遍历数据--%>
    <c:forEach items="${brands}" var="brand" varStatus="status">
        <tr align="center">
                <%--大括号内会对数据进行处理,变成getId,然后在brand中获取数据--%>
            <%--使用varStatus设置连续的序号代替id属性--%>
            <td>${status.count}</td>
            <td>${brand.brandName}</td>
            <td>${brand.companyName}</td>
            <td>${brand.ordered}</td>
            <td>${brand.description}</td>
            <c:if test="${brand.status == 1}">
                <td>启用</td>
            </c:if>
            <c:if test="${brand.status != 1}">
                <td>禁用</td>
            </c:if>

            <td><a href="#">修改</a> <a href="#">删除</a></td>
        </tr>
    </c:forEach>

</table>
</body>
</html>

八、MVC模式和三层架构

九、案例:使用三层架构模式完成增删改查

1、准备环境

2、查询所有

1、 Dao/Mapper层

public interface BrandMapper {

    List<Brand> selectAll();

}


<mapper namespace="com.itheima.mapper.BrandMapper">


    <select id="selectAll" resultType="com.itheima.pojo.Brand">
        select * from tb_brand;
    </select>
</mapper>

2、Service层

public class BrandService {
    //只需要创建一个会话工厂
    static SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 查询所有
     * @return
     */
    public static List<Brand> selectAll(){

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        List<Brand> brandList = mapper.selectAll();

        sqlSession.close();

        return brandList;
    }
}

3、Web层

@WebServlet("/select")
public class SelectAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //调用service查询
        List<Brand> brandList = BrandService.selectAll();

        //将数据存入request域中
        req.setAttribute("brands",brandList);

        //转发到index.jsp
        req.getRequestDispatcher("/select.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

3、添加数据

1、Dao/Mapper层

public interface BrandMapper {
    
    List<Brand> selectAll();
    
    void add(Brand brand);
}

 <insert id="add">
        insert into tb_brand(brandName,companyName,ordered,description,status)
        values
        (#{brandName},#{companyName},#{ordered},#{description},#{status});
    </insert>

2、Service层

public static void add(Brand brand){

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.add(brand);

        //增加操作要提交事务
        sqlSession.commit();

        sqlSession.close();
    }

3、Servlet层

@WebServlet("/add")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理post请求乱码问题
        req.setCharacterEncoding("UTF-8");

        Brand brand = new Brand();
        brand.setBrandName(req.getParameter("brandName"));
        brand.setCompanyName(req.getParameter("companyName"));
        brand.setDescription(req.getParameter("description"));
        brand.setOrdered(Integer.parseInt(req.getParameter("ordered")));
        brand.setStatus(Integer.parseInt(req.getParameter("status")));

        //调用servise添加方法
        BrandService.add(brand);

        //转发到selectServlet
        req.getRequestDispatcher("/select").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

4、修改数据

设置数据回显

点击修改后要通过商品id将数据回显给用户

1、Dao/Mapper层

Brand selectById(int id);

    <!--select 必须声明resultType-->
    <select id="selectById" resultType="com.itheima.pojo.Brand">
        select * from tb_brand where id=#{id};
    </select>

2、Service层

public static Brand selectById(int id){
        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        Brand brand = mapper.selectById(id);

        sqlSession.close();

        return brand;
    }

3、Servlet层

@WebServlet("/select")
public class SelectAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //调用service查询
        List<Brand> brandList = BrandService.selectAll();

        //将数据存入request域中
        req.setAttribute("brands",brandList);

        //转发到index.jsp
        req.getRequestDispatcher("/select.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

4、点击修改获取商品id的方法

使用get方式将id提交给Servlet,注意将数据回显后还要将对应实体类的id也要获取到页面上,使用input的hidden类型,将id隐藏,方便update的service方法获取商品对应的id,执行对应的SQL语句

 <td><a href="/jspcase/selectById?id=${brand.id}">修改</a> <a href="#">删除</a></td>

修改数据

1、Dao/Mapper层

void update(Brand brand);

<update id="update">
        update tb_brand
        set
        brandName=#{brandName},companyName=#{companyName},ordered=#{ordered},description=#{description},status=#{status}
        where
        id = #{id};
    </update>

2、Service层

public static void update(Brand brand){
        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.update(brand);

        //修改增加都要提交事务
        sqlSession.commit();

        sqlSession.close();
    }

3、Servlet层

@WebServlet("/update")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理post请求乱码问题
        req.setCharacterEncoding("UTF-8");


        Brand brand = new Brand();
        //获取页面上的隐藏id值
        brand.setId(Integer.parseInt(req.getParameter("id")));
        brand.setBrandName(req.getParameter("brandName"));
        brand.setCompanyName(req.getParameter("companyName"));
        brand.setDescription(req.getParameter("description"));
        brand.setOrdered(Integer.parseInt(req.getParameter("ordered")));
        brand.setStatus(Integer.parseInt(req.getParameter("status")));

        //调用servise修改方法
        BrandService.update(brand);

        req.setAttribute("brand",brand);

        //转发到selectServlet
        req.getRequestDispatcher("/select").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}

5、删除数据

1、Dao/Mapper层

void delete(int id);

<delete id="delete">
        delete from tb_brand where id=#{id};
    </delete>

2、Service层

public static void delete(int id){
        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.delete(id);
        
        //对数据库的增删改都要提交事务
        sqlSession.commit();

        sqlSession.close();
    }

3、Servlet层

@WebServlet("/delete")
public class DeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");

        BrandService.delete(Integer.parseInt(id));

        req.getRequestDispatcher("/select").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值