用户表——tbl_user(包括用户名、密码)
留言信息存储表——tbl_leave_message(包括留言人姓名、留言信息、留言主题、留言时间)
(1) 欢迎页面:
直接进入登录页面,如果登录不成功,就不可以进行留言
此页面还可以实现注册功能,为新用户提供方便
如果不输任何内容直接提交或者只是输入一项提交那么会输出友情提示:
注册页面(对于注册也划分了注册成功与失败页面):
(2) 成功登录后的页面:
(3) 留言页面(有留言成功与失败页面):
(4) 查看历史留言页面(此处之前有留言内容,所以可以直接查询到,如果没有历史留言也是会输出——“暂时没有留言,之后跳转到留言页面”)
欢迎页面——login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
登录
注册
Checklogin.java代码实现:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.JavaBean.DB_Conn;
import com.JavaBean.DB_User;
import com.JavaBean.User;
public class checklogin extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置请求响应的编码格式
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//获取out输出流对象
PrintWriter out=response.getWriter();
//设置注册成功与失败之后的页面跳转URL
String LoginSuccess="http://localhost:8080/MessageBoard/index.jsp";
String LoginFail="http://localhost:8080/MessageBoard/login.jsp";
//获取用户提交的信息
String name=request.getParameter("name");
String password=request.getParameter("password");
//核对用户名与密码,决定是否通过登录验证
DB_User db_User=new DB_User();
User user=db_User.get(name, password);
if(user!=null){
//登录成功时,把用户名存储到session中
request.getSession(true).setAttribute("username",name);
response.sendRedirect(LoginSuccess);
}else {
//否则(记录集为空)登录失败
out.print("不存在该用户!");
response.setHeader("Refresh", "3;url="+LoginFail);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
此过程中涉及数据库连接以及数据库操作代码,下面一次性展示:
User.java————封装一个user对象:
package com.JavaBean;
public class User {
//用户名与密码
private String name;
private String password;
//对用户填写的信息进行封装
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Message.java————用于封装一个message对象:
package com.JavaBean;
//实现对一个留言信息的封装
public class Message {
private String name;
private String title;
private String message;
private String time;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
DB_Conn.java————用于连接数据库与释放数据库连接功能(进行了封装操作)
package com.JavaBean;
import java.io.PrintWriter;
import java.sql.*;
public class DB_Conn {
//JDBC驱动程序名
private String driverName="com.mysql.jdbc.Driver";
//数据库用户名
private String userName="root";
//数据库密码
private String userPwd="root";
//数据库名
private String dbName="MessageBoard";
//数据库链接URL
private String url="jdbc:mysql://localhost:3306/"+dbName;
//数据库链接对象
private Connection conn=null;
//数据库语句对象
public Statement sm=null;
//创建out输出流对象
private PrintWriter out=null;
//建立数据库链接函数
public int ConnectDB(){
try {
Class.forName(driverName).newInstance();//实例化(另外一种说法——注册一个驱动)
conn=DriverManager.getConnection(url,userName,userPwd);//获取数据库链接
sm=conn.createStatement();
return 1;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
out.print("数据库链接失败!");
return 0;
}
}
//释放数据库链接函数
public void CloseDB(){
try {
if(sm!=null){
sm.close();
}
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
out.print("数据库关闭失败!");
}
}
}
DB_User.java————用于创建操作数据库的公用方法(包括用户登录验证、用户注册、用户留言、查看历史留言等功能)
package com.JavaBean;
import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import org.omg.CORBA.Request;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
public class DB_User {
// 创建公共数据库链接对象
DB_Conn conn = new DB_Conn();
/******************************************
* (1)查询数据库中是否存在改用户(使用用户名与密码进行验证)
******************************************/
public User get(String username,String password){
//链接数据库
conn.ConnectDB();
ResultSet rs=null;
//构建sql语句:
String sql="select * from tbl_user where username= '"
+username+"' and password='"+password+"'";
try {
//执行sql语句
rs=conn.sm.executeQuery(sql);
User user=null;//初始化一个user对象为空
if(rs.next()){
//如果存在该用户,那么就将他的用户名与密码存入user对象中
user=new User();
user.setName(rs.getString("username"));
user.setPassword(rs.getString("password"));
}
//不存在,直接返回user(其值为NULL)
return user;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}finally{
conn.CloseDB();
}
}
/*********************************************
* (2)在数据库的tbl_leave_message表中插入用户的留言信息
********************************************/
public int insert_Message(String username,String title,String message,String time){
//链接数据库,
conn.ConnectDB();
//构建sql语句
String sql="insert into tbl_leave_message(username,title,message,time) " +
"values('"+username+"'"+","+"'"+title+"','"+message+"','"+time+"')";
try {
//sql=new String(sql.getBytes("ISO-8859-1"),"utf-8");
int i=conn.sm.executeUpdate(sql);
return i;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return -2;
}finally{
//最释放数据库资源
conn.CloseDB();
}
}
/***************************************
* (3)从tbl_leave_message表中获取历史留言记录
* ************************************/
public ArrayList get_History_Message(){
//链接数据库
conn.ConnectDB();
//初始化结果集为空
ResultSet rs=null;
//创建一个用于存储对象的列表
ArrayList list= new ArrayList();
//构建sql语句
String sql="select * from tbl_leave_message ";
try {
rs=conn.sm.executeQuery(sql);
while(rs.next()){
//每次都新建一个对象,之后对对象进行封装赋值,之后把对象添加到列表中
Message message=new Message();
message.setName(rs.getString("username"));
message.setTitle(rs.getString("title"));
message.setMessage(rs.getString("message"));
message.setTime(rs.getString("time"));
list.add(message);//添加对象到列表中
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}finally{
conn.CloseDB();//关闭数据库释放资源!
}
}
/********************************************
* (4)注册用户信息(往tbl_user表中插入数据信息)
* ****************************************/
public int register_user(String username,String password){
// 链接数据库
conn.ConnectDB();
// 创建sql语句
String sql = "insert into tbl_user(username,password) " + " values( '"
+ username + "','" + password + "' )";
try {
// 执行sql语句
int flag = conn.sm.executeUpdate(sql);
return flag;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return 0;
} finally {
conn.CloseDB();// 释放数据库链接资源
}
}
}
下面进行servlet逻辑功能处理代码的介绍(checklogin.java已经有过介绍此处不再进行解释):
注册页面(register.java)的逻辑处理代码:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.JavaBean.DB_User;
public class register extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置请求响应的编码格式
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
// 获取out输出流对象
PrintWriter out = response.getWriter();
// 设置注册成功与失败之后的页面跳转URL
String RegisterSuccess = "http://localhost:8080/MessageBoard/login.jsp";
String RegisterFail = "http://localhost:8080/MessageBoard/login.jsp";
// 获取用户提交的信息
String name = request.getParameter("username");
String password = request.getParameter("password");
// 核对用户名与密码,决定是否通过登录验证
DB_User db_User = new DB_User();
int flag = db_User.register_user(name, password);
if (flag > 0) {
out.print("注册成功!");
response.setHeader("Refresh", "3;URL=" + RegisterSuccess);
} else {
// 否则(记录集为空)登录失败
out.print("注册失败!");
response.setHeader("Refresh", "3;url=" + RegisterFail);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
对于留言信息处理的(doMessage.java)页面代码:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.JavaBean.DB_User;
import com.sun.org.apache.bcel.internal.generic.NEW;
import java.util.*;
public class doMessage extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置请求相应的编码格式
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//创建输出流对象:
PrintWriter out=response.getWriter();
//创建URL
String URL="http://localhost:8080/MessageBoard/leaveMessage.jsp";
//获取用户提交的信息
String title =request.getParameter("title");
String message=request.getParameter("context");
HttpSession session=request.getSession();
String username=(String) session.getAttribute("username");
//获取当前时间,一并提交到数据库进行存储
SimpleDateFormat format=new SimpleDateFormat("YYYY年MM月dd日HH时mm分ss秒");
String time=format.format(new java.util.Date());
//创建数据库处理对象
DB_User user=new DB_User();
int i=user.insert_Message(username,title, message,time);
if(i>0){
out.print("留言成功!");
response.setHeader("Refresh", "3;URL="+URL);
}else{
out.print("留言失败!");
response.setHeader("Refresh", "3;URL="+URL);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
对于历史留言的逻辑处理代码(ShowMessage.java):
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.JavaBean.DB_User;
import com.JavaBean.Message;
public class ShowMessage extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置请求响应编码格式
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
// 创建输入流对象out
PrintWriter out = response.getWriter();
// 创建一个列表用来存储获得信息
ArrayList list = new ArrayList();
String Check_Success_URL = "http://localhost:8080/MessageBoard/show.jsp";
String Check_Fail_URL = "http://localhost:8080/MessageBoard/leaveMessage.jsp";
// 创建数据库操作对象
DB_User user = new DB_User();
// 获取存储留言历史的表
list = user.get_History_Message();
if(list!=null&&list.size()>0){
HttpSession session=request.getSession();
session.setAttribute("list", list);
response.setHeader("Refresh", "0;URL="+Check_Success_URL);
}else {
out.print("无历史留言记录!");
response.setHeader("Refresh", "3;URL="+Check_Fail_URL);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
接下来介绍一下对于view模式的jsp页面的代码实现:
Register.jsp页面
<%@ page language="java" import="java.util.*"pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
注册
LeaveMessage.jsp页面代码实现:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
留言板
留言板
Index.jsp代码实现:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
系统首页
我要留言
Show.jsp页面代码实现:
查询结果
<%
ArrayList list = (ArrayList) session.getAttribute("list");
if (list != null && list.size() > 0) {
for (int i = list.size() - 1; i >= 0; i--) {
Message message = list.get(i);
%>
留言主题:<%=message.getTitle()%>
留言人:<%=message.getName()%>
留言内容:
留言时间:<%=message.getTime()%>
<%
}
}
%>