学JavaEE也有一段时间了,跟着老师和教材做了不少东西,但是一直以来没时间写博客,今天就把以前写的一个简易留言板简单发一下吧。
主要用的开发工具为 MyEclipse(2014、2016均可)、Tomcat 7.0、SQL Server 2016、SSMS数据库管理工具、浏览器等。
百度网盘链接:https://pan.baidu.com/s/1c2iCPQ4 提取码:7yxk
开发环境为windows系统,已安装配置Java最新版开发环境。
登录、注册、并可以在留言板留言,所有留言内容均可见。
所采用JSP+Servlet+JavaBean传统方式,仅限于学习使用。
JSP代码:
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
登录界面
register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
注册界面
error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
登录出错界面
输入的用户名不存在或者密码错误
点我返回登陆界面
main.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@page import="org.model.MessBoar" %>
<%@page import="org.dao.*" %>
留言板界面
这里是留言板主界面
留言
leavemessage.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@page import="org.model.MessBoar" %>
<%@page import="org.dao.*" %>
留言界面
请留言
返回留言板界面
success.jsp
<%@ page language="java" pageEncoding="gb2312"%>
成功界面
留言成功,单击这里返回主界面。
web.xml(配置)
loginServlet
org.servlet.LoginServlet
loginServlet
/loginServlet
registerServlet
org.servlet.RegisterServlet
registerServlet
/registerServlet
leaveMessageServlet
org.servlet.LeaveMessageServlet
leaveMessageServlet
/leaveMessageServlet
messageBoard
login.jsp
DBconn.java
package org.db;
import java.sql.*;
public class DBconn {
public static Connection conn; //Connection对象(链接)
//连接数据库
public static Connection getConn(){
try{
//加载注册SQLSever的JDBC驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//编写链接字符串,创建并且获取链接
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=MessageBoard","sa","19961002happy");
return conn;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public static void CloseConn(){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
LoginDao.java
package org.dao;
import java.sql.*;
import org.model.*;
import org.db.*;
import java.util.ArrayList;
public class LoginDao {
Connection conn=DBconn.getConn(); ; //数据库连接对象
PreparedStatement pstmt;
public Login checkLogin(String name,String password){ //验证用户名密码
try{
pstmt=conn.prepareStatement("select*from[loginss] where name=? " + " and password=?");
pstmt.setString(1, name); //设置SQL语句参数
pstmt.setString(2, password); //设置SQL语句参数
ResultSet rs=pstmt.executeQuery(); //执行查询,返回结果集
if(rs.next()){ //通过JavaBean保存值
Login login=new Login();
login.setId(rs.getInt(1));
login.setName(rs.getString(2));
login.setPassword(rs.getString(3));
login.setRole(rs.getInt(4));
return login; //返回JavaBean对象
}
return null; //验证失败返回null
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public ArrayList findMbInfo(){
try{
ArrayList al=new ArrayList();
pstmt=conn.prepareStatement("select * from [messagess]");
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
MessBoar mb=new MessBoar();
mb.setId(rs.getInt(1));
mb.setName(rs.getString(2));
mb.setTime(rs.getDate(3));
mb.setTitle(rs.getString(4));
mb.setMessage(rs.getString(5));
al.add(mb);
}
return al;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public String getName(int id){
String name=null;
try{
pstmt=conn.prepareStatement("select name from [loginss] where id=?");
pstmt.setInt(1, id);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
name=rs.getString(1);
}
return name;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public boolean addInfo(MessBoar mb){
try{
pstmt=conn.prepareStatement("insert into [messagess] values(?,?,?,?,?)");
pstmt.setInt(1, mb.getId());
pstmt.setString(2, mb.getName());
pstmt.setDate(3, mb.getTime());
pstmt.setString(4, mb.getTitle());
pstmt.setString(5, mb.getMessage());
pstmt.executeUpdate();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
public boolean insertUser(int id,String name,String password){
try{
pstmt=conn.prepareStatement("insert into [loginss] values(?,?,?,?)");
pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.setString(3, password);
pstmt.setInt(4, 0);
pstmt.executeUpdate();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
}
Login.java
package org.model;
public class Login {
private Integer id;
private String name;
private String password;
private int role;
public Integer getId(){
return this.id;
}
public void setId(Integer id){
this.id=id;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name=name;
}
public String getPassword(){
return this.password;
}
public void setPassword(String password){
this.name=password;
}
public int getRole(){
return this.role;
}
public void setRole(int role){
this.role=role;
}
}
message.java
package org.model;
import java.sql.*;
public class MessBoar {
private int id;
private String name;
private Date time;
private String title;
private String message;
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 Date getTime() {
return time;
}
public void setTime(Date time) {
this.time=time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title=title;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message=message;
}
}
LeaveMessageServlet.java
package org.servlet;
import java.io.IOException;
import java.sql.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dao.LoginDao;
import org.model.*;
public class LeaveMessageServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
// 设置请求编码
request.setCharacterEncoding("gb2312");
// 设置响应编码
response.setContentType("gb2312");
// 获取title内容
String title=request.getParameter("title");
// 获取message内容
String message=request.getParameter("message");
// 从session中取出当前用户对象
Login leaveMessageBoard=(Login) request.getSession().getAttribute("login");
// 建立留言表对应JavaBean对象,把数据封装进去
MessBoar mb=new MessBoar();
mb.setId(leaveMessageBoard.getId());
// 参数为获取的当前时间
mb.setName(leaveMessageBoard.getName());
mb.setTime(new Date(System.currentTimeMillis()));
mb.setTitle(title);
mb.setMessage(message);
// 调DB类中的方法判断是否插入成功
if(new LoginDao().addInfo(mb)){
response.sendRedirect("success.jsp") ;
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doGet(request,response);
}
}
LoginServlet.java
package org.servlet;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.model.*;
import org.dao.*;
public class LoginServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
request.setCharacterEncoding("gb2312"); //设置请求编码
response.setContentType("gb2312"); //设置响应编码
LoginDao loginDao= new LoginDao();
HttpSession session=request.getSession(); // 先获得user对象,如果是第一次访问该Servlet,用户对象肯定为空,但如果是第
Login l=(Login) session.getAttribute("login"); // 二次甚至是第三次,就不应该再判断该用户的信息
if(l==null)
l = loginDao.checkLogin(request.getParameter("name"),request.getParameter("password"));
if(l!=null){ //如果登陆成功
session.setAttribute("login",l); //将获取的对象保存在session中
ArrayList al=loginDao.findMbInfo(); //获取留言板的内容,返回一个数组
session.setAttribute("al", al); //把数组保存起来
response.sendRedirect("main.jsp"); //验证成功跳转到 main.jsp
}
else{ //验证失败跳转到 error.jsp
response.sendRedirect("error.jsp");
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doGet(request,response);
}
}
RegisterServlet.java
package org.servlet;
import java.io.IOException;
import org.dao.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegisterServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
int id=Integer.valueOf(request.getParameter("id"));
String name=request.getParameter("name");
String password=request.getParameter("password");
if(new LoginDao().insertUser(id ,name, password)){
response.sendRedirect("login.jsp");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
粘代码粘得都心累,比较忙就不做太多介绍了。数据库需要自己新建,对应数据类型建立就可以了。下次有空回头复习时再加上详细介绍吧。附上几张效果图。
好好学习,天天向上~加油~~~
//LoginServlet.java错误已修改 2016/09/02
//数据库语句添加 2016/12/24
数据库创建语句:
create database messageBoard
go
use messageBoard
create table loginss
(
id int not null primary key,
name varchar(20) not null,
password varchar(20) not null,
role int not null
)
create table messagess
(
id int not null,
name varchar(20) not null,
time datetime not null,
title varchar(20) not null,
message varchar(50) not null
)
上面的表名称需要与LoginDao.java里面的保持一致。
进阶篇:SpringBoot搭建简单留言板项目