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

相关推荐
Gain_chance10 小时前
34-学习笔记尚硅谷数仓搭建-DWS层最近一日汇总表建表语句汇总
数据仓库·hive·笔记·学习·datagrip
Gain_chance11 小时前
36-学习笔记尚硅谷数仓搭建-DWS层数据装载脚本
大数据·数据仓库·笔记·学习
Gain_chance12 小时前
35-学习笔记尚硅谷数仓搭建-DWS层最近n日汇总表及历史至今汇总表建表语句
数据库·数据仓库·hive·笔记·学习
无级程序员21 小时前
大数据Hive之拉链表增量取数合并设计(主表加历史表合并成拉链表)
大数据·hive·hadoop
华农DrLai1 天前
Spark SQL Catalyst 优化器详解
大数据·hive·sql·flink·spark
心疼你的一切1 天前
解密CANN仓库:AIGC的算力底座、关键应用与API实战解析
数据仓库·深度学习·aigc·cann
qq_12498707531 天前
基于Hadoop的信贷风险评估的数据可视化分析与预测系统的设计与实现(源码+论文+部署+安装)
大数据·人工智能·hadoop·分布式·信息可视化·毕业设计·计算机毕业设计
十月南城2 天前
Hive与离线数仓方法论——分层建模、分区与桶的取舍与查询代价
数据仓库·hive·hadoop
鹏说大数据2 天前
Spark 和 Hive 的关系与区别
大数据·hive·spark
B站计算机毕业设计超人2 天前
计算机毕业设计Hadoop+Spark+Hive招聘推荐系统 招聘大数据分析 大数据毕业设计(源码+文档+PPT+ 讲解)
大数据·hive·hadoop·python·spark·毕业设计·课程设计