javaweb realizes login, registration, password modification, information display, and personal information modification functions

Implementation of user registration and login

Write JSP program to realize the basic functions of user registration, login, logout, change password and display and modify basic user information.
Judge whether the user has logged in through the session. If you are not logged in, please provide the registration and login functions; if you are logged in, please display the user ID, name, age and other information, and provide the logout, password modification, and basic user information functions.

数据库名请用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)
);

Project engineering documents:

Project Files

login.jsp

On the user's login interface, click the login account and password and then go to yanzheng.jsp to verify that the account password is correct. Click register to go to zhuce.jsp to complete the registration function

<%@ 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>		

Insert picture description here

zhuce.jsp

Register and fill in the information interface, after filling in the information, click register to go to insert.jsp to complete the data insertion operation of the database

<%@ 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 picture description here

insert.jsp

Insert new data in the database

<%@ 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

Verify whether there is a corresponding account password in the database, if so, set the corresponding session attributes, and go to 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

Display the relevant information of the user. If you want to modify the personal information, click the modify button after modifying it directly. To modify the password, click Modify Password, to log out, click the logout button

<%@ 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>

Insert picture description here

xiugai.jsp

Connect to the database, update the database

<%@ 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

Logout session

<%@ 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

Modify the display interface of the password, and use js to detect whether the password entered twice is consistent

<%@ 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>

Insert picture description here

changepsw1.jsp

Connect to the database to update the password

<%@ 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>

database:

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_39139505/article/details/90691789