实验十一 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的运行原理。

相关推荐
Zhu7581 天前
在k8s集群环境部署高可用的Apache Hadoop3.1.1定制版集群
hadoop·kubernetes
Y3815326621 天前
3 种 SERP 数据 ETL 方案对比:实时 / 定时 / 流式
数据仓库·etl
还有你Y2 天前
软件工程架构
hadoop·架构·软件工程
向夏威夷 梦断明暄2 天前
基于Tez引擎的 Hive SQL 性能优化
hive·sql·性能优化
这个DBA有点耶2 天前
交易型数据库是什么?OLTP核心能力与2026选型指南
数据库·数据仓库·sql·database·数据库架构·olap·dba
RestCloud4 天前
Informatica迁移国产ETL完整实施指南:ETLCloud自动化平滑替换方案
数据仓库·etl·etlcloud·数据传输·数据集成工具·informatica·国产化替代
德昂信息dataondemand4 天前
全量还是增量?聊聊数仓ETL中的数据同步策略
数据仓库·etl
观远数据5 天前
数据集成平台选型战卡:DataFlow对比传统ETL的5个维度与红线排除项
数据仓库·etl
Database_Cool_5 天前
云数据仓库开通指南:阿里云 AnalyticDB MySQL 10 分钟从 0 到分析实战教程
数据仓库·mysql·阿里云