登录案例---理解jsp内置对象的常用方法

getParameter(String key)获取客户端传来的参数

setAttribute(String key , String value ) 通过键值对的形式存储数据

getAttribute(String key)通过key值,取value

getRequestDispatcher  返回⼀个 RequestDispatcher 对象,该对 象的 forward ⽅法⽤于请求转发。
例如:
request.getRequestDispatcher("Welcome.jsp").forward(request,response);

将本次请求request 和 响应respond回传到Welcome.jsp(以及数据)

以下是登录案例:

将表单以post方式提交到Check.jsp文件

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2022/12/29
  Time: 10:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form method="post" action="Check.jsp">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value="登录">

</form>
</body>
</html>

接下来是Ckeck.jsp

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2022/12/29
  Time: 10:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    if(username.equals("root")&&password.equals("1234")){
        request.setAttribute("username","root");
        request.getRequestDispatcher("Welcome.jsp").forward(request,response);
    }else{
        response.sendRedirect("Login.jsp");
    }

%>
</body>
</html>

用户名和密码匹配:将用户名传到Welcome.jsp

反之,重定向到Login.jsp

接下来就是Welcom.jsp

el表达式取username的值

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2022/12/29
  Time: 10:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>欢迎回家</h1>
<h1>${username}</h1>
</body>
</html>

简单的案例理解jsp常用的内置对象,希望能对你有用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值