javaweb实现登陆,注册,修改密码,显示信息,修改个人信息功能

用户注册和登录的实现

编写JSP程序实现用户注册、登录、注销、修改密码和显示及修改用户基本信息等基本功能。
通过session判断用户是否已经登录。如果未登录,请提供注册和登录功能;如果已登录,请显示用户ID,姓名,年龄等信息,并请提供注销、修改密码,修改用户基本信息功能。

数据库名请用company和表名请用myuser,字段名和类型请严格参照如下SQL语句。
create database if not exists company;
use company;
create table if not exists myuser (
	id varchar(10),
	name varchar(20),
    psw varchar(20),
	age int,
    phone varchar(20),
	address varchar(50),
	city varchar(20),
	constraint primary key pk_myuser(id)
);

项目工程文件:

工程文件

login.jsp

用户的登陆界面,点击登陆填写的账号密码以post的方式转到yanzheng.jsp验证账号密码是否正确。点击注册转到zhuce.jsp完成注册功能

<%@ page contentType="text/html;charset=gb2312" language="java" %>
<html>
<head>
<title>用户登陆界面</title>
</head>
 <body>
 	<%
 	if(!session.isNew())
 	{
 		String name=(String)session.getAttribute("username");
 		if(name==null) name="";
 	}
 	%>
 	<table border="1" align="center">
 		<caption>用户登陆:</caption>
 		<form action="yanzheng.jsp" method="post">
 		<tr>
 			<td>用户名</td>
 			<td><input type="text" name="username"/></td>
 		</tr>
 		<tr>
 			<td>密码</td>
 			<td><input type="password" name="password"/></td>
 		</tr>
 		<tr>
 			<td><input type="submit" value="登陆" /></td>
 			<td><input type="button" value="注册" onclick="window.location.href='zhuce.jsp';">
 		</tr>
 		</form>
 	</table>
 </body>
 </html>		

在这里插入图片描述

zhuce.jsp

注册填写信息界面,信息填好以后点击注册转到insert.jsp完成数据库的数据插入操作

<%@ page contentType="text/html;charset=gb2312" language="java" %>
<%@page import="java.sql.*"%>
<html>
<head>
<title>注册用户</title>
</head>
<body>
<table border="1">
  <caption>
      信息填写:
  </caption>
  <form action="insert.jsp" method="post">
    <tr>
      <td>id:</td>
      <td><input type="text" name="id"></td>
    </tr>
    <tr>
      <td>密码:</td>
      <td><input type="password" name="psw"></td>
    </tr>
    <tr>
      <td>姓名:</td>
      <td><input type="text" name="name"></td>
    </tr>
    <tr>
      <td>年龄:</td>
      <td><input type="text" name="age"></td>
    </tr>
    <tr>
      <td>tel:</td>
      <td><input type="text" name="phone"></td>
    </tr>
    <tr>
      <td>地址:</td>
      <td><input type="text" name="address"></td>
    </tr>
    <tr>
      <td>城市:</td>
      <td><input type="text" name="city"></td>
    </tr>
    <tr>
    	<td><input type="submit" value="注册" /></td>
    </tr>
 </form>
</table>

</body>
</html>
	
	

在这里插入图片描述

insert.jsp

在数据库中插入新数据

<%@ page contentType="text/html;charset=gb2312" language="java" %>
<%@page import="java.sql.*"%>
<html>
<head>
<title>插入数据</title>
</head>
<body>
<%
try{
	String username=request.getParameter("name");
	String id=request.getParameter("id");
	String phone=request.getParameter("phone");
	String psw=request.getParameter("psw");
	int age=Integer.parseInt(request.getParameter("age"));
	String address=request.getParameter("address");
	String city=request.getParameter("city");
	//装载驱动程序
	Class.forName("org.gjt.mm.mysql.Driver").newInstance();
	//连接字符串   
	String url ="jdbc:mysql://localhost:3306/company?useSSL=FALSE&serverTimezone=GMT"; 
	//建立连接
	Connection conn= DriverManager.getConnection(url,"root","123456"); 
	//使用preparedstatement
	PreparedStatement pstmt=conn.prepareStatement("insert into myuser values(?,?,?,?,?,?,?)");
	pstmt.setString(1,id);
	pstmt.setString(2,username);
	pstmt.setString(3,psw);
	pstmt.setInt(4,age);
	pstmt.setString(5,phone);
	pstmt.setString(6,address);
	pstmt.setString(7,city);
	pstmt.execute();
	session.invalidate();
	response.sendRedirect("login.jsp");
	//关闭连接、释放资源
    pstmt.close();
    conn.close();
	}catch(ClassNotFoundException cnfe){
  	out.print(cnfe);
  }catch(SQLException sqle){
    out.print(sqle);
  }catch(Exception e){
    out.print(e);
  }
	%>
