JavaWeb Study Notes 5: JSP & JSTL

1-JSP Introduction

JSP - Java Server Page
 from the user's point of view, is an ordinary page
 from programmers point of view, is a Java class, he inherited a Servlet, it can be said that a JSP Servelt

Why should JSP?

Under most circumstances html used to display static content, immutable. But sometimes we need to show some dynamic data on a Web page
, such as: query all student information, according to a student's name to a specific query. These actions need to query the database and then displayed on the page. html is not supported write java code, but you can write jsp inside.

jsp three instructions:
1、page 2、include 3、taglib
jsp nine built-in objects:
1、request 2、session 3、response
4、out 5、aplication 6、config
7、jspcontext 8、exception 9、page

2-JSP Page directive

<% @ Command name%> which may provide some of the property of the jsp page

  • language
    表明jsp页面中可以写java代码
  • contentType
    表明这个文件是什么类型,告诉浏览器我是什么内容类型,以及使用什么编码
    contentType="text/html; charset=UTF-8" text/html MIMEType 这是一个文本,html网页
  • pageEncoding
    jsp内容所使用的编码
  • extends
    用于指定jsp翻译成java文件后,继承的父类是谁,一般不用改
  • import
    导包使用的,一般不用手写
  • session
    值可选的有true or false
    用于控制在这个jsp页面里面,是否能够直接使用session对象
    如果该值是true,那么在代码里面会有getSession()的调用
    如果该值是false,那么就不会有该方法调用,也就是没有session对象
  • errorPage
    指的是错误的页面,值需要给错误的页面路径
  • isErrorPage
    上面的errorPage 用于指定错误的时候跑到哪一个页面去,那么这个isErroPage就是声明某一个页面到底是不是错误的页面
3-JSP tag common instruction operation
  • taglib
    <%@ taglib prefix="" uri=""%>
    uri: tag library path, prefix: alias tag library
  • include
    <%@ include file="other02.jsp"%>
    details behind: in fact, all the contents of another page of output to take over together all the elements of the label are included in, so a little bloated
  • jsp: include
    <jsp:include page="other02.jsp"></jsp:include>
    contains the specified page, here is the dynamically included. That is not the page tab contains all the elements to take over the entire output, but to take over its operating results
  • jsp: forward
    <jsp:forward page=""></jsp:forward>
    to the designated page, the internal implementation of the code is still done by the Servlet:
    <% 
    	//请求转发
    	request.getRequestDispatcher("other02.jsp").forward(request, response);
    %>
    
  • jsp: param
    means: When a page containing or join this parameter when jumping to a page
    <jsp:forward page="other02.jsp">
    	<jsp:param value="beijing" name="address"/>
    </jsp:forward>
    
    在other02.jsp中获取参数:
    <br>收到的参数是:<br>	<%= request.getParameter("address")%>
    
4-JSP built-in objects (four scopes)

The so-called built-in objects that we can use directly in the jsp page, these objects do not create, specifically under four:
pageContext, Request, the session, the Application

  • Scope
    表示这些对象可以存值,他们的取值范围有限定
    //分别存储不同作用域数据
    <%
    	pageContext.setAttribute("name", "page");
    	request.setAttribute("name", "request");
    	session.setAttribute("name", "session");
    	application.setAttribute("name", "application");
    %>
    //取出四个作用域中的值
    <%=pageContext.getAttribute("name")%>
    <%=request.getAttribute("name")%>
    <%=session.getAttribute("name")%>
    <%=application.getAttribute("name")%>
    
    Scope Size range:
     pageContext - current page display
     request - forward the request to display
     session - Redirection display
     application - before the server shuts down

