首页 > 编程 > JSP > 正文

jsp内置9个对象和4个作用域(一)

2019-11-06 06:24:21
字体:
来源:转载
供稿:网友

备注: jsp的forward跳转界面指令 ,地址不变

<%@ 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>

程序猿的记录。。。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表