jsp内置9个对象和4个作用域(一)
2017-03-06 22:13
477 查看
备注: jsp的forward跳转界面指令 ,地址不变
target.jsp的文件
pageContext对象,用的方法是:pageContext.setAttribute(“关键字”,”值”);和pageContext.getAttribute(“关键字”);
2.request对象。常调用的方法是request.setAttribute(“key”,”value”);
3.response对象的方法用于客户端跳转界面。地址改变response.sendRedirect(“index.html”);
4.session对象
5.application对象:
Cookie内容
程序猿的记录。。。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- 界面跳转到target.jsp--> <jsp:forward page="target.jsp"> <jsp:param value="123456" name="java"/> <jsp:param value="i love you" name="secret"/> </jsp:forward> </body> </html>
target.jsp的文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>服务器跳转到targe.jsp界面</h1> java=<%=request.getParameter("java")%> <br/> secret=<%=request.getParameter("secret") %> <br/> </body> </html>
pageContext对象,用的方法是:pageContext.setAttribute(“关键字”,”值”);和pageContext.getAttribute(“关键字”);
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% //设置两个page范围的数据(key,value) pageContext.setAttribute("name","rocky"); pageContext.setAttribute("age", 10); %> <% //取值 String name=(String)pageContext.getAttribute("name"); int age=(Integer)pageContext.getAttribute("age"); %> <p>姓名: <%= name %> </p> <p> 年龄: <%= age %> </p> </body> </html>
2.request对象。常调用的方法是request.setAttribute(“key”,”value”);
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% //设置两个值,格式分别是key----value; request.setAttribute("boss", "宋江"); request.setAttribute("skill","money"); request.setAttribute("position",1); %> <% //取值 String boss=(String)request.getAttribute("boss"); String skill=(String)request.getAttribute("skill"); int position=(Integer)request.getAttribute("position"); %> <h1>boss is: <%=boss%> </h1> <h2>his skill is<%=skill %></h2> <h3>his position is <%=position %></h3> <jsp:forward page="requestTarget.jsp"></jsp:forward> </body> </html>
3.response对象的方法用于客户端跳转界面。地址改变response.sendRedirect(“index.html”);
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.* " %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% //客户端跳转界面,地址改变为index.html,重定向 response.sendRedirect("index.html"); %> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>hello.i miss you</h1> </body> </html>
4.session对象
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% //设置两个值,格式分别是key----value; session.setAttribute("Hero", "诸葛亮"); session.setAttribute("skill","wisdom"); session.setAttribute("position",1); %> <h1>session设置完毕</h1> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% //取值 String hero=(String)session.getAttribute("Hero"); String skill=(String)session.getAttribute("skill"); int position=(Integer)session.getAttribute("position"); %> <h1>hero是:<%=hero %></h1> <h1>他的技能是:<%=skill %></h1> <h1>他的风云榜单是:<%=position%></h1> </body> </html>
5.application对象:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% application.setAttribute("dream","enabled"); application.setAttribute("action","ing..."); %> <p>application 设置完毕</p> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% //取值 String dream=(String)application.getAttribute("dream"); String action=(String)application.getAttribute("action"); %> <p>我的梦想是:<%=dream %></p><br/> <p>我的行动是:<%=action%></p> </body> </html>
Cookie内容
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.util.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> <script type="text/javascript"> function resetValue(){ document.getElementById("userName").value=""; document.getElementById("pwd").value=""; } </script> <% String userName=null; String pwd=null; Cookie[] cookies=request.getCookies();//浏览器自带的cookie for(int i=0;cookies!=null &&i<cookies.length;i++){ if(cookies[i].getName().equals("userNameAndPwd")){ userName=cookies[i].getValue().split("-")[0]; pwd=cookies[i].getValue().split("-")[1]; } } if(userName==null){ userName=""; } if(pwd==null){ pwd=""; } %> </head> <body> <form action="userLogin.jsp" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" id="userName" name="userName" value="<%=userName%>"/></td> </tr> <tr> <td>密码:</td> <td><input type="password" id="pwd" name="pwd" value="<%=pwd %>" /></td> </tr> <tr> <td>记住密码:</td> <td><input type="checkbox" id="remember" name="remember" value="remember-me"/></td> </tr> <tr> <td><input type="submit" value="登录"/></td> <td><input type="button" value="重置" onclick="resetValue()"/></td> </tr> </table> </form> </body> </html>
<!--userLogin.jsp文件--> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="javax.servlet.http.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% String userName=request.getParameter("userName"); // 获取用户名 String pwd=request.getParameter("pwd"); // 获取密码 String remember=request.getParameter("remember"); // 获取记住密码 if("remember-me".equals(remember)){ Cookie userNameAndPwd=new Cookie("userNameAndPwd",userName+"-"+pwd); userNameAndPwd.setMaxAge(1*60*60*24*7); // cookie记录一个星期 response.addCookie(userNameAndPwd); // 保存cookie System.out.println("设置Cookie成功"); } response.sendRedirect("response03.jsp"); %> </body> </html>
程序猿的记录。。。
相关文章推荐
- Jsp 9个内置对象和4个作用域简介
- jsp的4个作用域和9个内置对象
- JSP整理总结--9大内置对象、4个作用域
- jsp中9个内置对象与servlet对应关系及四个作用域
- JSP 4个域对象-9个内置对象-11个EL隐式对象
- jsp的9个内置对象及各自的作用?
- JSP的9大内置对象和4个作用域
- JSP的9大内置对象和4个作用域
- jsp中9大内置对象以及4个作用域
- jsp九个内置对象及4个作用域
- jsp中9个内置对象与servlet对应关系及四个作用域
- JSP的9大内置对象及4个作用域
- JSP 4个域对象-9个内置对象-11个EL隐式对象
- JSP 4个域对象-9个内置对象-11个EL隐式对象
- JavaWeb(三)JSP之3个指令、6个动作、9个内置对象和4大作用域
- JSP整理总结--9大内置对象、4个作用域
- JSP 4个域对象、7个动作指令、9个内置对象
- (六)jsp9大内置对象和4个作用域
- jsp中的内置对象(9个)、作用
- jsp有哪些内置对象?作用分别是什么?