JSP实例1.用户登录程序实现(JSP and mysql)

一.程序实现思路

1.首先我们要在数据库中建立一个表
2.我们要写四个jsp文件:
(1)登录表单页面。
(2)检测页面。
(3)登录成功页面
(4)登录失败页面。

二.具体实现过程:

1.首先在mysql中建立一个表且插入一组数据:

mysql> use jdbc;
Database changed
mysql> create table user(
    -> userid varchar(30) primary key,
    -> name   varchar(30) not null,
    -> password varchar(32) not null);
Query OK, 0 rows affected (0.23 sec)

mysql> insert into user(userid,name,password)
    -> values
    -> (01,'yanjiad',1314520);
Query OK, 1 row affected (0.10 sec)

mysql> select * from user;
+--------+---------+----------+
| userid | name    | password |
+--------+---------+----------+
| 1      | yanjiad | 1314520  |
+--------+---------+----------+
1 row in set (0.00 sec)

2.在Tomcat 文件夹中的lib目录下添加MySQL驱动包:
保证服务器能访问到数据库
在这里插入图片描述
3.四个页面的编写:
(1)登录页面 :JaveWeb01.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页面</title>
</head>
<body>
<center>
 <h1>登录操作</h1>
 <hr>
 <form action="login_check.jsp" method="post">
  <table border="1">
     <tr>
       <td colspan="2">用户登录</td>
     </tr>
     <tr>
        <td>登录ID:</td>    
        <td><input tepy="text" name="id"></td>
     </tr>
     <tr>
      <td>登录密码:</td>    
       <td><input tepy="password" name="password"></td>
     </tr>
     <tr>
        <td colspan="2">
         <input type="submit" value="登录">
         <input type="reset" value="重置">
        </td>    
     </tr>

(2)检测页面:login_check.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!
 public static final String dbdriver="com.mysql.cj.jdbc.Driver";
public static final String dburl="jdbc:mysql://localhost:3306/jdbc?&useSSL=false&serverTimezone=UTC";
public static final String dbuser="root";
public static final String dbpass="root";
%>
<%
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
boolean flag=false;
String name=null;

%>
<%
try{
	Class.forName(dbdriver);
	conn=DriverManager.getConnection(dburl,dbuser,dbpass);
	
	String sql = "select name from user where userid=?and password=?";
	pstmt=conn.prepareStatement(sql);
	pstmt.setString(1,request.getParameter("id"));
	pstmt.setString(2,request.getParameter("password"));
	rs=pstmt.executeQuery();
	if(rs.next()){
		
		name=rs.getString(1);
		flag=true;
	}
}catch(Exception e){
	System.out.println(e);
}

finally{
	try{
		rs.close();
		pstmt.close();
		conn.close();
	}catch(Exception e){}
}

%>
<%
  if(flag){
%>
<jsp:forward page="login_success.jsp">

<jsp:param value="<%=name %>" name="uname"/>

</jsp:forward>
<%
  }else{
%>
<jsp:forward page="login_failure.jsp"/>
<%} %>
</body>
</html>

(3).成功页面:login_success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>

<h1>登录操作</h1>
<h2>登录成功</h2>
<h3>欢迎</h3>
</center>
</body>
</html>

(4)失败页面:login_failure.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>登录操作</h1>
<h2>登录失败,请重新登录<a href="JaveWeb01.jsp">登录</a></h2>
</center>
</body>
</html>

结果:在这里插入图片描述
在这里插入图片描述

发布了73 篇原创文章 · 获赞 1 · 访问量 2447

猜你喜欢

转载自blog.csdn.net/c1776167012/article/details/105076285
今日推荐