Four scopes difference:

  1. pageContext PageContext []
    > scope is limited to the current page
    > You can also get to eight other built-in objects
  2. HttpServletRequest request []
    > scope is limited to the first request, as long as the server responded to the request, the value stored in the domain, there is no
  3. HttpSession session] [
    > scoped to a session (multiple request and response) which
  4. ServletContext [application]
    > The whole project can access the server can not be shut down after a visit

The remaining built-in objects:

  • out 【JspWriter】
  • response 【HttpServletResponse】
  • exception 【Throwable】
  • page [Object] - this is an instance of an object jsp translated into java class
  • config 【ServletConfig】
5-EL expression (a value of usage)

1, takes a value stored in the scope 4

<%
	pageContext.setAttribute("name", "page");
	request.setAttribute("name", "request");
	session.setAttribute("name", "session");
	application.setAttribute("name", "application");
%>
<br>按普通手段取值<br>
<%= pageContext.getAttribute("name")%>
<%= request.getAttribute("name")%>
<%= session.getAttribute("name")%>
<%= application.getAttribute("name")%>

<br>使用EL表达式取出作用域中的值<br>
${ pageScope.name }
${ requestScope.name }
${ sessionScope.name }
${ applicationScope.name }

2, if the field is stored in an array

<%
	String [] a = {"aa","bb","cc","dd"};
	pageContext.setAttribute("array", a);
%>
<br>使用EL表达式取出作用域中数组的值<br>
${array[0]},${array[1]},${array[2]},${array[3]}

3, if the latch is set domain

<%
	List li = new ArrayList();
	li.add("nihao");
	li.add("i,m");
	li.add("List");
%>
<br>使用EL表达式取出作用域中集合的值<br>
${li[0]},${li[1]},${li[2]}

4, takes a value set Map

<%
	Map map = new HashMap();
	map.put("name", "zhangsna");
	map.put("age",18);
	map.put("address","北京..");
	map.put("address.aa","深圳..");
	pageContext.setAttribute("map", map);
%>
<br>使用EL表达式取出作用域中Map的值<br>
${map.name},${map.age},${map.address},${map["address.aa"]}
6-EL expression (use of two values)

Details Value:
1, the value from the domain, you must first stored value

<%
//pageContext.setAttribute("name", "zhangsan");
session.setAttribute("name", "lisi...");
%>
<br>直接指定说了,到这个作用域里面去找这个name<br>
${ pageScope.name } 
<br>//先从page里面找,没有再去request、session、application找 <br>
${ name }
<br>指定从session中取值<br>
${ sessionScope.name }  

2, the value of the way
if this value is under the target, then the direct use []

<%
	String[] array = {"aa","bb","cc"}
	session.setAttribute("array",array);
%>
${ array[1] } --> 这里array说的是attribute的name

If there is no index, used directly. The way to get

<%
	User user = new User("zhangsan",18);
	session.setAttribute("u", user);
%>
${ u.name }  , ${ u.age } 

注意:一般使用EL表达式,用的比较多的,都是从一个对象中取出它的属性值,比如取出某一个学生的姓名

EL expressions built-in objects:

$ {Object name. Members}
pageContext, pageScope, requestScope
sessionScope, applicationScope, header
headerValues, param, paramValues, the cookie, initParam

7-JTSL basic usage

EL do not traverse the work, it may only be capable JSTL value such work
JSTL: JSP Standard Tag Library - JSP Standard Tag Library
simplifies the coding jsp Replace <%%> writing, generally used with an EL expression use
JSTL introduced methods:
1, import support JSTL jar files jstl.jar and standard.jar
2, used on page taglib tag library introduced version 1.0 does not support EL expressions, the introduction of version 1.1
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

JSTL常用方法:
1、<c:set></c:set>
2、<c:if test=""></c:if>
3、<c:forEach></c:forEach>

  • c:set

    <!-- 声明一个对象name,对象的值zhangsan,存储到了page(默认),指定是session -->
    <c:set var="name" value="zhangsan" scope="session"></c:set>
    ${sessionScope.name}
    
  • c:if

    //判断test里面的表达式是否满足,如果满足,就执行c:if标签中的输出,c:if 是没有else的
    <c:set var="age" value="18" ></c:set>
    <c:if test="${ age > 26 }">
    	年龄大于了26...
    </c:if>
    <c:if test="${ age <= 26 }">
    	年龄小于了26...
    </c:if>
    ------------------------------
    //定义一个变量名 flag  去接收前面表达式的值,然后存在session域中
    <c:if test="${ age > 26 }" var="flag" scope="session">
    	年龄大于了26...
    </c:if>
    
  • c:forEach

//从1开始遍历到10,得到的结果,赋值给i,并且会存储到page域中,step增幅为2
step增幅为2的意思是:1 3 5 7 9,即输出间隔为2
<c:forEach begin="1" end="10" var="i" step="2">
	${i }
</c:forEach>
-----------------------------------------------
<!-- items : 表示遍历哪一个对象,注意,这里必须写EL表达式
	var: 遍历出来的每一个元素用user 去接收 -->
<c:forEach var="user" items="${list }">
	${user.name } ----${user.age }
</c:forEach>
8- integrated case: student management system (*)

Because a lot of content, you can look at a blog written before: Web Project - Student Management System

HPF- self-summary

  Built-in objects You know, EL expressions to be used, JSTL to master.
  Errors and improve student management system has lasted a long time, so it's vaguely remember that idea.
  --- short step, a thousand miles; not small streams into a mighty torrent.

Published 15 original articles · won praise 18 · views 4574

Guess you like

Origin blog.csdn.net/oZuoShen123/article/details/105150610