实训笔记——JSP项目的整体结构

Posted _23_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实训笔记——JSP项目的整体结构相关的知识,希望对你有一定的参考价值。

基于书籍管理系统

整体目录:

在这里插入图片描述

代码

jsp代码

路径:/WebContent/…

BookList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书展示</title>
</head>
<body>
	<h3>全部书籍</h3>
	<table border="1">
		<tr>
			<td>编号</td>
			<td>书名</td>
			<td>作者</td>
			<td>价格</td>
			<td>操作</td>
		</tr>
		<c:forEach items="${Model}" var="book">
		<tr>
			<td>${book.id}</td>
			<td>${book.name}</td>
			<td>${book.author}</td>
			<td>${book.price}</td>
			<td>操作</td>
		</tr>
		</c:forEach>
	</table>
	<a href="AddBook.jsp">增加</a>
</body>
</html>

AddBook.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>书籍增加</title>
</head>
<body>
	<h3>添加书籍</h3>
	<form action="AddBookServlet" method="post">
		书名:<input type="text" name="name"><br>
		作者:<input type="text" name="author"><br>
		价格:<input type="text" name="price"><br>
		<input type="submit" value="save"/>
	</form>
</body>
</html>

AddSuc.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>书籍增加成功页面</title>
</head>
<body>
	添加书籍成功!
	
	<a href="BookListServlet"> 点击跳转图书展示页面!  </a>
</body>
</html>

java代码

Bean(Book.java)

package com.gg.demo2.pojo;

public class Book {
	private long id;
	private String name;
	private String author;
	private float price;
	
	public Book() {
		super();
	}
	
	public Book(long id, String name, String author, float price) {
		super();
		this.id = id;
		this.name = name;
		this.author = author;
		this.price = price;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	
}

servlet

BookListServlet.java

package com.gg.demo2.servlet;

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

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 com.gg.demo2.pojo.Book;

/**
 * Servlet implementation class BookListServlet
 */
@WebServlet("/BookListServlet")
public class BookListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Book> books=new ArrayList<Book>();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/bms?useSSL=false";
			Connection conn=DriverManager.getConnection(url,"root","root");
			String sql="select id,name,author,price from book";
			PreparedStatement pstmt=conn.prepareStatement(sql);		
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()) {
				Book temp=new Book();
				temp.setId(rs.getLong(1));
				temp.setName(rs.getString(2));
				temp.setAuthor(rs.getString(3));
				temp.setPrice(rs.getFloat(4));
				books.add(temp);
			}
			pstmt.close();
			conn.close();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("Model", books);
		request.getRequestDispatcher("BookList.jsp").forward(request, response);
	}

}

AddBookServlet.java

package com.gg.demo2.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class AddBookServlet
 */
@WebServlet("/AddBookServlet")
public class AddBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddBookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取页面传递过来的数据
		request.setCharacterEncoding("utf-8");
		String name=request.getParameter("name");
		String author=request.getParameter("author");
		float price=Float.parseFloat(request.getParameter("price"));
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/bms";
			Connection conn=DriverManager.getConnection(url,"root","root");
			String sql="insert into book (name,author,price) values (?,?,?)";
			PreparedStatement pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, name);
			pstmt.setObject(2, author);
			pstmt.setObject(3, price);
			pstmt.executeUpdate();
			pstmt.close();
			conn.close();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		response.sendRedirect("AddSuc.jsp");
	}

}

以上是关于实训笔记——JSP项目的整体结构的主要内容,如果未能解决你的问题,请参考以下文章

项目实训—基于AI的智能视频剪辑器项目架构搭建

JSP笔记

Java学习笔记8.2.2 JDBC实战 - GUI学生信息管理系统

Java学习笔记8.2.2 JDBC实战 - GUI学生信息管理系统

JSP 学习笔记

JSP学习笔记:JSP语法和指令