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

相关推荐
WHYBIGDATA4 小时前
Hive之数据定义DDL
大数据·数据库·hive·hadoop
一水鉴天7 小时前
为AI聊天工具添加一个知识系统 之86 详细设计之27 数据处理:ETL
数据仓库·etl
编程指南针13 小时前
基于Hadoop实现气象分析大屏可视化项目【源码+LW+PPT+解析】
大数据·hadoop·分布式·气象分析
码农幻想梦16 小时前
实验十 Servlet(一)
hive·hadoop·servlet
赛博末影猫17 小时前
Maven(Ⅱ):依赖范围,依赖传递,依赖阻断,可选依赖
数据库·hive·maven
乙卯年QAQ18 小时前
【Hadoop】Hadoop的HDFS
大数据·hadoop·hdfs
STONE_KKK1 天前
Hive重点面试题
数据仓库·hive·hadoop
想做富婆2 天前
Hive:日志,hql运行方式,Array,行列转换
数据仓库·hive·hadoop