</body>
</html>

yanzheng.jsp

验证数据库中是否有对应的账号密码,有的话设置对应的会话属性,并转到xianshi.jsp

<%@ page contentType="text/html;charset=gb2312" language="java" %>
<%@page import="java.sql.*"%>
<html>
<head>
<title>用户身份验证</title>
</head>
<body>
	<%
try{
	String username=request.getParameter("username");
	String password=request.getParameter("password");
	//装载驱动程序
	Class.forName("org.gjt.mm.mysql.Driver").newInstance();
	//连接字符串   
	String url ="jdbc:mysql://localhost:3306/company?useSSL=FALSE&serverTimezone=GMT"; 
	//建立连接
	Connection conn= DriverManager.getConnection(url,"root","123456"); 
	//建立Statement
	Statement stmt=conn.createStatement();
	//执行查询建立ResultSet
	ResultSet rs=stmt.executeQuery("select * from myuser");
	if(username==null) username="";
	if(password==null) password="";
	String flag="111";
	while(rs!=null && rs.next())
	{
		if(username.equals(rs.getString("id")) && password.equals(rs.getString("psw")))
		{
			session.setAttribute("username",rs.getString("id"));
			session.setAttribute("age",rs.getString("age"));
			session.setAttribute("psw",rs.getString("psw"));
			session.setAttribute("name",rs.getString("name"));
			session.setAttribute("phone",rs.getString("phone"));
			session.setAttribute("address",rs.getString("address"));
			session.setAttribute("city",rs.getString("city"));
			response.sendRedirect("xianshi.jsp");
			flag="222";
			break;
		}
	}
	if(flag!="222")
		response.sendRedirect("login.jsp");
	
	//关闭连接、释放资源
    rs.close();
    stmt.close();
    conn.close();
	}catch(ClassNotFoundException cnfe){
  	out.print(cnfe);
  }catch(SQLException sqle){
    out.print(sqle);
  }catch(Exception e){
    out.print(e);
  }
	%>
</body>
</html>

xianshi.jsp

显示用户的相关信息,若要修改个人信息,直接修改后点击修改按钮。要修改密码则点击修改密码,要注销登陆则点击注销按钮

<%@ page contentType="text/html;charset=gb2312" language="java" errorPage="" %>
<html>
<head>
<title>用户欢迎页面</title>
</head>
<body>
 	<%
 	String username=(String)session.getAttribute("username");
 	String name=(String)session.getAttribute("name");
 	String age=(String)session.getAttribute("age");
 	String psw=(String)session.getAttribute("psw");
 	String phone=(String)session.getAttribute("phone");
 	String address=(String)session.getAttribute("address");
 	String city=(String)session.getAttribute("city");
 	out.println("欢迎:"+name+"来到这里!");
 	%>
 	<table border="1" align="center">
  <caption>
      用户信息:
  </caption>
  <form action="xiugai.jsp" method="post">
    <tr>
      <td>id:</td>
      <td><input type="text" name="id" value=<%=username %> readonly="true"></td>
    </tr>
    <tr>
      <td>密码:</td>
      <td><input type="password" name="psw"value=<%=psw%> readonly="true"></td>
    </tr>
    <tr>
      <td>姓名:</td>
      <td><input type="text" name="name"value=<%=name%>></td>
    </tr>
    <tr>
      <td>年龄:</td>
      <td><input type="text" name="age"value=<%=age%>></td>
    </tr>
    <tr>
      <td>tel:</td>
      <td><input type="text" name="phone"value=<%=phone%>></td>
    </tr>
    <tr>
      <td>地址:</td>
      <td><input type="text" name="address"value=<%=address%>></td>
    </tr>
    <tr>
      <td>城市:</td>
      <td><input type="text" name="city"value=<%=city%>></td>
    </tr>
    <tr>
    	<td><input type="submit" value="修改" onclick="window.location.href='xiugai.jsp';"></td>
    	<td><input type="button" value="注销" onclick="window.location.href='zhuxiao.jsp';"></td>
    </tr>
    <tr>
    	<td><input type="button" value="修改密码" onclick="window.location.href='changepsw.jsp';"></td>
    </tr>
 </form>
</table>
 </body>
 </html>

在这里插入图片描述

xiugai.jsp

连接数据库,对数据库进行更新

