Javaweb's first project - to achieve a simple login function

Step 1: Open idea-->file-->new

 

Step two:

Right-click on the Demo folder --> Add Framework Support --> Find the Web application and check it 

 Step 3: Configure Tomcat

 

 

 

 Step 4: Create a new lib (under the web-INF folder) folder for storing jar packages

 You can first import mysql-connector for subsequent connection to the database

After importing, right-click the lib folder and find "Add as library" to use it later

 

Right click on the web file--"New--"jsp file

 

get_info.jsp


<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>这是连接MySQL数据库的测试</title>
</head>
<body>
<%
    //加载驱动程序
    String driverName="com.mysql.jdbc.Driver";
    //数据库信息
    String userName="root";
    //密码
    String userPasswd="";
    //数据库名
    String dbName="Student";
    //表名
    String tableName="stu_info";
    //将数据库信息字符串连接成为一个完整的url(也可以直接写成url,分开写是明了可维护性强)

    String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn=DriverManager.getConnection(url);
    Statement stmt = conn.createStatement();
    String sql="SELECT * FROM "+tableName;
    ResultSet rs = stmt.executeQuery(sql);
%>


<table>
    <tr>
        <td>学号</td>
        <td>姓名</td>
        <td>电话号码</td>
    </tr>


    <%
        while(rs.next()) {
            String no = rs.getString(1);
            String name=rs.getString(2);
            String phone=rs.getString(3);


    %>
    <tr>
        <td> <%=no%></td>
        <td> <%=name%></td>
        <td> <%=phone%></td>
    </tr>

    <%
        }
    %>
</table>

<%
    out.print("<br>");
    out.print("ok, Database Query Successd!");
    rs.close();
    stmt.close();
    conn.close();
%>
</body>
</html>

 check.jsp

<%@ page pageEncoding="UTF-8" %>    <%-- 设置显示编码 --%>
<%@ page import="dbc.DatabaseConnection"%> <%-- 负责数据库的连接 --%>
<%@ page import="java.sql.*" %> <%-- 负责JDBC的开发操作 --%>
<html>
<head>
    <title>用户登录案例</title>
</head>
<body>
<%  // 本次直接通过JDBC编写数据库的认证操作
    String name = null; // 保存最终用户的姓名
    String mid = request.getParameter("name"); // 接收mid的请求参数
    String password = request.getParameter("password"); // 接收password请求参数
    String sql = "SELECT name FROM user WHERE name=? AND password=?";
    PreparedStatement pstmt = DatabaseConnection.getConnection().prepareStatement(sql);
    pstmt.setString(1, mid);
    pstmt.setString(2, password);
    ResultSet rs = pstmt.executeQuery(); // 执行数据查询
    if (rs.next()) {    // 有数据返回
        name = rs.getString(1); // 保存姓名
    }
    DatabaseConnection.close(); // 关闭数据库连接
%>
<%
    if (name == null) { // 登录失败,没有查询到name字段
%>      <%-- 如果现在存在有这个参数就表示出错了 --%>
        <jsp:forward page="failer.jsp"/>
<%
    } else {
%>      <%-- 登录成功之后将姓名传递到欢迎页上进行显示 --%>
        <jsp:forward page="success.jsp">
            <jsp:param name="name" value="<%=name%>"/>
        </jsp:forward>
<%
    }
%>
</body>
</html>

 login.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=ISO-8859-1">
<title>登录</title>
</head>
<body>
<form action="check.jsp" method="post">

用户名:<input type="text" name = "name"><br>
密码:<input type="password" name = "password"><br>

<input type="submit" value="登录">

</form>
</body>
</html>

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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>欢迎<%= request.getParameter("name")%>登录</h1>
</body>
</html>

 failer.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>登录失败了,<a href="login.jsp">点击重新返回首页</a></h1>
</body>
</html>

After the project is written, import the relevant jar package to run and realize the login interface 

Guess you like

Origin blog.csdn.net/weixin_62440328/article/details/129349202