JSP案例

  • JSP三种声明:

第一种形式 java代码片断 <% %>

第二种形式 jsp表达式 <%= %>

第三种形式 jsp声明 <%! %>

 

  • page指令的session属性:

session属性的取值为true(缺省)/false。

如果值为false,则对应的servlet代码 当中不会生成声明和创建session的代码。

也就是说,不能够使用session隐含对象了。

 

  • page指令的isErrorPage属性和errorPage属性

jsp03.jsp:

<%@ page errorPage="errorHandler.jsp"  contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
    <title>Insert title here</title>
</head>
<body style="font-size:30px;">
    <%
      String num=request.getParameter("num");
      out.println(Integer.parseInt(num)+100);
     %>
</body>
</html>

errorHandler.jsp:

<%@  page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isErrorPage="true" %> 
 <!-- exception隐含对象 -->
<html>
<head>
    <title>Insert title here</title>
</head>
<body style="font-size:30px;">
 
      出现异常   
        <font> <%=exception.getMessage()%> </font>  
</body>
</html>

访问:http://localhost:8080/xxx/jsp03.jsp?num=abc

  • 对JSP配置参数:

jsp:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
    <title>Insert title here</title>
</head>
<body style="font-size:30px;">
    address:
    <%=config.getInitParameter("address") %>
</body>
</html>

 

web.xml部分:

 

<servlet>
   <servlet-name>jsp04</servlet-name>
   <jsp-file>/jsp04.jsp</jsp-file>
   <init-param>
    <param-name>address</param-name>
    <param-value>shanghai</param-value>
   </init-param>
  </servlet>
  <servlet-mapping>
   <servlet-name>jsp04</servlet-name>
   <url-pattern>/abc.html</url-pattern>
  </servlet-mapping>

访问http://localhost:8080/web09/abc.html

  • 隐藏对象pageContext

jsp05:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
    <title>Insert title here</title>
</head>
<body style="font-size:30px;">
    <%
     pageContext.setAttribute("username","cj");
     request.setAttribute("pwd","123");
     %>
     username:
     <%=pageContext.getAttribute("username")%>
     pwd:
     <%=request.getAttribute("pwd")%>
     <a href="jsp06.jsp">链接</a>
</body>
</html>

 

jsp06:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
    <title>Insert title here</title>
</head>
<body style="font-size:30px;">
    username:
    <%=pageContext.getAttribute("username")%>
     pwd:
     <%=request.getAttribute("pwd")%>
</body>
</html>

测试

一个JSP对应一个pageContext对象,所以在jsp06.jsp中访问不到jsp05.jsp中pageContext 对象中的数据username;

同时,因为点击“链接”是一次新请求,所以jsp06.jsp中也访问不到jsp05.jsp中request对象 中的数据username;

放入session中是可以的。

如下jsp隐藏对象访问范围从小到大

pageContext 只有对应的JSP实例自己可以访问,生命周期从对应的JSP对象创建到JSP对象消亡。

request 一次请求能访问,生命周期在一起请求和响应期间。

session 一次会话期间能访问,多次请求和响应期间都存在。

ServletContext 整个应用内部所有组件都能访问,除非服务器关闭,否则一直存在。

 

  • 活动元素

<jsp:forward page=""/> 转发,page属性指定转发的地址

jsp07:

 

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
   <html>
   <head>
       <title>Insert title here</title>
   </head>
   <body style="font-size:30px;">
        <%
        request.setAttribute("username","cj");
        %>
      <jsp:forward page="jsp08.jsp"></jsp:forward>
   </body>
   </html>

jsp08:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
  <html>
  <head>
    <title>Insert title here</title>
  </head>
  <body style="font-size:30px;">
   username:
       <%=request.getAttribute("username")%>
  </body>
  </html>


 

  • <jsp:include page=""/> 一个jsp在运行过程当中,调用另外一个jsp

jsp09:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
 <html>
 <head>
     <title>Insert title here</title>
 </head>
 <body style="font-size:30px;">
  jsp09..<br/>
  <jsp:include page="jsp10.jsp">
   <jsp:param value="123" name="pwd"/>
  </jsp:include>
      <br/>
      jsp09 other..
 </body>
 </html>

 

jsp10:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
 <html>
 <head>
     <title>Insert title here</title>
 </head>
 <body style="font-size:30px;"> 
      pwd:
      <%=
      request.getParameter("pwd")
      %>
 </body>
 </html>

 

  • <jsp:useBean id="" scope="" class=""/> 在指定的范围绑订一个对象。

 

范围指的是四个对象pageContext,request,session,servletContext。

也就是说scope的值可以是"page","request","session","application"。

 

两段代码是等价的:

 

  • <jsp:getProperty/>

<jsp:setProperty name="" property="" value=""/>

<jsp:setProperty name="" property="" param=""/> 依据请求参数给属性赋值。

<jsp:setProperty name="" property="*"/> 使用"自省机制"给属性赋值。

34.新建bean.User.java

35.新建jsp11.jsp

 

测试:

 

  • Jsp中的注释

<!-- <%=new Date()%> --> 注释中的代码会执行,但不会在页面上输出。

<%--xxxx--%> 注释中的代码不会执行,也不会在页面上输出。

 

源代码:https://files.cnblogs.com/files/C3054/web05.rar

原文地址:https://www.cnblogs.com/C3054/p/4277447.html