实验要求:
注册页面为RegisterForm.jsp,登录页面为loginForm.jsp,登录之前不能访问与注册、登录无关的页面。登录和注册功能应由register.jsp和login.jsp实现。
注册页面的效果如下图所示:
登陆页面的效果如下图所示:
编写主页index.jsp,内容包括查询所有好友信息和按姓名模糊查询好友信息两个链接。登录之前访问index.jsp应自动转到登录页面。
编写实现“查询所有好友信息”功能的JSP页面allfriend.jsp,用来显示index.jsp页面第一个链接所需的登录用户好友信息查询功能。
编写实现“按姓名模糊查询”功能的页面querybyname.jsp。模糊查询功能只需实现按姓名查询,如输入“李”,并利用allfriend.jsp查询所有姓名中带有“李”字的好友信息。
数据库访问功能应由独立的Java类实现。
数据库使用方法参照
http://blog.csdn.net/kydkong/article/details/49964363
package com.myfriend;
import java.sql.*;
import java.util.*;
public class DataBaseOperate {
static public void main(String[] args){
}
/**
*
* @param UserName
* @param PassWord
* @return 1:注册成功 0:用户名已经存在
* @throws SQLException
* @throws ClassNotFoundException
*/
static public int RegisterUser(String UserName,String PassWord) throws SQLException, ClassNotFoundException{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
Statement DataStatement = DataConnection.createStatement();
//查询用户名以否已经存在
ResultSet UserIsExistResult = DataStatement.executeQuery("select name from user where name = '" + UserName + "'");
if (UserIsExistResult.next()){
//存在
return 0;
}
else{
//不存在,添加
//获取最大id
ResultSet MaxIdResult = DataStatement.executeQuery("select max(userid) from user");
int MaxId;
if (MaxIdResult.next()){
MaxId = MaxIdResult.getInt("max(userid)");
}
else{
MaxId = 0;
}
MaxId ++;
DataStatement.executeUpdate("INSERT INTO user VALUES ('" + MaxId + "', '" + UserName + "', '" + PassWord + "')");
return 1;
}
}
/**
*
* @param UserName
* @param PassWord
* @return 0:登录成功 1:用户名不存在 2:密码不正确
* @throws ClassNotFoundException
* @throws SQLException
*/
static public int LoginUser(String UserName,String PassWord) throws ClassNotFoundException, SQLException{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
Statement DataStatement = DataConnection.createStatement();
//查询用户名以否已经存在
ResultSet UserIsExistResult = DataStatement.executeQuery("select * from user where name = '" + UserName + "'");
if (UserIsExistResult.next()){
String DBPassWord = UserIsExistResult.getString("password");
if (DBPassWord.equals(PassWord)){
//登录成功
return 0;
}
else{
return 2;
}
}
else{
return 1;
}
}
static public String[] GetFriend(String FriendId) throws ClassNotFoundException, SQLException{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
Statement DataStatement = DataConnection.createStatement();
//查询用户名关联的朋友
ResultSet MyFriendsResult = DataStatement.executeQuery(
"select * from myfriend where id = '" + FriendId + "'");
if (MyFriendsResult.next()){
String[] RowStringArray = new String[9];
RowStringArray[0] = String.valueOf(MyFriendsResult.getInt("id"));
RowStringArray[1] = String.valueOf(MyFriendsResult.getInt("userid"));
RowStringArray[2] = String.valueOf(MyFriendsResult.getString("name"));
RowStringArray[3] = String.valueOf(MyFriendsResult.getString("sex"));
RowStringArray[4] = String.valueOf(MyFriendsResult.getInt("age"));
RowStringArray[5] = String.valueOf(MyFriendsResult.getString("qq"));
RowStringArray[6] = String.valueOf(MyFriendsResult.getString("telephone"));
RowStringArray[7] = String.valueOf(MyFriendsResult.getString("email"));
RowStringArray[8] = String.valueOf(MyFriendsResult.getString("address"));
return RowStringArray;
}
return null;
}
/**
*
* @param UserName
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
static public String[][] GetAllFriends(String UserName,String SubName) throws ClassNotFoundException, SQLException{
//System.out.println(UserName + SubName);
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
Statement DataStatement = DataConnection.createStatement();
//查询用户名关联的朋友
ResultSet MyFriendsResult = DataStatement.executeQuery(
"select * from myfriend where userid in (select userid from user where name = '" + UserName + "')");
if (MyFriendsResult.next()){
MyFriendsResult.beforeFirst();
ArrayList ResultArray = new ArrayList();
while (MyFriendsResult.next()){
String[] RowStringArray = null;
String TempName = String.valueOf(MyFriendsResult.getString("name"));
if (SubName == "" || TempName.matches("^.*" + SubName + ".*$")){
RowStringArray = new String[9];
RowStringArray[0] = String.valueOf(MyFriendsResult.getInt("id"));
RowStringArray[1] = String.valueOf(MyFriendsResult.getInt("userid"));
RowStringArray[2] = String.valueOf(MyFriendsResult.getString("name"));
RowStringArray[3] = String.valueOf(MyFriendsResult.getString("sex"));
RowStringArray[4] = String.valueOf(MyFriendsResult.getInt("age"));
RowStringArray[5] = String.valueOf(MyFriendsResult.getString("qq"));
RowStringArray[6] = String.valueOf(MyFriendsResult.getString("telephone"));
RowStringArray[7] = String.valueOf(MyFriendsResult.getString("email"));
RowStringArray[8] = String.valueOf(MyFriendsResult.getString("address"));
ResultArray.add(RowStringArray);
}
}
String[][] Result = new String[ResultArray.size()][9];
for (int i = 0;i < ResultArray.size();i ++){
Result[i] = ResultArray.get(i);
}
return Result;
}
else{
return null;
}
}
static public int Insert(String UserName,String NewName,String NewSex,String NewAge,String NewQQ,String NewTel,String NewMail,String NewAddr) throws ClassNotFoundException, SQLException{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
Statement DataStatement = DataConnection.createStatement();
//查询userid
ResultSet UserIdResult = DataStatement.executeQuery(
"select userid from user where name = '" + UserName + "'");
String userid;
if (UserIdResult.next()){
userid = UserIdResult.getString("userid");
}
else{
return -1;
}
//获取最大值
ResultSet MaxIdResult = DataStatement.executeQuery("select max(id) from myfriend");
int MaxId;
if (MaxIdResult.next()){
MaxId = MaxIdResult.getInt("max(id)");
}
else{
MaxId = 0;
}
MaxId ++;
System.out.println(MaxId);
//插入新纪录
DataStatement.executeUpdate(
"INSERT INTO myfriend VALUES ('" + MaxId + "', '" + userid + "', '" + NewName + "', '" + NewSex + "', '" + NewAge + "', '" + NewQQ + "', '" + NewTel + "', '" + NewMail + "', '" + NewAddr + "')");
return 0;
}
/**
*
* @param FriendId
* @throws ClassNotFoundException
* @throws SQLException
*/
static public void DeleteFriend(String FriendId) throws ClassNotFoundException, SQLException{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
Statement DataStatement = DataConnection.createStatement();
//删除id
DataStatement.executeUpdate(
"delete from myfriend where id = '" + FriendId + "'");
}
static public int Modify(String FriendId,String NewName,String NewSex,String NewAge,String NewQQ,String NewTel,String NewMail,String NewAddr) throws ClassNotFoundException, SQLException{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
Statement DataStatement = DataConnection.createStatement();
//插入新纪录
DataStatement.executeUpdate(
"UPDATE myfriend SET name='" + NewName + "', sex='" + NewSex + "', age='" + NewAge + "', qq='" + NewQQ + "', telephone='" + NewTel + "', email='" + NewMail + "', address='" + NewAddr + "' WHERE id = '" + FriendId + "' ");
return 0;
}
}
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
主页
<%
if (session.getAttribute("LoginedUserName") == null){
out.print("请先登录!
登录");
return;
}
%>
我的好友录
查询所有好友信息
按姓名模糊查询好友信息
登出
当前时间:<%=(new java.util.Date()).toString()%>
主页,显示几个功能的链接。
LoginForm.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
登录
<%
if (session.getAttribute("LoginedUserName") != null){
out.print("您已经登录了!
主页");
return;
}
%>
我的好友录
用户注册
当前时间:<%=(new java.util.Date()).toString()%>
登录页面
login.jsp
<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>
<%
String UserName = request.getParameter("UserName");
String PassWord = request.getParameter("PassWord");
if (UserName == null || PassWord == null){
response.sendRedirect("LoginForm.jsp");
}
int LoginUserResult = DataBaseOperate.LoginUser(UserName,PassWord);
if (LoginUserResult == 0){
//登录成功
session.setAttribute("LoginedUserName", request.getParameter("UserName"));
out.print("登录成功!
主页");
}
else if (LoginUserResult == 1){
out.print("用户名不存在!
重新登录");
}
else if (LoginUserResult == 2){
out.print("密码不正确!
重新登录");
}
%>
登录的功能实现页面
logout.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
if (session.getAttribute("LoginedUserName") == null){
out.print("未登录!
登录");
return;
}
else{
session.removeAttribute("LoginedUserName");
out.print("已经登出。
登录");
}
%>
登出的功能实现页面
RegisterForm.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
注册
我的好友录
用户注册
当前时间:<%=(new java.util.Date()).toString()%>
注册页面
register.jsp
<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>
<%
String UserName = request.getParameter("UserName");
String PassWord = request.getParameter("PassWord");
if (UserName == null || PassWord == null){
response.sendRedirect("RegisterForm.jsp");
}
int RegisterResult = DataBaseOperate.RegisterUser(UserName,PassWord);
if (RegisterResult == 1){
out.print("注册成功!
登录");
}
else if (RegisterResult == 0){
out.print("用户名已经存在!
重新注册");
}
%>
allfriend.jsp
<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>
查询所有好友信息
<%
if (session.getAttribute("LoginedUserName") == null){
out.print("请先登录!
登录");
return;
}
%>
我的好友录
主页
当前时间:<%=(new java.util.Date()).toString()%>
显示全部好友页面,SubName是参数,只显示包含SubName的记录,主要与querybyname.jsp配合,显示特定的朋友。
allfriend中有2个超链接,其中用超链接提交表单的用法是:
删除选中记录
由于有修改和删除2个超链接,后台页面并不知道是这次提交是修改还是删除,所以又使用了一个
querybyname.jsp
<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>
按姓名模糊查询好友信息
<%
if (session.getAttribute("LoginedUserName") == null){
out.print("请先登录!
登录");
return;
}
%>
我的好友录
主页
当前时间:<%=(new java.util.Date()).toString()%>
模糊查询好友页面,具体实现为allfriend.jsp
InsertNewRowForm.jsp
<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>
添加新纪录
<%
if (session.getAttribute("LoginedUserName") == null){
out.print("请先登录!
登录");
return;
}
%>
我的好友录
主页
当前时间:<%=(new java.util.Date()).toString()%>
插入新纪录页面
insertrow.jsp
<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>
<%
if (session.getAttribute("LoginedUserName") == null){
out.print("请先登录!
登录");
return;
}
else{
request.setCharacterEncoding("utf-8");
String NewName = request.getParameter("NewName");
String NewSex = request.getParameter("NewSex");
String NewAge = request.getParameter("NewAge");
String NewQQ = request.getParameter("NewQQ");
String NewTel = request.getParameter("NewTel");
String NewMail = request.getParameter("NewMail");
String NewAddr = request.getParameter("NewAddr");
if (NewName == null || NewSex == null || NewAge == null || NewQQ == null || NewTel == null || NewMail == null || NewAddr == null){
response.sendRedirect("InsertNewRowForm.jsp");
}
DataBaseOperate.Insert((String)session.getAttribute("LoginedUserName"), NewName, NewSex, NewAge, NewQQ, NewTel, NewMail, NewAddr);
response.sendRedirect("allfriend.jsp");
}
%>
插入新纪录实现页面
ModifyForm.jsp
<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>
添加新纪录
<%
if (session.getAttribute("LoginedUserName") == null){
out.print("请先登录!
登录");
return;
}
%>
<%
String FriendId = request.getParameter("id");
if (FriendId == null){
response.sendRedirect("index.jsp");
}
String[] FriendContent = DataBaseOperate.GetFriend(FriendId);
if (FriendContent == null){
response.sendRedirect("allfriend.jsp");
return;
}
%>
我的好友录
主页
当前时间:<%=(new java.util.Date()).toString()%>
修改记录页面
Modify.jsp
<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>
<%
if (session.getAttribute("LoginedUserName") == null){
out.print("请先登录!
登录");
return;
}
else{
request.setCharacterEncoding("utf-8");
String NewName = request.getParameter("NewName");
String NewSex = request.getParameter("NewSex");
String NewAge = request.getParameter("NewAge");
String NewQQ = request.getParameter("NewQQ");
String NewTel = request.getParameter("NewTel");
String NewMail = request.getParameter("NewMail");
String NewAddr = request.getParameter("NewAddr");
if (NewName == null || NewSex == null || NewAge == null || NewQQ == null || NewTel == null || NewMail == null || NewAddr == null){
response.sendRedirect("ModifyForm.jsp");
}
DataBaseOperate.Modify(request.getParameter("FriendId"), NewName, NewSex, NewAge, NewQQ, NewTel, NewMail, NewAddr);
response.sendRedirect("allfriend.jsp");
}
%>
修改记录实现页面
注意:如果用户没有登录,那他不能访问除登录、注册外的所有页面。
还有有些功能有2个页面组成,一个是表单,一个是具体的程序,后台的页面不应该被除了前台访问以外的任何其他方式访问。