jsp的九大内置对象为:
pageContext
request
response
session
out
page
application
config
exception
一、pageContext的使用
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>example01</title>
</head>
<body>
<!-- pageContext使用,存入一个键值对 -->
<%
pageContext.setAttribute("users","王祖贤");
HttpServletRequest request1=(HttpServletRequest)pageContext.getRequest();
request1.setAttribute("goods","打印机");
HttpSession session1=(HttpSession) pageContext.getSession();
session1.setAttribute("animals","十块");
%>
<!-- 访问pageContext的值 -->
页面上下文对象:<%=pageContext.getAttribute("users")%>
<!-- 访问请求对象 -->
请求对象数据: <%=request1.getAttribute("goods") %>
<!-- 获取会话数据 -->
最爱的宠物: <%=session1.getAttribute("animals")%>
</body>
</html>
二、request的使用
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%request.setAttribute("users","小红");%>
<%=request.getAttribute("users")%>
<!-- 获取编码标准 --> <%=request.getCharacterEncoding()%>
<br/>
<!-- 获取服务器的本地地址 --><%=request.getLocalAddr()%>
<!-- 获取服务器本地端口号 --><%=request.getLocalPort()%>
<!-- 获取请求的方法 --><%=request.getMethod()%>
<!-- 获取服务器协议 --><%=request.getProtocol()%>
<!-- 获取servlet上下文 --><%=request.getServletContext()%>
<%=request.getRequestURL()%>
<!-- 获取跳转页面的请求对象 --><%=request.getRequestDispatcher("index.jsp")%>
</body>
</html>
三、response的使用
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Response服务器对客户端的响应</title>
</head>
<body>
<!-- 设置编码标准 --><%=response.setCharacterEncoding("UTF-8")%>
<!-- 设置重定向,跳转到index.jsp --><%=response.sendRedirect("index.jsp")%>
<!-- 获得打印流 -->
<%
PrintWriter out1 =response.getWriter();
out1.print("Hello World!!");
%>
</body>
</html>
四、session的使用
<%@ page contentType="text/html;charset=UTF-8" language="java" import="com.openlab.*" %>
<html>
<head>
<title>Session</title>
</head>
<body>
<!-- 保存对象 -->
<%
Card card=new Card();
card.setId(1);card.setName("Coco");card.setNum(001);
session.setAttribute("cards",card);
%>
<!-- 访问会话里保存的各个对象数据-->
<%=((Card)session.getAttribute("cards")).getId()%>
<%=((Card)session.getAttribute("cards")).getName()%>
<%=((Card)session.getAttribute("cards")).getNum()%>
<%=session.getId()%>
<%=session.getCreationTime()%>
<%=session.getLastAccessedTime()%>
</body>
</html>
五、out的使用
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Out</title>
</head>
<body>
<%=out.println("我是页面默认的输出对象")%>
</body>
</html>
六、page的使用
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Page</title>
</head>
<body>
<!-- 声明各种变量,方法 -->
<%=page.hashCode()%>
</body>
</html>
七、application的使用
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Application</title>
</head>
<body>
<!-- 被所有的用户所共享,应用于所有页面,都可被访问,session会话,只能在服务器访问到 -->
<%application.setAttribute("man","大帅哥");%>
<%session.setAttribute("woman","大美女");%>
<!-- 获取application的上下文 -->
<%=application.getContextPath()%>
<br/>
<!-- 获取index.jsp真实路径 -->
<%=application.getRealPath("index.jsp")%>
<!-- 获取会话的超时时间 -->
<%=application.getSessionTimeout()%>
<<!-- 获取application的上下文的名字 -->
<%=application.getServletContextName()%>
</body>
</html>
八、config的使用
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Config</title>
</head>
<body>
<!-- 传递两个参数 -->
<%=config.getInitParameter("param1")%>
<%=config.getInitParameter("param2")%>
<%=config.getServletContext().getContextPath()%>
</body>
</html>
九、exception的使用
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
<title>exception</title>
</head>
<body>
<!-- exception用来处理jsp页面的异常信息 -->
</body>
</html>
El表达式常见api
配置EL的方式:
在<%page %>里加上 isELIgnored="false" 。
在web.xml里加上代码块
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>false</el-ignored>
</jsp-property-group>
</jsp-config>
EL表达式
<%pageContext.setAttribute("age","23");%>
<%request.setAttribute("username","张明");%>
<%session.setAttribute("salary","6000");%>
<%application.setAttribute("address","西安");%>
<!-- 获取值的两种方式 -->
${age}
${pageScope.age}
<br/>
常见的api
算数运算符
<h2>算数运算符</h2>
======================================================================================
${2+2}<br/>
${2*2}<br/>
${100/9}<br/>
${100%10}<br>
比较运算符
<h2>比较运算符</h2>
=====================================================================================
${10>100}<br/>
${10<9}<br/>
${20<=20}<br/>
${100>90 && 200<300}<br/>
${100>90 and 200<300}<br/>
${empty requestScope.username}<br/> <!--判断值是否为空 -->
${100>99?1:2} <!-- 三目运算符 -->