使用IDEA创建一个JSP项目

首先创建一个项目:
1

选择 Web Application
2

项目名称:
3

然后点击Finish
创建好后如下:
4

到项目中去设置一下
5

新建一个lib文件夹
6

然后到这里把lib文件夹选上:
7

选上
8

把lib作为jar包目录
9

在勾上:
10

有时候,这里没有自动创建项目,这时就需要我们手动去创建(已创建请跳过)
11

12

然后选上我们的Test项目:
13

这里有时候也没有创建(已创建请跳过)
14

手动创建如下:

15

也是选择Test项目
16

然后点ok

然后配置Tomcat 服务器
17

如下:
18

添加后如下:
19

基本设置如下:
20

创建:
21

22

说明:此处的/test 是项目在 服务器的 路径即:localhost:8080/test就是我们服务器主页的路径

然后点ok

会自动创建如下:
23

然后我们开始码代码

把jar包放进来
31

mysql jar包
EL jar包
JSTL jar包

然后创建几个包,分别是dao、model、servlet、test
32

目录结构:
33

由于太刺眼了 故换成了黑色主题…

创建了一个BaseDao类

package dao;

import java.sql.*;

public class BaseDao {

    public Connection getCN8() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila" +
                "?useSSL=false&useUnicode=true&characterEncoding=utf-8&server Timezone=UTC", "root", "123456");
    }

    public void closeAll(Connection cn, Statement st, ResultSet rs) {
        if (cn != null) {
            try {
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

然后创建了一个test类

package test;

import dao.BaseDao;

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

public class Test {
    public static void main(String[] args) {
        BaseDao b = new BaseDao();

        Connection cn = null;

        try {
            cn = b.getCN8();
            if (cn != null) {
                System.out.println("yes");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            b.closeAll(cn, null, null);
        }

    }
}

然后右击运行:
34

好吧,它报错了…
35

这里打错了
36

应该是 server ,而不是service
该过来以后就正确了
37

然后我们把sakila数据库中film表中的数据查出来

把字段 改成 类中的属性
38

如下:
39

然后依次创建构造方法、get、set 、equals、hashcode、toStirng方法
40

打开这个菜单的快捷键是 Alte + Insert

现在的目录结构如下:
41

Film类

package model;

import java.io.Serializable;
import java.util.Objects;

public class Film implements Serializable {

    private String filmId,title,description,releaseYear,languageId,originalLanguageId,rentalDuration,rentalRate
            ,length,replacementCost,rating,specialFeatures,lastUpdate;

    public Film() {
    }

    public Film(String title, String description, String releaseYear, String languageId, String originalLanguageId, String rentalDuration, String rentalRate, String length, String replacementCost, String rating, String specialFeatures, String lastUpdate) {
        this.title = title;
        this.description = description;
        this.releaseYear = releaseYear;
        this.languageId = languageId;
        this.originalLanguageId = originalLanguageId;
        this.rentalDuration = rentalDuration;
        this.rentalRate = rentalRate;
        this.length = length;
        this.replacementCost = replacementCost;
        this.rating = rating;
        this.specialFeatures = specialFeatures;
        this.lastUpdate = lastUpdate;
    }

    public Film(String filmId, String title, String description, String releaseYear, String languageId, String originalLanguageId, String rentalDuration, String rentalRate, String length, String replacementCost, String rating, String specialFeatures, String lastUpdate) {
        this.filmId = filmId;
        this.title = title;
        this.description = description;
        this.releaseYear = releaseYear;
        this.languageId = languageId;
        this.originalLanguageId = originalLanguageId;
        this.rentalDuration = rentalDuration;
        this.rentalRate = rentalRate;
        this.length = length;
        this.replacementCost = replacementCost;
        this.rating = rating;
        this.specialFeatures = specialFeatures;
        this.lastUpdate = lastUpdate;
    }

    public String getFilmId() {
        return filmId;
    }

    public Film setFilmId(String filmId) {
        this.filmId = filmId;
        return this;
    }

    public String getTitle() {
        return title;
    }

    public Film setTitle(String title) {
        this.title = title;
        return this;
    }

    public String getDescription() {
        return description;
    }

    public Film setDescription(String description) {
        this.description = description;
        return this;
    }

    public String getReleaseYear() {
        return releaseYear;
    }

    public Film setReleaseYear(String releaseYear) {
        this.releaseYear = releaseYear;
        return this;
    }

    public String getLanguageId() {
        return languageId;
    }

    public Film setLanguageId(String languageId) {
        this.languageId = languageId;
        return this;
    }

    public String getOriginalLanguageId() {
        return originalLanguageId;
    }

    public Film setOriginalLanguageId(String originalLanguageId) {
        this.originalLanguageId = originalLanguageId;
        return this;
    }

    public String getRentalDuration() {
        return rentalDuration;
    }

    public Film setRentalDuration(String rentalDuration) {
        this.rentalDuration = rentalDuration;
        return this;
    }

    public String getRentalRate() {
        return rentalRate;
    }

    public Film setRentalRate(String rentalRate) {
        this.rentalRate = rentalRate;
        return this;
    }

    public String getLength() {
        return length;
    }

    public Film setLength(String length) {
        this.length = length;
        return this;
    }

    public String getReplacementCost() {
        return replacementCost;
    }

    public Film setReplacementCost(String replacementCost) {
        this.replacementCost = replacementCost;
        return this;
    }

    public String getRating() {
        return rating;
    }

    public Film setRating(String rating) {
        this.rating = rating;
        return this;
    }

    public String getSpecialFeatures() {
        return specialFeatures;
    }

    public Film setSpecialFeatures(String specialFeatures) {
        this.specialFeatures = specialFeatures;
        return this;
    }

    public String getLastUpdate() {
        return lastUpdate;
    }

    public Film setLastUpdate(String lastUpdate) {
        this.lastUpdate = lastUpdate;
        return this;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Film)) return false;
        Film film = (Film) o;
        return Objects.equals(getFilmId(), film.getFilmId()) &&
                Objects.equals(getTitle(), film.getTitle()) &&
                Objects.equals(getDescription(), film.getDescription()) &&
                Objects.equals(getReleaseYear(), film.getReleaseYear()) &&
                Objects.equals(getLanguageId(), film.getLanguageId()) &&
                Objects.equals(getOriginalLanguageId(), film.getOriginalLanguageId()) &&
                Objects.equals(getRentalDuration(), film.getRentalDuration()) &&
                Objects.equals(getRentalRate(), film.getRentalRate()) &&
                Objects.equals(getLength(), film.getLength()) &&
                Objects.equals(getReplacementCost(), film.getReplacementCost()) &&
                Objects.equals(getRating(), film.getRating()) &&
                Objects.equals(getSpecialFeatures(), film.getSpecialFeatures()) &&
                Objects.equals(getLastUpdate(), film.getLastUpdate());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getFilmId(), getTitle(), getDescription(), getReleaseYear(), getLanguageId(), getOriginalLanguageId(), getRentalDuration(), getRentalRate(), getLength(), getReplacementCost(), getRating(), getSpecialFeatures(), getLastUpdate());
    }

    @Override
    public String toString() {
        return "<tr><td>" + filmId +
                "</td><td>" + title +
                "</td><td>" + description +
                "</td><td>" + releaseYear +
                "</td><td>" + languageId +
                "</td><td>" + originalLanguageId +
                "</td><td>" + rentalDuration +
                "</td><td>" + rentalRate +
                "</td><td>" + length +
                "</td><td>" + replacementCost +
                "</td><td>" + rating +
                "</td><td>" + specialFeatures +
                "</td><td>" + lastUpdate +
                "</td></tr>";
    }
}

然后再创建一个IFilmDao接口
42

FilmImpl类
43

package dao.impl;

import dao.BaseDao;
import dao.IFilmDao;
import model.Film;

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

public class FilmImpl extends BaseDao implements IFilmDao {
    @Override
    public List<Film> selectAllFilm() {
        String sql = "select * from film;";
        ArrayList<Film> arrayList = new ArrayList<>();
        Film f = null;

        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;

        try {
            cn = getCN8();
            st = cn.createStatement();
            rs = st.executeQuery(sql);

            while (rs.next()) {
                f = new Film(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5)
                        , rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10)
                        , rs.getString(11), rs.getString(12), rs.getString(13));
                arrayList.add(f);
            }

        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeAll(cn, st, rs);
        }

        return arrayList;
    }
}

