jsp 三大指令和动作标签

jsp三大指令
一个jsp页面中可以有0-N个指令

1.page--->最复杂:<%@page language="" ...%>
*pageEncoding和contentType
pageEncoding:指定当前jsp页面的编码,只要不说谎,就不会有乱码!在服务器要把jsp编译成.java时需要使用pageEncoding
contentType:它表示添加一个响应头:Content-Type ,相当于response.setContentType()

*import:导包,可以出现多次
*errorPage和isErrorPage
errorPage:当前页面如果抛出异常,那么要转发到哪一个页面,由errorPage决定,状态码为200
isErrorPage:指定页面为处理错误页面!当该属性为true时,这个页面会设置状态码为500,而且这个页面可以使用9大内置对象中的exception

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" errorPage="error/error.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'a.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <%
        int a=13/0;
     %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isErrorPage="true"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'error.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    哈哈,出错了!<br/>
     
     <%
         //<%=exception.getMessage()% > <br/>
         //<%exception.printStackTrace(response.getWriter()); % >
         //当然,一般不会直接将错误信息显示给用户,会变得人性化 
      %>
      
        
        
      
    
     
    
   
  </body>
</html>
View Code

<error-page>
<error-code>404</error-code>
<location>/error/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.RuntimeException</exception-type>
<location>/index.jsp</location>
</error-page>
*autoFlush和buffer
autoFlush:指定jsp的输出流满时,是否自动刷新,默认为true,如果为false,那么在缓冲区满时会抛出异常
buffer:指定缓冲区大小,默认为8kb,不需要修改
*isELIgnored:是否忽略el表达式,默认为false,不忽略,即支持!
*基本没用
language:指定当前jsp编译后的语言类型,默认为Java
info:信息
isThreadSafe:当前的jsp是否支持并发访问!
session:当前页面是否支持session,如果为false,那么当前页面就没有session这个内置对象
extends:让jsp生成的servlet去继承该属性指定的类

2.include-->静态包含
*与RequestDispatcher的include()方法的功能相似
*<%@include%>是在jsp编译成Java文件时完成的,它们共同生成一个Java文件,然后再生成一个class!
*RequestDispatcher的include()是一个方法,包含和被包含是两个Servlet,即两个class,它们只是把响应的内容在运行时合并了
*作用:将页面分解了,使用包含的方式组合在一起,这样一个页面中不变的部分就是一个独立的jsp,而我们只需要处理变化的页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'hel.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  <!-- 
      这里注意file不能使用变量,因为变量是在Java代码中的,Java代码的执行是在程序运行之后财货执行的
   -->
  <body>
    <%
        String name="guodaxia";
     %>
     <%@include file="lj.jsp" %>
  </body>
</html>
<%=name %>
<!-- 这里需要将其他删掉,因为它是将两个jsp拼接成为一个jsp生成class文件 -->
View Code


3.taglib-->导入标签库
两个属性
>prefix:指定标签库在本页面中的前缀!由我们自己来起名称!
>uri:指定标签库的位置
><%@taglib prefix="pre" uri="/struts-tags"%> 前缀的用法<s:text>

jsp动作标签

JSP动作标签
这些jsp的动作标签,与html的标签有本质的区别。
*动作标签是由tomcat(服务器)来解释执行!它和Java代码一样,都是由服务端来执行的。
*html由浏览器来执行!
*<jsp:forward>: 转发!它与RequestDispatcher的forwaard方法是一样的,一个是在Servlet中使用,一个是在jsp中使用!

*<jsp:include>: 动态包含:它与RequestDispatcher的include方法是一样的,一个是在Servlet中使用,一个是在jsp中使用!
><%@include%>和<jsp:include>有什么不同?

*<jsp:param>: 它用来作为forward和include的子标签,用来传递参数使用

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'a.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <h3>a.jsp</h3>
    <!-- <jsp:include page="b.jsp"/> -->
    <jsp:forward page="b.jsp">
        <jsp:param value="guodaxia" name="name"/>
        <jsp:param value="1234" name="password"/>
    </jsp:forward>
  </body>
</html>
    <h3>b.jsp</h3>
    <%
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        out.write(name+","+password);
    %>
View Code


javaBean规范:

javaBean的规范:
1、必须要有一个模认构造器
2、提供get/set方法,如果只有get方法,那么这个属性就是只读属性!
3、属性:有get/set方法的成员,还可以没有成员,只有get/set方法。属性的名称就是get/set方法将前面的get/set去除之后首字母小写得到
4、方法名称满足一定的规范,那么它就是属性!boolean类型的属性的读方可以是is开头也可以是get开头

内省:
内省类-->Bean信息-->属性描述符-->属性的get/set对应的method-->可以反射了
=====
commons-beanutils,它是依赖内省完成
*导包
>commons-beanutils.jar
>commins-loggin.jar
BeanUtils.setProperty(stu, "username", "guodaxia");
int age=Integer.valueOf(BeanUtils.getProperty(stu, "age"));
BeanUtils.populate(stu, map);

jsp中与javaBean相关的标签
*<jsp:useBean>--->创建或者查询bean
*<jsp:useBean id="user1" class="cn.itcast.domain.User" scope="session"/>在session域中查找名为user1的useBean不存在则创建
*<jsp:useBean id="user1" class="cn.itcast.domain.User" scope="session"/>
*<jsp:setProperty>
*<jsp:setProperty property="username" name="user1" value="admin"/>
*<jsp:getProperty>
*<jsp:getProperty property="username" name="user1"/>

原文地址:https://www.cnblogs.com/aigeileshei/p/5697193.html