【知了堂学习笔记】_Java中EL和JSTL的学习

5 篇文章 0 订阅
3 篇文章 0 订阅

请关注“知了堂学习社区”,地址:http://www.zhiliaotang.com/portal.php 

JSP内置对象的概念

1.      JSP本质是一个Servlet。

2.      JSP被访问时:

(1)    web4用起会把请求交给JSP引擎处理。

(2)    JSP引擎会将JSP翻译成一个jspServlet(本质上是一个servlet)。

(3)    安装servlet的调用方式进行调用。

3.      JSP变成jspServlet的时候:

(1)    创建和传递9个web开发相关的对象供jspServlet使用。

(2)    在JSP页面中就有这9个对象的变量可以直接使用。

(3)    这就是JSP内置对象。

4.      JSP本身是一个Servlet,Servlet中本身有web开发需要的对象JSP,JSP引擎在将其翻译为servlet的时候,new。我们将web开发需要的对象提前定义好了,JSP中直接使用这些对象。

5.      NewFile.jsp的代码:

body主体代码:

<body>

        this is jsp;

        <!--

           JSP本身是一个Servlet

           Servlet中本身有web开发需要的对象

           JSPJSP引擎在将其翻译为servlet的时候,new

           我们将web开发需要的对象提前定义好了

           JSP中直接使用这些对象

        -->

        <% %>

</body>

JSP内置对象使用1

1.      JSP内置对象有:

(1)    request对象

(2)    session对象

(3)    application对象

(4)    response对象

(5)    out对象

(6)    config对象

(7)    exception对象

(8)    page对象

(9)    pageContext对象

2.      request对象:封装了用户的依次请求,并且所有的请求参数都在其中。

3.      out对象:代表更简单的响应,页面输出流。

4.      config对象:代表当前JSP配置信息(ServletConfig)。

5.      exception对象:JSP脚本中产生的错误和异常,JSP页面处理机制的一部分。