<%@ page contentType="text/html;charset=gb2312" language="java" %>
<%@page import="java.sql.*"%>
<html>
<head>
<title>修改数据</title>
</head>
<body>
<%
try{
	String username=request.getParameter("name");
	String id=request.getParameter("id");
	String phone=request.getParameter("phone");
	String psw=request.getParameter("psw");
	int age=Integer.parseInt(request.getParameter("age"));
	String address=request.getParameter("address");
	String city=request.getParameter("city");
	//装载驱动程序
	Class.forName("org.gjt.mm.mysql.Driver").newInstance();
	//连接字符串   
	String url ="jdbc:mysql://localhost:3306/company?useSSL=FALSE&serverTimezone=GMT"; 
	//建立连接
	Connection conn= DriverManager.getConnection(url,"root","123456"); 
	//建立statement
	Statement stmt=conn.createStatement();
	//执行update
	String sql1="update myuser set address='"+address+"'where id='"+id+"'";
	stmt.executeUpdate(sql1);
	String sql2="update myuser set phone='"+phone+"'where id='"+id+"'";
	stmt.executeUpdate(sql2);
	String sql3="update myuser set age='"+age+"'where id='"+id+"'";
	stmt.executeUpdate(sql3);
	String sql4="update myuser set city='"+city+"'where id='"+id+"'";
	stmt.executeUpdate(sql4);
	String sql5="update myuser set name='"+username+"'where id='"+id+"'";
	stmt.executeUpdate(sql5);
	session.invalidate();
	response.sendRedirect("login.jsp");
    conn.close();
	}catch(ClassNotFoundException cnfe){
  	out.print(cnfe);
  }catch(SQLException sqle){
    out.print(sqle);
  }catch(Exception e){
    out.print(e);
  }
	%>
</body>
</html>

zhuxiao.jsp

注销会话

<%@ page contentType="text/html;charset=gb2312" language="java" %>
<html>
<head>
<title>用户欢送页面</title>
</head>

<body>
	<%
	String name=(String)session.getAttribute("name");
	session.invalidate();
	out.println(name+"再见!欢迎再来!");
	%>
	<a href="login.jsp">去登陆 </a>
</body>
</html>

changepsw.jsp

修改密码的显示界面,并用js检测两次输入的密码是否一致

<%@ page contentType="text/html;charset=gb2312" language="java" %>
<%@page import="java.sql.*"%>
<html>
<head>
<title>修改密码</title>
</head>
<body>
	<table border="1" align="center">
 		<caption>密码修改:</caption>
 		<form name="form1" onsubmit="return userCheck()" action="changepsw1.jsp" method="post">
 		<tr>
 			<td>密码</td>
 			<td><input id="psw1" type="password" name="password1"/></td>
 		</tr>
 		<tr>
 			<td>确认密码</td>
 			<td><input id="psw1" type="password" name="password2"/></td>
 		</tr>
 		<tr>
 			<td><input type="submit" value="提交" ></td>
 			<td><input type="reset" value="重置"></td>
 		</tr>
 		</form>
 	</table>
 	<script  type="text/javascript">
 	function userCheck()
 	{
 	   var pass1=document.form1.password1.value;
 	   var pass2=document.form1.password2.value;
 	   if(pass1=="" || pass2=="")
 	   {
 		   window.alert("请填写密码");
 		   return false;
 	   }
 	   else if(pass1!=pass2)
 		{
 		   window.alert("请重新填写密码,两次得密码不正确");
 		   return false;
 		}
 	}
 	  </script>
 </body>
 </html>

在这里插入图片描述

changepsw1.jsp

连接数据库更新密码

<%@ page contentType="text/html;charset=gb2312" language="java" %>
<%@page import="java.sql.*"%>
<html>
<head>
<title>修改密码</title>
</head>
<body>
<script type="text/javascript">
	alert("密码修改成功");
	</script>
<%
try{
	String pass=request.getParameter("password1");
	String id=(String)session.getAttribute("username");
	//装载驱动程序
	Class.forName("org.gjt.mm.mysql.Driver").newInstance();
	//连接字符串   
	String url ="jdbc:mysql://localhost:3306/company?useSSL=FALSE&serverTimezone=GMT"; 
	//建立连接
	Connection conn= DriverManager.getConnection(url,"root","123456"); 
	//建立statement
	Statement stmt=conn.createStatement();
	//执行update
	String sql1="update myuser set psw='"+pass+"'where id='"+id+"'";
	stmt.executeUpdate(sql1);
	session.invalidate();
	response.sendRedirect("login.jsp");
	//关闭连接、释放资源
    stmt.close();
    conn.close();
	}catch(ClassNotFoundException cnfe){
  	out.print(cnfe);
  }catch(SQLException sqle){
    out.print(sqle);
  }catch(Exception e){
    out.print(e);
  }
	%>
</body>
</html>

数据库:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_39139505/article/details/90691789