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常用的内置对象,希望能对你有用