6.      NewFile.jsp的代码:(注意:errorPage="error.jsp"

<%@ page language="java"contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"errorPage="error.jsp"%>

<!DOCTYPEhtml PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

    this is jsp;

    <!--

        JSP本身是一个Servlet

        Servlet中本身有web开发需要的对象

        JSPJSP引擎在将其翻译为servlet的时候,new

        我们将web开发需要的对象提前定义好了

        JSP中直接使用这些对象

    -->

    <%

        out.print("this is jsp");

    %>

    <br>

    <!-- config对象:对应ServletConfig的一个实例 -->

    <%=config.getInitParameter("")%>

    <!-- 所有的JSP而言,相同的名字,jsp -->

    <%=config.getServletName()%>

 

    <%

        int a = 5/0;

    %>

</body>

</html>

7.      OutTestServlet的代码:

doGet方法的代码:

protected voiddoGet(HttpServletRequest request, HttpServletResponseresponse) throwsServletException, IOException {

       // TODO Auto-generatedmethod stub

       PrintWriter out = response.getWriter();

       out.print("this is servlet");

}

8.      error.jsp的代码:(注意:isErrorPage="true"

<%@ page language="java"contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"isErrorPage="true"%>

<!DOCTYPEhtml PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

    sorry

    <%=exception.getClass() %>

    <%=exception.getMessage() %>

</body>

</html>

JSP内置对象使用2

1.      pageContextTest.jsp的代码:

body主体代码:

<body>

        <!-- pageContext封装了8大内置对象 -->

        <%=pageContext.getRequest()%>

        <hr>

        <%

           request.setAttribute("request","hello request");

          

String requestParam = (String)pageContext.getAttribute("request",pageContext.REQUEST_SCOPE);

          

           out.print(requestParam);

           //pageContext中保存了数据session = hello session

           pageContext.setAttribute("session","hello session");

           //session作用域中放入session = hello session

pageContext.setAttribute("session","hello session", pageContext.SESSION_SCOPE);

      

           out.print("<hr>");

           out.print(session.getAttribute("session"));

        %>

</body>

2.      NewFile.jsp的代码:

body主体代码:

<body>

        this is jsp;

        <!--

           JSP本身是一个Servlet

           Servlet中本身有web开发需要的对象

           JSPJSP引擎在将其翻译为servlet的时候,new

           我们将web开发需要的对象提前定义好了

           JSP中直接使用这些对象

        -->

        <%

            out.print("this is jsp");

        %>

        <br>

        <!-- config对象:对应ServletConfig的一个实例 -->

        <%=config.getInitParameter("")%>

        <!-- 所有的JSP而言,相同的名字,jsp -->

        <%=config.getServletName()%>

       

        <%

            pageContext.forward("pageContextTest.jsp");

    %>

</body>

EL入门:运算符、对象和数组

1.      EL表达式:Expression Language表达式语言。

(1)    要简化jsp中Java代码开发。

(2)    它不是一种开发语言,是jsp中获取数据的一种规范。

2.      EL能够从作用域中获取数据。

3.      pageContextTest的代码:

body主体代码:

<body>

        this is jsp;

        <!--

           JSP本身是一个Servlet

           Servlet中本身有web开发需要的对象

           JSPJSP引擎在将其翻译为servlet的时候,new

           我们将web开发需要的对象提前定义好了

           JSP中直接使用这些对象

        -->

        <%

            out.print("this is jsp");

        %>

        <br>

        <!-- config对象:对应ServletConfig的一个实例 -->

        <%=config.getInitParameter("")%>

        <!-- 所有的JSP而言,相同的名字,jsp -->

        <%=config.getServletName()%>

       

        <%

            pageContext.forward("pageContextTest.jsp");

    %>

   

       <hr>

        ${session }

    ${request }

</body>

4.      ELCal.jsp的代码:

body主体代码:

<body>

        <tableborder="1">

           <tr>

               <td>\${1+1 }</td>

               <td>${1+1 }</td>

           </tr>

           <tr>

               <td>\${10mod4 }</td>

               <td>${10mod 4 }</td>

           </tr>

           <tr>

               <td>\${10%4 }</td>

               <td>${10%4 }</td>

           </tr>

           <tr>

               <td>\${(1==2)?true:false}</td>

               <td>${(1==2)?true:false}</td>

           </tr>

           <tr>

               <td>\${1>2 }</td>

               <td>${1>2 }</td>

           </tr>

           <tr>

               <td>\${'a'>'b' }</td>

               <td>${'a'>'b' }</td>

           </tr>

        </table>

</body>

5.      Person.java的代码:

public class Person {

    private int id;

    private Stringname;

    private int age;

    private Dogdog;

    public Dog getDog() {

        returndog;

    }

    public void setDog(Dog dog) {

        this.dog =dog;

    }

    public int getId() {

        returnid;

    }

    public void setId(intid) {

        this.id =id;

    }

    public String getName() {

        returnname;

    }

    public void setName(String name) {

        this.name =name;

    }

    public int getAge() {

        returnage;

    }

    public void setAge(intage) {

        this.age =age;

    }

   

}

6.      Dog.java的代码:

public class Dog {

    private Stringname;

    private Toy[]toys;

 

    public String getName() {

        returnname;

    }

 

    public void setName(String name) {

        this.name =name;

    }

 

    public Toy[] getToys() {

       returntoys;

    }

 

    public void setToys(Toy[] toys) {

        this.toys =toys;

    }

}

7.      Toy.java的代码:

public class Toy {

    private Stringname;

 

    public String getName() {

        returnname;

    }

 

    public void setName(String name) {

        this.name =name;

    }

 

}

8.      ELObject.jsp的代码:

body主体代码:

<body>

        <%

           Person p = new Person();

           p.setId(1);

           p.setName("shunyu");

           p.setAge(21);

      

           Dog dog = new Dog();

           dog.setName("旺财");

      

           p.setDog(dog);

      

           Toy toy1 = new Toy();

           toy1.setName("骨头");

           Toy toy2 = new Toy();

           toy2.setName("");

           Toy[] toys = new Toy[2];

           toys[0] = toy1;

           toys[1] = toy2;

      

           dog.setToys(toys);

      

           request.setAttribute("person", p);

        %>

        ${person }

        ${person.name }<!-- 对应的是setter/getter方法 -->

        ${person.dog.name }

        ${person.dog.toys[0].name}

</body>

EL入门:集合

1.      ELObject.jsp的代码:

body主体代码:

<body>

        <%

           Person p = new Person();

           p.setId(1);

           p.setName("shunyu");

           p.setAge(21);

      

           Dog dog = new Dog();

           dog.setName("旺财");

      

           p.setDog(dog);

      

           Toy toy1 = new Toy();

           toy1.setName("骨头");

           Toy toy2 = new Toy();

           toy2.setName("");

           Toy[] toys = new Toy[2];

           toys[0] = toy1;

           toys[1] = toy2;

      

           dog.setToys(toys);

      

           request.setAttribute("person", p);

        %>

        ${person }

        ${person.name }<!-- 对应的是setter/getter方法 -->

        ${person.dog.name }

        ${person.dog.toys[0].name}

        <hr>

        ${person }

        ${person["name"]}

        ${person["dog"]["name"]}

        ${person["dog"]["toys"][0]["name"]}

</body>

2.      ELCollection.jsp的代码:

body主体代码:

<body>

        <%

           List<Person>list = new ArrayList<Person>();

           Person p1 = new Person();

           p1.setName("张三");

           Person p2 = new Person();

           p2.setName("李四");

      

           list.add(p1);

           list.add(p2);

      

           Map<String,Person>map = new HashMap<String,Person>();

           map.put("1",p1);

           map.put("2",p2);

      

           request.setAttribute("list", list);

           request.setAttribute("map", map);

        %>

        ${list }

        ${list[1] }

        ${list[1].name }

        <hr>

        ${map }

        ${map["1"] }

        ${map["1"].name}

</body>

EL操作内置对象

1. EL.jsp的代码:

body主体代码:

<body>

    <%

        request.setAttribute("request","this is request");

        session.setAttribute("session","this is session");

application.setAttribute("application","this is application");

        Cookie cookie = new Cookie("name","admin");

        cookie.setMaxAge(30*60);

        response.addCookie(cookie);

    %>

    ${requestScope.request }<br>

    ${sessionScope.session }<br>

    ${applicationScope.application }<br>

    ${cookie.name.value }

    <formaction="EL.jsp"method="post">

        <inputtype="text"name="username"/>

        <inputtype="text"name="pwd"/>

        <inputtype="submit"/>

    </form>

    ${param.username }<br>

    ${param.pwd }<br>

    ${paramValues.username[0] }

</body>

2. EL.jsp的代码:

body主体代码:

<body>

    <%

           request.setAttribute("info","this is request");

           session.setAttribute("info","this is session");

        application.setAttribute("info","this is application");              Cookie cookie =new Cookie("name","admin");

        cookie.setMaxAge(30*60);

        response.addCookie(cookie);

    %>

        <!-- 会依次从request,session,application中去进行寻找 -->

        ${info }

      <hr>

    ${requestScope.info }<br>

    ${sessionScope.info }<br>

    ${applicationScope.info }<br>

    ${cookie.name.value }

    <formaction="EL.jsp"method="post">

        <inputtype="text"name="username"/>

        <inputtype="text"name="pwd"/>

        <inputtype="submit"/>

    </form>

    ${param.username }<br>

    ${param.pwd }<br>

    ${paramValues.username[0] }

</body>

 

JSTL入门

1. JSTL(JavaServerPages Standard TagLibrary)JSP标准标签库,使用JSTL实现JSP页面中逻辑处理。如判断、循环等。

2. Core标签库:

(1)通用标签

(2)控

(3)URL相关

taglibTest.jsp的代码:(注意:<%@ tagliburi=http://java.sun.com/jsp/jstl/coreprefix="c"%>

 

<%@pageimport="java.util.ArrayList"%>

<%@pageimport="java.util.List"%>

<%@ page language="java"contentType="text/html; charset=UTF-8"

   pageEncoding="UTF-8"%>

<%@ taglib uri=http://java.sun.com/jsp/jstl/coreprefix="c"%>

<!DOCTYPEhtml PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<!-- JSTLEL表达式 -->

<c:iftest="${5>3 }">

    满足条件

</c:if>

<!-- num = 8 -->

<c:setvar="num"value="${8 }"></c:set>

<c:choose>

    <c:whentest="${num==1 }">num is 1</c:when>

    <c:whentest="${num==5 }">num is 5</c:when>

    <c:whentest="${num==8 }">num is 8</c:when>

    <c:otherwise>

        num

    </c:otherwise>

</c:choose>

<!-- for(i=0;i>10;i++) -->

<c:forEachvar="i"begin="0"end="10"step="1">

    ${i}<br>

</c:forEach>

<%

    List<String>list = new ArrayList<String>();

    list.add("qwe");

    list.add("asd");

    list.add("zxc");

    list.add("qaz");

    list.add("wsx");

    list.add("edc");

   

    request.setAttribute("listString", list);

%>

<!-- for(String str:list) -->

${listString}

<hr>

<c:forEachitems="${listString }"var="str">

    ${str}<br>

</c:forEach>

<c:forEachitems="${listString }"var="str"varStatus="status">

    ${str}:${status.index }/${status.count }/${status.first }/${status.last }<br>

</c:forEach>

<c:forEachitems="${listString }"var="str"varStatus="status">

    <p ${status.count%2==0?"style='background-color:red'":"style='background-color:orange'"}>${str}:${status.index }/${status.count }</p>

</c:forEach>

</body>

</ html >

JSTL小结

1.  formatJSTLTest的代码:

(注意:<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"prefix="fmt"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core"prefix="c"%>

 

<%@pageimport="java.util.Date"%>

<%@ page language="java"contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"prefix="fmt"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core"prefix="c"%>

<!DOCTYPEhtml PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<fmt:formatNumbervalue="46.38976"pattern=".000"></fmt:formatNumber>

<!-- Date d = new Date() -->

<c:setvar="d"value="<%=new Date()%>"></c:set>

${d }

<hr>

<fmt:formatDatevalue="${d }"type="both"/>

</body>

</html>

 

还有一点需要注意的是<fmt:formatDate value="${d}" type="both"/>可以将”both”改为”Date”,”both”的结果日期和时间都有,而”Date”则只出现日期。

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值