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

相关推荐
大数据CLUB12 小时前
基于spark的奥运会奖牌变化数据分析
大数据·hadoop·数据分析·spark
Edingbrugh.南空12 小时前
Hadoop高可用集群搭建
大数据·hadoop·分布式
无级程序员1 天前
hive2服务启动报错:/tmp/hive on HDFS should be writable(不是chmod 777能解决的)
hive·hadoop·hdfs
rui锐rui1 天前
大数据学习2:HIve
大数据·hive·学习
凌辰揽月2 天前
Servlet学习
hive·学习·servlet
weixin_307779132 天前
Hive集群之间迁移的Linux Shell脚本
大数据·linux·hive·bash·迁移学习
王小王-1232 天前
基于Hadoop的公共自行车数据分布式存储和计算平台的设计与实现
大数据·hive·hadoop·分布式·hadoop公共自行车·共享单车大数据分析·hadoop共享单车
王小王-1232 天前
基于Hadoop的大规模文本词频统计分析系统设计与实现
hadoop·mapreduce·hadoop词频统计·hadoop文本统计·mapreduce词频统计
陈敬雷-充电了么-CEO兼CTO2 天前
推荐算法系统系列>推荐数据仓库集市的ETL数据处理
大数据·数据库·数据仓库·数据挖掘·数据分析·etl·推荐算法
桂成林3 天前
Hive UDF 开发实战:MD5 哈希函数实现
hive·hadoop·哈希算法