然后我们测试一下

44

然后我们可以看到,1000条数据已经出来了

接下来就是显示到网页上的事了

写个servlet
45

package servlet;

import dao.impl.FilmImpl;

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;

@WebServlet("/showfilm")
public class ShowFilmServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("list",new FilmImpl().selectAllFilm());
        resp.sendRedirect("index.jsp");
    }
}

46

然后把bootstrap也丢进来

网页:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/12/8/008
  Time: 下午 2:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>显示</title>
    <link type="text/css" href="bootstrap.css" rel="stylesheet">
</head>
<body>
<table class="table table-hover">
    <tr>
        <td>电影编号</td>
        <td>电影名称</td>
        <td>简介</td>
        <td>发布年份</td>
        <td>语言</td>
        <td>原始语言</td>
        <td>租赁期限</td>
        <td>租金率</td>
        <td>长度</td>
        <td>重置成功</td>
        <td>评级</td>
        <td>特殊功能</td>
        <td>最后一次更新</td>
    </tr>

    <c:forEach var="f" items="${list}">
        ${f.toString()}
    </c:forEach>

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

在浏览器中输入:http://localhost:8080/test/showfilm就可以看到jsp页面了

结果:
47

猜你喜欢

转载自blog.csdn.net/a145127/article/details/84893355