1.page范围
代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
pageContext.setAttribute("name","zhxy");
pageContext.setAttribute("time","9/16");
%>
姓名:${pageScope.name} <br>
时间:${pageScope.time} <br>
</body>
</html>
结果:
2.request属性范围
(1)getParameterValues
<form action="request-1.jsp" method="post">
username:<input type="text" name="user"/><br/>
password:<input type="password" name="pwd"/><br/>
<input type="submit" name="登录"/>
</form>
<%
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("user");
String pwd=request.getParameter("pwd");
System.out.println(name+","+pwd);
if("zhxy".equals(name)&&"12345".equals(pwd))
{
out.println("登录成功");
}
else
{
out.println("errors");
}
%>
获取结果
(2)request.gettAttribute(key)
<form action="request-1.jsp" method="post">
username:<input type="text" name="user"/><br/>
password:<input type="password" name="pwd"/><br/>
<input type="submit" name="登录"/>
<%
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("user");
String pwd=request.getParameter("pwd");
System.out.println(name+","+pwd);
if("zhxy".equals(name)&&"12345".equals(pwd))
{
out.println("登录成功");
request.setAttribute("age","20");
request.getRequestDispatcher("request-2.jsp").forward(request,response);
}
else
{
out.println("errors");
}
%>
欢迎,<%=request.getParameter("user") %><br/>
年龄:<%=request.getAttribute("age")%>
获取结果
3.resonpse
<%
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("user");
String pwd=request.getParameter("pwd");
System.out.println(name+","+pwd);
if("zhxy".equals(name)&&"12345".equals(pwd))
{
out.println("登录成功");
request.setAttribute("age","20");
request.getRequestDispatcher("request-2.jsp").forward(request,response);
}
else
{
out.println("errors");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.sendRedirect("response.jsp");
}
%>
<body>
登录失败!!
</body>
获取结果
4.session
<form action="session.jsp" method="post">
username:<input type="text" name="user"/><br/>
password:<input type="password" name="pwd"/><br/>
<input type="submit" name="登录"/>
</form>
<%
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("user");
String pwd=request.getParameter("pwd");
if("zhxy".equals(name)&&"12345".equals(pwd))
{
session.setAttribute("name",name);
session.setAttribute("pwd",pwd);
response.sendRedirect("session-1.jsp");
System.out.println(session.getId());
session.setMaxInactiveInterval(1000*60*30);
session.invalidate();
}
else
{
response.sendRedirect("fail.jsp");
}
%>
<%
String name=(String)session.getAttribute("name");
String pwd=(String)session.getAttribute("pwd");
out.println("登录成功");
out.println("name+","+pwd");
System.out.println(session.getId());
%>
<body>
登录失败!!
</body>
5. out
<%
out.print("hello world");
%>
结果
6.pageContext
<form action="<%=request.getContextPath()%>/session.jsp" method="post">
username:<input type="text" name="user"/><br/>
password:<input type="password" name="pwd"/><br/>
<input type="submit" name="登陆"/>
</form>
<h2>pageContext对象</h2>
<%
pageContext.getRequest();
pageContext.getResponse();
pageContext.getSession();
pageContext.getOut();
String path = request.getContextPath();
out.println("当前上下文的绝对路径:"+path);
%>
结果
7.application
<%
String serverPath = application.getContextPath();
out.println(serverPath);
application.setAttribute("", "");
%>
结果