图书租赁系统

项目架构

图书租赁系统_第1张图片
webContent/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




登录界面







欢迎登录

用户名:
密码:
注册

com.bean下面新建
User.java
package com.bean;

public class User {
	private int id;
	private String username;
	private String password;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(String username, String password) {
		super();
		
		this.username = username;
		this.password = password;
	}
	@Override
	public String toString() {
		return "User [id="+id+", username=" + username + ", password=" + password + "]";
	}
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
	

}

Book.java

package com.bean;

public class Book {
	
	private int id;
	private String name;
	private double price;
	private String info;
	private String img_url;
	private String borrow_date;
	private String user;
	public Book() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Book(int id, String name, double price, String info, String img_url, String borrow_date, String user) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.info = info;
		this.img_url = img_url;
		this.borrow_date = borrow_date;
		this.user = user;
	}
	@Override
	public String toString() {
		return "Book [id=" + id + ", name=" + name + ", price=" + price + ", info=" + info + ", img_url=" + img_url
				+ ", borrow_date=" + borrow_date + ", user=" + user + "]";
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	public String getImg_url() {
		return img_url;
	}
	public void setImg_url(String img_url) {
		this.img_url = img_url;
	}
	public String getBorrow_date() {
		return borrow_date;
	}
	public void setBorrow_date(String borrow_date) {
		this.borrow_date = borrow_date;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	
	
	
	

}

com.dao下面建
DBConnection.java
package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/*
 * JDBC连接数据库
 */
public class DBConnection {

	private static Connection conn;

	// ?useUnicode=true&characterEncoding=UTF-8 防止中文出现乱码
	private static String url = "jdbc:mysql://localhost:3306/17101?useUnicode=true&characterEncoding=UTF-8";
     //数据库用户名
	private static String username = "root";
   //数据库密码
	private static String password = "root";
	// static代码块,只在类加载的时候执行一次
	static {

		try {
			
			
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url, username, password);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static Connection getConnection() {

		return conn;
	}

}

UserDao.java

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.bean.User;


//数据访问层
public class UserDao {

	public User findByInfo(User user) {

		User user_find = null;

		// 得到数据库连接
		Connection conn = DBConnection.getConnection();
		// 根据用户名和密码查询用户是否存在
		String sql = "select * from user where username=? and password=?";

		try {
			// 预编译sql
			PreparedStatement ps = conn.prepareStatement(sql);

			ps.setString(1, user.getUsername());

			ps.setString(2, user.getPassword());

			// 查询得到结果集

			ResultSet rs = ps.executeQuery();

			// 遍历结果集,获取信息

			while (rs.next()) {

				String username_find = rs.getString("username");

				String password_find = rs.getString("password");

				// 将信息赋值给对象user_find
				user_find = new User(username_find, password_find);

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return user_find;
	}

	// 根据用户名查询用户信息

	public User findByName(String username) {

		User user_find = null;

		// 获取数据库连接

		Connection conn = DBConnection.getConnection();
		// sql语句
		String sql = "select*from user where username=?";

		try {
			// 预编译
			PreparedStatement ps = conn.prepareStatement(sql);
			// 对未定义的用户名传值
			ps.setString(1, username);

			// 查询结果集

			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				String username_find = rs.getString("username");

				String password_find = rs.getString("password");

				user_find = new User(username_find, password_find);

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return user_find;

	}
	
	//将用户信息添加进数据库
	
	public int add(User user) {
		
		int result=0;
		
		//获取数据库连接
		
		Connection conn=DBConnection.getConnection();
		//sql语句
		String sql="insert into user(username,password) value(?,?)";
		
		try {
			//预编译
			PreparedStatement ps=conn.prepareStatement(sql);
		ps.setString(1, user.getUsername());
		
		ps.setString(2, user.getPassword());
		
		result=ps.executeUpdate();
		
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return result;
		
		
			
	}
	
	
	
	
	

}

BookDao.java

package com.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.bean.Book;
import com.sun.glass.ui.Pixels.Format;

public class BookDao {

	public List findAll(String username) {

		List list = new ArrayList();

		// 获取数据库连接
		Connection conn = DBConnection.getConnection();

		String sql = "select*from book where user=? or user=?";

		try {
			PreparedStatement ps = conn.prepareStatement(sql);

			ps.setString(1, username);
			ps.setString(2, "");

			// 查询得到结果集
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {

				int id = rs.getInt("id");
				String name = rs.getString("name");
				double price = rs.getDouble("price");
				String info = rs.getString("info");
				String img_url = rs.getString("img_url");
				String borrow_date = rs.getString("borrow_date");
				String user = rs.getString("user");
				Book book = new Book(id, name, price, info, img_url, borrow_date, user);
				list.add(book);
			}

			// 测试
			for (Book book : list) {
				System.out.println("db" + book.toString());
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;

	}

	// 获取最大页数
	public int getMaxPage(String username, int num) {

		int result = 0;
		int PageNum = 0;

		// 获取数据库连接
		Connection conn = DBConnection.getConnection();
		String sql = "select count(*) as count from book where user=? or user=?";

		try {
			PreparedStatement ps = conn.prepareStatement(sql);

			ps.setString(1, username);
			ps.setString(2, "");
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				result = rs.getInt("count");
			}
			// 防止算数异常
			if (num == 0) {
				PageNum = 1;
				return PageNum;
			}
			// 能够整除的情况
			if (result % num == 0) {

				PageNum = result / num;
			} else {
				PageNum = result / num + 1;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return PageNum;
	}

	// 获取分页数据

	public List Limit(String username, int page, int num) {
		List list = new ArrayList();

		// 获取数据库连接
		Connection conn = DBConnection.getConnection();
		String sql = "select * from book where user=? or user=? limit ?,?";

		try {
			PreparedStatement ps = conn.prepareStatement(sql);

			ps.setString(1, username);
			ps.setString(2, "");
			ps.setInt(3, (page - 1) * num);// (查询页数-1)*每页记录数
			ps.setInt(4, num);
			// 得到结果集

			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				double price = rs.getDouble("price");
				String info = rs.getString("info");
				String img_url = rs.getString("img_url");
				String borrow_date = rs.getString("borrow_date");
				String user = rs.getString("user");

				Book book = new Book(id, name, price, info, img_url, borrow_date, user);
				list.add(book);

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;

	}

	// 根据id修改图书信息为借出
	public int updateById(int id, String username) {

		int result = 0;
		// 获取数据库连接
		Connection conn = DBConnection.getConnection();
		String sql = "update book set borrow_date=?,user=? where id=?";

		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, getDate());
			ps.setString(2, username);
			ps.setInt(3, id);

			result = ps.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;

	}

	// 获取当前日期
	public String getDate() {

		String time = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		time = sdf.format(new Date());

		return time;
	}

	// 计算借出天数

	public int getDays(String borrow_time, String back_time) {
		int days = 0;

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		try {
			Date date_borrow = format.parse(borrow_time);
			Date Date_back = format.parse(back_time);

			// 转换为毫秒
			long borrow = date_borrow.getTime();
			long back = Date_back.getTime();
			// 计算租金
			days = (int) ((back - borrow) / 1000 / 60 / 60 / 24);

			System.out.println("共借出了" + days + "天");

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return days;

	}

	// 根据id查找图书信息

	public Book findById(int id) {

		Book book = null;

		// 获取数据库连接
		Connection conn = DBConnection.getConnection();
		String sql = "select*from book where id=?";

		try {
			PreparedStatement ps = conn.prepareStatement(sql);

			ps.setInt(1, id);
			// 查询结果集
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {

				int id_find = rs.getInt("id");

				String name = rs.getString("name");
				double price = rs.getDouble("price");
				String info = rs.getString("info");
				String img_url = rs.getString("img_url");
				String borrow_date = rs.getString("borrow_date");
				String user = rs.getString("user");
				book = new Book(id_find, name, price, info, img_url, borrow_date, user);

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return book;

	}

	// 根据id修改图书信息为归还
	public int updateByID(int id) {

		int result = 0;
		// 获取数据库连接

		Connection conn = DBConnection.getConnection();

		String sql = "update book set borrow_date=?,user=? where id=?";

		try {
			PreparedStatement ps = conn.prepareStatement(sql);

			ps.setString(1, "尚未借出");
			ps.setString(2, "");
			ps.setInt(3, id);
			result = ps.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;

	}

}

com.filter下新建过滤器
CharacterEncodingFilter.Java
package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
//过滤器 
public class CharacterEncodingFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		
		System.out.println("过滤器处理字符编码...");
		arg0.setCharacterEncoding("utf-8");
		arg1.setCharacterEncoding("utf-8");
		
		arg1.setContentType("text/html; charset=UTF-8");
		
		
		
		arg2.doFilter(arg0, arg1);

	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}

com.service下新建
UserService.java
package com.service;

import com.bean.User;
import com.dao.UserDao;

//业务逻辑层
public class UserService {

	private UserDao udao = new UserDao();

	// 登录验证

	public boolean login(User user) {

		User user_find = udao.findByInfo(user);

		if (user_find != null) {

			return true;

		}

		return false;
	}
	
	//注册
	
	public void regist(User user) {
		
		udao.add(user);
		
	
	}
	public boolean registcheck(String username) {
		
		if(udao.findByName(username)==null) {
			//可以注册
			return true;
		}
		
		
		return false;
	}
	
	

}

BookService.java

package com.service;

import java.util.ArrayList;
import java.util.List;

import com.bean.Book;
import com.dao.BookDao;

//业务逻辑层
public class BookService {

	private BookDao bdao = new BookDao();

	public List listbook(String username) {

		List list = new ArrayList();

		list = bdao.findAll(username);

		return list;
	}

	// 分页得到共多少页
	public int getMaxPage(String username, int num) {

		return bdao.getMaxPage(username, num);

	}

	// 分页数据

	public List Limit(String username, int page, int num) {

		List list = new ArrayList();
		list = bdao.Limit(username, page, num);

		return list;
	}

	// 借出图书

	public int borrow_book(int id, String username) {

		int result = 0;

		result = bdao.updateById(id, username);
		return result;

	}

	// 借出天数
	public int getDays(String borrow_time, String back_time) {

		int days = 0;

		days = bdao.getDays(borrow_time, back_time);
		return days;

	}

	// 计算租金

	public double money(int days, double price) {
		double pay = 0;

		pay = price * days;

		return pay;

	}

	// 获取当前日期的字符串

	public String getDate() {

		return bdao.getDate();

	}

	// 根据id得到图书信息
	public Book findById(int id) {

		Book book = null;
		book = bdao.findById(id);

		return book;

	}
	//归还图书
	
	public int back_book(int id) {
		int result=0;
		result=bdao.updateByID(id);
		
		return result;
		}
	

}

新建com.servlet

package com.servlet;

import java.io.IOException;
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 javax.servlet.http.HttpSession;

import com.bean.Book;
import com.service.BookService;

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	doPost(request, response);
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//得到登录的用户名
		HttpSession session=request.getSession();
		
		String username=session.getAttribute("username").toString();
	
		
		//分页每页记录数
		int num=3;
		
		//得到分页的最大页数
		int PageNum=bservice.getMaxPage(username, num);
		
		//得到请求的页数
		int page=Integer.parseInt(request.getParameter("page"));
		
		//准备数据
		List list=bservice.Limit(username, page, num);
		
		request.setAttribute("booklist", list);
		request.setAttribute("PageNum", PageNum);
		request.setAttribute("page", page);
		
		//转发
		request.getRequestDispatcher("WEB-INF/jsp/main.jsp").forward(request, response);
		
		
		
	}

}
package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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.service.UserService;

/**
 * Servlet implementation class CheckUserServlet
 */
@WebServlet("/checkuser.do")
public class CheckUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private UserService uservice = new UserService();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CheckUserServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
		doPost(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter pw = response.getWriter();

		String username = request.getParameter("name");
		System.out.println("CheckUserServlet" + username);

		System.out.println("CheckUserServlet" + uservice.registcheck(username));
		// 不能注册 返回已存在
		if (!uservice.registcheck(username)) {
			pw.write("exists");
			pw.flush();
			pw.close();

		}

	}

}
package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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 javax.servlet.http.HttpSession;

import com.service.BookService;

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	
	doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		System.out.println("借出图书");
		PrintWriter pw=response.getWriter();
		
		//得到登陆的用户名
		HttpSession session=request.getSession();
		
		String username=session.getAttribute("username").toString();
		int id=Integer.parseInt(request.getParameter("id"));
		int page=Integer.parseInt(request.getParameter("page"));
		
		//修改图书信息
		bservice.borrow_book(id, username);
		
		//刷新页面
		request.getRequestDispatcher("listbook.do?page="+page).forward(request, response);
		
		
		
	}

}
package com.servlet;

import java.io.IOException;
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 javax.servlet.http.HttpSession;

import com.bean.Book;
import com.service.BookService;

/**
 * Servlet implementation class BackBookShowServlet
 */
@WebServlet("/backbookshow.do")
public class BackBookShowServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private BookService bservice = new BookService();

    /**
     * @see HttpServlet#HttpServlet()
     */
    public BackBookShowServlet() {
        super();
        // TODO Auto-generated constructor stub


    
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	
	doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
   
		// 得到登录的用户名

				HttpSession session = request.getSession();

				String username = session.getAttribute("username").toString();
				// 得到操作的图书编号

				int id = Integer.parseInt(request.getParameter("id"));

				// 得到第几页数据

				int back_page = Integer.parseInt(request.getParameter("back_page"));

				// 根据id得到图书信息

				Book book_back = bservice.findById(id);

				// 得到书名

				String book_name = book_back.getName();

				// 得到图书图片地址

				String img_url = book_back.getImg_url();

				// 得到借出日期

				String borrow_date = book_back.getBorrow_date();

				// 得到每日租金

				double price = book_back.getPrice();

				System.out.println("BackBookShowServlet:" + id);

				System.out.println("BackBookShowServlet:" + book_name);
				System.out.println("BackBookShowServlet:" + borrow_date);

				// 准备显示数据

				// 归还日期

				String back_date = bservice.getDate();

				/* 得到借出天数 */
				int days = bservice.getDays(borrow_date, back_date);
				// 得到租金

				double money = bservice.money(days, price);

				// 将页数保存进session

				session.setAttribute("back_page", back_page);

				// 归还图书

				bservice.back_book(id);

				// 转发到页面显示

				request.setAttribute("back_img", img_url);

				request.setAttribute("back_name", book_name);

				request.setAttribute("back_price", price);

				request.setAttribute("back_days", days);

				request.setAttribute("back_money", money);

				// 转发

				request.getRequestDispatcher("WEB-INF/jsp/back_book.jsp").forward(
						request, response);

			}

	
	
	}



package com.servlet;

import java.io.IOException;
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 javax.servlet.http.HttpSession;

import com.bean.User;
import com.service.UserService;

/**
 * Servlet implementation class login_servlet
 */
@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 调用业务对象
	UserService uservice = new UserService();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());

		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		

		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(username);
		System.out.println(password);

		// 创建对象保存信息
		User user = new User(username, password);
		// 调用登陆业务,判断是否登陆成功
		if (uservice.login(user)) {

			// 登陆成功
			HttpSession session = request.getSession();

			session.setAttribute("username", username);

			// 转发到登陆成功界面

			request.getRequestDispatcher("listbook.do?page=1").forward(request, response);

		} else {
			// 登陆失败,则转发到登陆失败界面

			request.getRequestDispatcher("WEB-INF/jsp/failed.jsp").forward(request, response);

		}

	}

}

package com.servlet;

//控制层
import java.io.IOException;

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.bean.User;
import com.service.UserService;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/regist.do")
public class  RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// 业务处理对象

	UserService uservice = new UserService();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegistServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		

		String username = request.getParameter("username");
		String password = request.getParameter("password");

		System.out.println("regist"+username);
		System.out.println("regist"+password);

		// 创建对象保存信息

		User user = new User(username, password);
        uservice.regist(user);
        
        //注册成功
        request.getRequestDispatcher("WEB-INF/jsp/regist_success.jsp").forward(request, response);

	}

}
package com.servlet;

import java.io.IOException;
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 ToRegistServlet
 */
@WebServlet("/toregist.do")
public class ToRegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ToRegistServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	
	doPost(request, response);
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		//转发到注册界面
		request.getRequestDispatcher("WEB-INF/jsp/regist.jsp").forward(request, response);
		
		
		
		
	}

}



WEB-INF/jsp
back_book.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>




back_book






	

归还图书信息!


书名:${ back_name}
每日租金(元):${ back_price}
借出天数(天):${ back_days}
租金(元):${back_money }

regist.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>




用户注册





	

用户注册


用户名:
密码:
确认密码:

failed.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here
<%
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	%>
	


用户名错误,请重新输入
返回

regist_success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
  <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
    
 %>   




Insert title here

	
	    
	
	
	
	 


注册成功,正在自动跳转...
登录


main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>




main





	

欢迎您:${sessionScope.username }

切换用户


封面 书名 描述 每日租金(元) 借出日期 操作
${book.name } ${book.info } ${book.price } ${book.borrow_date }
首页 上页 上页 下页 下页 末页 跳转到第 ${page }/${PageNum}页

你可能感兴趣的:(java)