Jsp内置对象
重点:
各个对象的生命周期
各种页面间传值的方式
Jsp有九个内置对象,包括:
对象名 类型 作用域
request javax.servlet.ServletRequest Request
response javax.servlet.ServletResponse Page
pageContext javax.servlet.jsp.PageContext Page
session javax.servlet.http.HttpSession Session
application javax.servlet.SevletContext Application
out javax.servlet.jsp.JspWriter Page
config javax.servlet.ServletConfig Page
page java.lang.Object Page
exception java.lang.Throwable Page
***********************************************************************
主要的三个对象
request
主要的方法有:
String getParameter(String name) 多用于值唯一的情况
Enumeration getParameterNames()
String[] getParameterValues(String name) 多用于值有多个时,比如复选框里的勾选内容
Object getAttribute(String name) 用于用户向服务器端发出请求时,传递参数,必须是同一个请求时才有效。可以一次请求多页面传递。
用户的每一个请求都会产生一个request对象。请求结束时,request对象被销毁,也就没有attribute了。当发出下一个请求时,生成了另外一个request对象。
void setAttribute(String name,Object o)
request1.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'request1.jsp' starting page</title> </head> <body> <form action="request2.jsp"> username: <input type="text" name="username"/><br> <input type="submit" value="submit"/> </form> </body> </html> |
Request2.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'request2.jsp' starting page</title> </head> <body> <% String
username = request.getParameter("username"); request.setAttribute("username",username); %> username: <%=username %> <jsp:forward page="request3.jsp"></jsp:forward> </body> </html> |
Request3.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'request3.jsp' starting page</title> </head> <body> <% String
username = (String)request.getAttribute("username"); %> <%=username %> </body> </html> |
session
http是无状态的协议
web server对每一个客户端的请求都没有历史记忆
session用来保存当前客户端状态信息
主要的方法有:
Object getAttribute(String name)
void setAttribute(String name,Object value)
只要当前连接没有关闭,或session没有到期,session里的数据就不丢失(序列化到硬盘的情况除外)
session1.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'session1.jsp' starting page</title> </head> <body> <form action="session2.jsp"> username: <input type="text" name="username"/><br> <input type="submit" value="submit"/> </form> </body> </html> |
Session2.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'session2.jsp' starting page</title> </head> <body> <% String
username = request.getParameter("username"); session.setAttribute("name",username); %> your name "<%=username %>" is saved in session<br> <a href="session3.jsp?name=<%=username %>">check</a> </body> </html> |
Session3.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'session3.jsp' starting page</title> </head> <body> <% String
username = (String)session.getAttribute("name"); if(username == null) { out.println("你还没有登录"); } else { out.println(username
+ "已经登录"); } %> </body> </html> |
执行结果
application
Object getAttribute(String name)
void setAttribute(String name,Object object)
application.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'application.jsp' starting page</title> </head> <body> <% int count = 0; if(application.getAttribute("count")==null) { application.setAttribute("count",1); count
= 1; } else { String
icount = application.getAttribute("count").toString(); count
= Integer.parseInt(icount); count++; application.setAttribute("count",count); } out.println("你是第"+count +"个访问者"); %> </body> </html> |
存活范围最大的对象,只要服务器没有关闭,application的数据会一直存在,会被所有的用户共享。
其他常用的方法
<%@ page language="java"
import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'application2.jsp' starting page</title> </head> <body> <% out.println("java servlet API version: <b>" +
application.getMajorVersion() + "." +
application.getMinorVersion() + "</b><br>"); out.println("URL of 'application2.jsp' is: <b>" +
application.getResource("/application2.jsp") + "</b><br>"); out.println("getServerInfo() = <b>" + application.getServerInfo() + "</b><br>"); out.println("<b>" + application.getRealPath(" ") + "</b>"); %> </body> </html> |
输出结果为:
java servlet API version: 2.5 |
Forward机制
<jsp:forward>
会将用户的请求转发到其他页面,传递的是始终是同一个请求。
在请求期间,可以用request对象传递数据,但是请求结束,数据就会销毁。
myForward.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'myForward.jsp' starting page</title> </head> <body> <form action="MyFormServlet"> username: <input type="text" name="username"/><br> <input type="submit" value="submit"/> </form> </body> </html> |
MyFormServlet.jsp
public class MyFormServlet extends HttpServlet { public void doGet(HttpServletRequest
request, HttpServletResponse response) throws ServletException, IOException { String username= request.getParameter("username"); request.setAttribute("username",username); List<String> list = new
ArrayList<String>(); for(int i=0;i<100;i++) { list.add(i+""); } request.setAttribute("list",list); RequestDispatcher rd =
request.getRequestDispatcher("myresult.jsp"); rd.forward(request,response); } } |
Myresult.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'myresult.jsp' starting page</title> </head> <body> username: <%=request.getAttribute("username")%> <br> <% List<String> list = (List<String>)
request.getAttribute("list"); for (int i = 0; i < list.size(); i++) { out.println(list.get(i) + "<br>"); } %> </body> </html> |
Servlet主要进行流程处理,包括派发请求并增加额外处理等。
jsp主要处理页面显示的问题。
************************************************************************
Asp.net常用的页面传值的方式:(也是实际开发中常用的方式)
<input type=”hidden”/>
Hidden1.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'hidden1.jsp' starting page</title> </head> <body> <form action="hidden2.jsp" method="post"> please input your name: <input type="text" name="username"
/> <input type="submit" value="submit"
/> </form> </body> </html> |
Hidden2.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'hidden2.jsp' starting page</title> </head> <body> <% String username =
request.getParameter("username"); %> username: <%=username%> <form method="post" action="hidden3.jsp"> please input your like: <input type="text" name="like"
/> <input type="hidden" name="username"
value="<%=username%>" /> <input type="submit" value="submit"/> </form> </body> </html> |
Hidden3.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML
PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'hidden3.jsp' starting page</title> </head> <body> <% String username =
request.getParameter("username"); String like = request.getParameter("like"); %> your name is : <%=username%> <br/> your like is : <%=like%> </body> </html> |