day10-jsp&ELの代码学习(jsp)

来源:互联网 发布:魔方淘宝网 编辑:程序博客网 时间:2024/06/08 15:35

day10-jsp&ELの代码学习

jsp的页面学习

01.jsp(out隐式对象和内嵌java代码)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title></title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">          </head>  <body>    <!-- html注释   1.下面的写法是一个表达式,专门用于输出  不要在表达式中加;  -->    <%=1+2 %>   <!--  out.print(1+2 ); -->    <h2>out.write(2)输出结果为: </h2>    <%        out.write(2);    %>    <h2>out.write(2+95)输出结果为: </h2>    <%        out.write(2+95);    %>    <h2>out.write(2+"")输出结果为:</h2>    <%        out.write(2+"");    %>    <h2>它用于实现加入java代码  就用小脚本</h2>    <%  int a=1; %>    <!--它用于实现加入java代码  2.  <%a++ ; %> 这里虽然被注释了,但是依然可以执行-->    <%        out.write(a+"<br>");//注意此时的数字代表的是一个编码,而这个数字的编码最终结果是        out.write(97);//97对应的ascii码就是字母a,所以输出a        out.write("<br>"+97);//输出 97     %>     <!-- 3.注释问题   html注释-->                <%-- JSP注释  它不参与翻译过程,所以在翻译后的源码没有它,自然也就不会发送给客户端--%>                <%-- <%a++ ; %> --%>                <%out.write(""+a); %>                <%                    //1.建立连接                    //2.开火                    //3.煮饭                    /*                      java中的多行注释                    *///                  private   int  c=2; //不能写,因为它是放在service()内部                    final int  c=10;                    final int m=15;                 %>        <h2>用于实现方法声明 变量声明   静态代码块声明 ,放在Service ()方法外部</h2>        <%!            private int m=20;            private int a=5;            public void mm(){                    int a=8;            }            static{                int a=10;            }         %>         <%            //这里没有访问静态变量,还是访问的是service方法中的局部变量            out.write(m+"");          %>  </body></html>

02taglib.jsp(jstl核心标签库的简单应用)

<%@ page language="java" import="java.util.*" isELIgnored="false" pageEncoding="UTF-8"%><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title></title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">          </head>  <body>    <c:if test="${1+2>=3 }">算对了</c:if>     <c:if test="${1+2<3 }">算错了</c:if>     <br>     <%        if(1+2>=3){            out.write("算对了");        }else{            out.write("算错了");        }      %>  </body></html>

03include(静态包含与动态包含)

<%@ page language="java" import="java.util.*" isELIgnored="false" pageEncoding="UTF-8"%><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title></title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">          </head>  <body>    <c:if test="${1+2>=3 }">算对了</c:if>     <c:if test="${1+2<3 }">算错了</c:if>     <br>     <%        if(1+2>=3){            out.write("算对了");        }else{            out.write("算错了");        }      %>  </body></html>

04page.jsp(配置error.jsp页面,当页面发生错误时,跳到error.jsp页面)

<%@page import="java.text.SimpleDateFormat"%><%@ page language="java" import="java.util.*" isErrorPage="true" errorPage="/error.jsp" pageEncoding="UTF-8"%><!-- errorPage="/error.jsp"  它是转发过去的 --><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title></title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">          </head>  <body>    <br>    <%        SimpleDateFormat sdf= new SimpleDateFormat();        int i=1/0;     %>  </body></html>

error.jsp(错误处理页面)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title></title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">          </head>  <body>    <%        for(int i=1;i<20;i++){     %>            <h1>服务器正忙,请稍候</h1>     <%} %>  </body></html>

error404.jsp(404错误)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title></title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">          </head>  <body>     <%        for(int i=1;i<20;i++){     %>            <h1>该功能正在开发中,请等待</h1>     <%} %>  </body></html>

web.xml配置

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"     xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <display-name></display-name>   <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <error-page>        <error-code>404</error-code>        <location>/error404.jsp</location>  </error-page></web-app>
0 0
原创粉丝点击