JSP—一个简单的登录界面

登录页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录信息title>
head>
<body>

<form action="check.jsp">
<input type="text" name="stuAccount" value=""/><br/>
<input type="password" name="stuPwd" value=""/><br/>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
form> 
body>
html>


连接数据库
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>学员登录信息title>
head>
<body>
<h1>学院信息h1>
    <%
        request.setCharacterEncoding("UTF-8"); // 设置参数字符集
        String stuAccount = request.getParameter("stuAccount");
        String stuPwd = request.getParameter("stuPwd");

        // 获取数据库连接
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = 
        DriverManager.getConnection("jdbc:mysql://localhost:3306/teach?useUnicode=true&characterEncoding=utf8","root","");
        PreparedStatement pst = conn.prepareStatement("select name, code from t_student where stuAccount=? and stuPwd=?");
        pst.setString(1, stuAccount);
        pst.setString(2, stuPwd);
        ResultSet rs = pst.executeQuery();

        String stuName = "";
        String stuCode = "";

        boolean flag = false;
        while (rs.next()) {
            stuName = rs.getString("name");
            stuCode = rs.getString("code");
            flag = true;
        }

        if(flag) {
            out.println("姓名: " + stuName + " 学号: " + stuCode);
        } else {
            out.println("没有找到相关数据!请重新登录");
        }

        pst.close();
        conn.close();
    %>
body>
html>

你可能感兴趣的:(周末个人练习)