实验十一 Servlet(二)

实验十一 Servlet(二)

【实验目的】

1.了解Servlet运行原理

2.掌握Servlet实现方式

【实验内容】

改造实验10,引入数据库,创建用户表,包括用户名和密码:客户端通过login.jsp发出登录请求,请求提交到loginServlet处理。如果用户名和密码跟用户表匹配则视为登录成功,跳转到loginSuccess.jsp页面,显示"欢迎你"+用户名;否则跳转到loginFail.jsp页面,显示"登录失败",通过超链接返回login.jsp。

说明:把用户名和密码跟用户表匹配的功能放到loginServlet并实现相应请求转发或跳转即可。可以暂时不考虑创建其它java类。
login.jsp

html 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="loginServlet" method="post">
    <label for="username">用户名:</label>
    <input type="text" id="username" name="username" required><br>
    <label for="password">密码:</label>
    <input type="password" id="password" name="password" required>
    <input type="submit" value="登陆">
</form>
</body>
</html>

loginServlet

java 复制代码
package servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.*;
import java.sql.*;
//使用@WebServlet注解
@WebServlet(name = "loginServlet" )
public class loginServlet extends HttpServlet {
    private static final String url = "jdbc:mysql://localhost:3306/test9";
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从请求中获取用户名(stuId)和密码(stuPwd)
        String stuId = request.getParameter("username");
        String stuPwd = request.getParameter("password");
        try {
            // 加载和注册JDBC驱动
            Class.forName("com.mysql.jdbc.Driver");

            Connection conn = DriverManager.getConnection(url, "root", "123456");

            String sql = "select * from password where stuId = ? and stuPwd = ?";

            //创建PreparedStatement对象,这有助于防止SQL注入攻击
            PreparedStatement stmt = conn.prepareStatement(sql);

            //使用setString方法设置SQL查询中的参数值
            stmt.setString(1,stuId);
            stmt.setString(2,stuPwd);


            ResultSet rs = stmt.executeQuery();

            if (rs.next()) {
                //从结果集中获取用户名
                String stuname = rs.getString("stuId");

                //将用户名保存到会话中
                HttpSession session = request.getSession();
                session.setAttribute("username",stuname);

                //重定向到loginSuccess.jsp页面
                response.sendRedirect("loginSuccess.jsp");
            } else {
                //重定向到loginFail.jsp页面
                response.sendRedirect("loginFail.jsp");
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

loginSuccess.jsp

html 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登陆成功</title>
</head>
<body>
    <h4>欢迎你,${sessionScope.username}</h4>
</body>
</html>

loginFail.jsp

java 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登陆失败</title>
</head>
<body>
    <h4>登录失败</h4>
    <a href="login.jsp">返回登陆页面</a>
</body>
</html>

结果:

登录成功:

登录失败:

[实验心得]

通过本次Servlet实验,理解了Servlet的运行原理。

相关推荐
yyf96012643 分钟前
hiveserver2与beeline进行远程连接hive配置及遇到的问题
数据仓库·hive
yyf9601261 小时前
hive在配置文件中添加了hive.metastore.uris之后进入hive输入命令报错
hive
jiedaodezhuti2 小时前
hive两个表不同数据类型字段关联引发的数据倾斜
数据仓库·hive·hadoop
IvanCodes2 小时前
五、Hive表类型、分区及数据加载
大数据·数据仓库·hive
镜舟科技3 小时前
什么是数据集市(Data Mart)?
数据仓库·olap·数据集市·多维数据模型·在线分析处理·定制化数据
计算机人哪有不疯的4 小时前
Hadoop的组成,HDFS架构,YARN架构概述
大数据·数据库·hadoop·spark
SelectDB技术团队19 小时前
顺丰科技:从 Presto 到 Doris 湖仓构架升级,提速 3 倍,降本 48%
大数据·数据库·数据仓库·信息可视化·数据分析·doris·实时分析
静听山水1 天前
Hive JOIN 优化策略详解
hive
Microsoft Word1 天前
数据仓库Hive
数据仓库·hive·hadoop
IvanCodes1 天前
四、Hive DDL表定义、数据类型、SerDe 与分隔符核心
大数据·hive·hadoop