javaEE之jsp+JavaBean实现登录+注册+留言功能(外挂数据库)

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'">&nbsp;
    <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是比较简单而且全能的,还是应该好好学习。
代码里面其实有一些重复的地方,比如数据库的连接,可以专门写到一个类里面通过调用类来实现数据库的连接,减少代码的重复性。但是因为我太懒了,大家有兴趣的自行搞一下吧~

猜你喜欢

转载自blog.csdn.net/weixin_43699616/article/details/84100825