javaEE之jsp+JavaBean實現登入+註冊+留言功能(外掛資料庫)
阿新 • • 發佈:2018-11-17
javaEE之jsp+JavaBean實現登入+註冊+留言功能(外掛資料庫)
實現效果
#UserBean.java
package nmx; public class UserBean { private String username; private String truename; private String email; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getTruename() { return truename; } public void setTruename(String truename) { this.truename = truename; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
#login.jsp(登入頁面)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form name="form1" method="post"> 使用者名稱:<input type="text" name="username"><br> 密碼 : <input type="password" name="password"><br> <input type="submit" value="註冊" name="register" onclick="this.form.action = 'register.jsp'"> <input type="submit" value="登入" name="login" onclick="this.form.action = 'validate.jsp'"> </form> </body> </html>
#validate.jsp(登入驗證)
<%@ page language="java" import="java.util.*,nmx.*,java.sql.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'validate.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% request.setCharacterEncoding("utf-8"); %> <jsp:useBean id="userBean" class="nmx.UserBean" /> <% String Username=request.getParameter("username"); String Password=request.getParameter("password"); Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"; String usename="root"; String psw=""; Connection conn=DriverManager.getConnection(url, usename, psw); if(conn!=null){ String sql="select * from user where username='"+Username+"' and password='"+Password+"'"; Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery(sql); if(rs.next()){ userBean=new UserBean(); userBean.setUsername(rs.getString("username")); userBean.setTruename(rs.getString("truename")); userBean.setEmail(rs.getString("email")); session.setAttribute("user", userBean); response.sendRedirect("main.jsp"); }else{ response.sendRedirect("error.jsp"); } conn.close(); }else{ out.println("資料庫連線失敗!"); } %> </body> </html>
#main.jsp(登陸後的主頁面)
<%@ page language="java" import="java.util.*,nmx.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'main.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="user" class="nmx.UserBean" scope="session" />
<%//if(user.getUsername()==null){ response.sendRedirect("login.jsp");} %>
Hello,<%=user.getUsername()%><br>
<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
String usename="root";
String psw="";
Connection conn=DriverManager.getConnection(url, usename, psw);
if(conn!=null){
String sql="select * from user ";
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
// List user_list=new ArrayList();
%>
<table border="1">
<tr>
<td>id</td><td>使用者名稱</td><td>真實姓名</td><td>郵箱</td>
</tr>
<%
while(rs.next()){
%>
<tr>
<td><%=rs.getInt("id") %></td>
<td><%=rs.getString("username") %></td>
<td><%=rs.getString("truename") %></td>
<td><%=rs.getString("email") %></td>
</tr>
<%} %>
</table>
<%
String sql1="select * from message ";
Statement stmt1=conn.createStatement();
ResultSet rs1=stmt1.executeQuery(sql1);
%>
<table border="1">
<tr><td>使用者名稱</td><td>留言</td></tr>
<%while(rs1.next()){ %>
<tr>
<td><%=rs1.getString("username") %></td>
<td><%=rs1.getString("message") %></td>
</tr>
<%
}%>
</table>
<% conn.close();
}else{
out.println("資料庫連線失敗!");
}
%>
<form action="message.jsp" method="post">
<input type="submit" value="留言" >
<%
// out.print(user.getUsername());
session.setAttribute("user", user); %>
</form>
</body>
</html>
#message.jsp(留言頁面)
<%@ page language="java" import="java.util.*,nmx.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'message.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="user" class="nmx.UserBean" scope="session" />
<% request.setCharacterEncoding("utf-8");
session.getAttribute("user");
session.setAttribute("Username", user);
%>
<form >
<textarea name="message" method="post"></textarea><br>
<input type="submit" value="提交" onclick="this.form.action = 'message.jsp'">
<input type="button" value="返回" onclick="window.location='main.jsp'">
</form>
<%
int id=0;
String message=request.getParameter("message");
if(message!=null){
Class.forName("com.mysql.jdbc.Driver");//記載資料庫驅動,註冊到驅動管理器
String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
String username="root";
String password="";
Connection conn=DriverManager.getConnection(url,username,password);
String sql1="insert into message values(?,?,?)";
String sql2="select id from message";
PreparedStatement ps1=conn.prepareStatement(sql1);
PreparedStatement ps2=conn.prepareStatement(sql2);
ResultSet rs=ps2.executeQuery();
while(rs.next()){
id=rs.getInt(1);
}
id+=1;
ps1.setInt(1,id);
ps1.setString(2,user.getUsername());
ps1.setString(3,message);
int rtn=ps1.executeUpdate();
ps1.close();
ps2.close();
conn.close();
%>
<%
if(rtn==1){
out.println("留言成功!");
}else{
out.println("留言失敗!");
}
}%>
</body>
</html>
#error.jsp(登入失敗頁面)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'error.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
使用者名稱或密碼錯誤,請<a href="login.jsp">重新登入</a>
</body>
</html>
#register.jsp(註冊頁面)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'register.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form name="form2" action="insertdata.jsp" method="post">
<table border="0">
<tr><td> 使用者名稱 :</td><td> <input type="text" name="username"></td></tr>
<tr><td>密碼 :</td><td><input type="password" name="password"></td></tr>
<tr><td>真實姓名:</td><td><input type="text" name="truename"></td></tr>
<tr><td>郵箱 :</td><td><input type="text" name="email"></td></tr>
<tr><td colspan="2" align="center"> <input type="submit" name="reg" value="註冊"> </td>
</table>
</form>
</body>
</html>
#insertdata.jsp(向資料庫增加新使用者)
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String Username=request.getParameter("username");
String Password=request.getParameter("password");
String Truename=request.getParameter("truename");
//out.print(Truename);
String Email=request.getParameter("email");
int id=0;
Class.forName("com.mysql.jdbc.Driver");//記載資料庫驅動,註冊到驅動管理器
String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
String username="root";
String password="";
Connection conn=DriverManager.getConnection(url,username,password);
String sql1="insert into user values(?,?,?,?,?)";
String sql2="select id from user";
PreparedStatement ps1=conn.prepareStatement(sql1);
PreparedStatement ps2=conn.prepareStatement(sql2);
ResultSet rs=ps2.executeQuery();
while(rs.next()){
id=rs.getInt(1);
}
id+=1;
ps1.setInt(1,id);
ps1.setString(2,Username);
ps1.setString(3,Password);
ps1.setString(4,Truename);
ps1.setString(5,Email);
int rtn=ps1.executeUpdate();
ps1.close();
ps2.close();
conn.close();
%>
<%
if(rtn==1){
response.sendRedirect("re_sucess.jsp");
}else{
response.sendRedirect("re_fail.jsp");
}
%>
</body>
</html>
#re_sucess(註冊成功頁面)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 're_sucess.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
註冊成功,請返回<a href="login.jsp">登入</a>
</body>
</html>
#re_fail.jsp(註冊失敗頁面)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 're_fail.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
註冊失敗,請重新<a href="register.jsp">註冊</a>!
</body>
</html>
可以看出來,用jsp檔案實現的話,整體會比較亂一些,因為資料的處理、頁面的顯示等都可以由jsp檔案來完成,所以有的時候會分不清哪個檔案是幹什麼工作的。但是jsp是比較簡單而且全能的,還是應該好好學習。
程式碼裡面其實有一些重複的地方,比如資料庫的連線,可以專門寫到一個類裡面通過呼叫類來實現資料庫的連線,減少程式碼的重複性。但是因為我太懶了,大家有興趣的自行搞一下吧~