使用IDEA建立一個JSP專案
首先建立一個專案:
選擇 Web Application
專案名稱:
然後點選Finish
建立好後如下:
到專案中去設定一下
新建一個lib資料夾
然後到這裡把lib資料夾選上:
選上
把lib作為jar包目錄
在勾上:
有時候,這裡沒有自動建立專案,這時就需要我們手動去建立(已建立請跳過)
然後選上我們的Test專案:
這裡有時候也沒有建立(已建立請跳過)
手動建立如下:
也是選擇Test專案
然後點ok
然後配置Tomcat 伺服器
如下:
新增後如下:
基本設定如下:
建立:
說明:此處的/test 是專案在 伺服器的 路徑即:localhost:8080/test
就是我們伺服器主頁的路徑
然後點ok
會自動建立如下:
然後我們開始碼程式碼
把jar包放進來
mysql jar包
EL jar包
JSTL jar包
然後建立幾個包,分別是dao、model、servlet、test
目錄結構:
由於太刺眼了 故換成了黑色主題…
建立了一個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); } } }
然後右擊執行:
好吧,它報錯了…
這裡打錯了
應該是 server ,而不是service
該過來以後就正確了
然後我們把sakila資料庫中film表中的資料查出來
把欄位 改成 類中的屬性
如下:
然後依次建立構造方法、get、set 、equals、hashcode、toStirng方法
開啟這個選單的快捷鍵是 Alte + Insert
現在的目錄結構如下:
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介面
FilmImpl類
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;
}
}
然後我們測試一下
然後我們可以看到,1000條資料已經出來了
接下來就是顯示到網頁上的事了
寫個servlet
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");
}
}
然後把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頁面了
結果: