欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVAEE 实训日志 一

程序员文章站 2022-07-06 15:30:10
文章目录1. 创建 web-app 项目导入初始4个 maven 依赖TestServlet.javatest.jsp测试:2. 连接数据库导入依赖 驱动编写连接类 JDBCUtil测试连接3. 开发一个简单的javaweb项目EntityService 层DaoController前端 test.jsp效果1. 创建 web-app 项目导入初始4个 maven 依赖 javax.servlet

1. 创建 web-app 项目

导入初始4个 maven 依赖

	<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp.jstl</groupId>
      <artifactId>jstl-api</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>

TestServlet.java

package com.beyond.nothing.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/test")
public class TestServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add("博瑞");

        // 将 list集合 放入request中
        req.setAttribute("list", list);
        // 转发到 test.jsp
        req.getRequestDispatcher("WEB-INF/jsp/test.jsp").forward(req, resp);

    }
}

test.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%--引入jstl标签库--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
测试:
<c:forEach items="${list}" var="str">
    ${str}
</c:forEach>
</body>
</html>

测试:

JAVAEE 实训日志 一

2. 连接数据库

导入依赖 驱动

 	<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.49</version>
    </dependency>

编写连接类 JDBCUtil

package com.beyond.nothing.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCUtil {
    private static final String USERNAME = "root";
    private static final String PWD = "210374520";
    private static final String URL = "jdbc:mysql://localhost:3308/thedemo";
    private static final String DRIVER = "com.mysql.jdbc.Driver";

    static {
        // 注册驱动
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    public static Connection getCoon() throws SQLException {
        return DriverManager.getConnection(URL,USERNAME,PWD);
    }


    public static void main(String[] args) throws SQLException {
        Connection coon = getCoon();
        System.out.println(coon);
    }
}

测试连接

JAVAEE 实训日志 一

3. 开发一个简单的javaweb项目

Entity

package com.beyond.nothing.entity;

public class Product {
    private Integer id;
    private String name;

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Service 层

package com.beyond.nothing.service;

import com.beyond.nothing.dao.TestDao;
import com.beyond.nothing.entity.Product;

import java.util.List;

public class TestService {
    private TestDao dao = new TestDao();

    public List<Product> queryList(){

        return dao.queryList();
    }

}

Dao

package com.beyond.nothing.dao;

import com.beyond.nothing.entity.Product;
import com.beyond.nothing.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TestDao {

    public List<Product> queryList() {
        Connection connection = null ;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        // 将结果转为 list
        List<Product> list = new ArrayList<>();

        try {
            // 获取 Connection 对象
            connection = JDBCUtil.getCoon();
            // 获取预编译对象
            preparedStatement = connection.prepareStatement("select * from product");
            // 执行查询,并且返回结果集
            resultSet = preparedStatement.executeQuery();

            // 迭代结果集
            while (resultSet.next()){
                Product product = new Product();
                product.setId(resultSet.getInt("id"));
                product.setName(resultSet.getString("name"));
                list.add(product);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(resultSet,preparedStatement,connection);
        }

        return list;
    }
}

Controller

package com.beyond.nothing.servlet;

import com.beyond.nothing.service.TestService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/test")
public class TestServlet extends HttpServlet {


    private TestService testService = new TestService();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 将 list集合 放入request中
        req.setAttribute("list", testService.queryList());
        // 转发到 test.jsp
        req.getRequestDispatcher("WEB-INF/jsp/test.jsp").forward(req, resp);

    }
}

前端 test.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%--引入jstl标签库--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
测试:
<c:forEach items="${list}" var="p">
    ${p.id}
    ${p.name}
</c:forEach>
</body>
</html>

效果

JAVAEE 实训日志 一

本文地址:https://blog.csdn.net/Beyond_Nothing/article/details/112570730