Detailed explanation of JSP page foundation of Java

Posted by tbobker on Mon, 10 Jan 2022 11:13:18 +0100

JSP details

brief introduction

JSP (full name Java Server Pages) is a technical standard that enables software developers to dynamically generate Web pages with HTML, XML or other format documents in response to client requests. Is the language running on the server.

JSP file suffix is jsp .

The WEB application developed by JSP can be used across platforms, both on Linux and Windows.

JSP output Hello World

<html>
    <head>
           <title>first JSP program</title>
    </head>
    <body>
           <%
                  out.println("Hello World!");
           %>
    </body>
</html>

JSP syntax

Script program

A script program can contain any number of Java statements, variables, methods, or expressions as long as they are valid in the scripting language.

Mode 1

<% code snippet %>

Mode II

<jsp:scriptlet>
   code snippet
</jsp:scriptlet>

statement

A declaration statement can declare one or more variables and methods for later Java code

Mode 1

<%! int i = 0; %> 
<%! int a, b, c; %> 
<%! Circle a = new Circle(2.0); %> 

Mode II

<jsp:declaration>
   code snippet
</jsp:declaration>

expression

Mode 1

<%= expression %>
<%= (new java.util.Date()).toLocaleString()%>

Mode II

<jsp:expression>
   expression
</jsp:expression>

notes

Syntax format of JSP comments

<%-- This part of the comment will not be displayed in the web page--%> jsp
<!-- notes --> Httml

instructions

JSP instructions are used to set attributes related to the entire JSP page

Define page

<%@ page errorPage="/error/500.jsp" %>		//Point to the customized error reporting page
<%@ page isErrorPage="true" %>						//Is it an error page

Import file

<%@ include file="common/header.jsp"%>		//Include jsp files in this location

Import definition of label Library

<%@ taglib prefix="c" uri="http://java. sun. COM / JSP / JSTL / core "% > / / import JSTL core tag library

behavior

Behavior tags are basically pre-defined functions. The following table lists some available JSP behavior tags

grammardescribe
jsp:includeUse to include static or dynamic resources in the current page
jsp:useBeanFind and initialize a JavaBean component
jsp:setPropertySet the value of the JavaBean component
jsp:getPropertyInsert the value of the JavaBean component into the output
jsp:forwardPass a request object containing user requests from one JSP file to another
jsp:pluginUsed to include Applet and JavaBean objects in the generated HTML page
jsp:elementDynamically create an XML element
jsp:attributeDefines the attributes of dynamically created XML elements
jsp:bodyDefines the body of dynamically created XML elements
jsp:textUsed to encapsulate template data
<jsp:include page="common/footer.jsp"></jsp:include>

Difference between directive and label reference

Instruction marks are statically included. The files contained are statically inserted without translation. When the JSP page is compiled to generate java code, it becomes a method call. This method will be executed once every time the JSP page is run.

The action tag is added to the included file at run time. When compiling the JSP page to generate java code, the content in the JSP is directly compiled as its own part

Implicit Object

JSP supports nine automatically defined variables, also known as built-in variables

objectdescribe
requestAn instance of the HttpServletRequest class
responseAn instance of the HttpServletResponse class
outAn instance of the PrintWriter class used to output results to a web page
sessionAn instance of the HttpSession class
applicationThe instance of ServletContext class, which is related to the application context
configAn instance of the ServletConfig class
pageContextThe instance of PageContext class provides access to all objects and namespaces of JSP pages
pageSimilar to the this keyword in a Java class
exceptionThe object of exception class represents the corresponding exception object in the JSP page where the error occurred

Storage properties

<%--Built in object--%>
<%--    4 An object for storing things--%>
<%
	//The saved data is valid in one page
    pageContext.setAttribute("name1", "value");     
    //The saved data is valid in one request, and the request forwarding will carry this data
    request.setAttribute("name2", "value");  
    //The saved data is valid in one session, from opening the browser to closing
    session.setAttribute("name3", "value");   
    //The saved data is valid in the server. The data is lost only when the server crashes
    application.setAttribute("name4", "value");     
%>

<%
    //Find Attribute through find
    String name1 = (String) pageContext.findAttribute("name1");
    String name2 = (String) pageContext.findAttribute("name2");
    String name3 = (String) pageContext.findAttribute("name3");
    String name4 = (String) pageContext.findAttribute("name4");
    String name5 = (String) pageContext.findAttribute("name5");     //non-existent
%>

<%--EL Expression output ${}}--%>
<h1>The values taken are:</h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<h3>${name5}</h3>

The scope of pagecontext is in the current page context, the scope of request is in a request, and the subsequent session and application are the largest

Scope from low to high: pagecontext = = > request = = > session = = > Application

EL

The EL (express language) expression will automatically search the four scopes and obtain data. It can reduce the writing of tedious JSP small script fragments.

Header information needs to be added

<%@ page isELIgnored="false" %>

Use format:

${key};

JSTL

JSTL (Java Server Pages Standard Tag Library), which encapsulates the general core functions of JSP applications.

Common core tags

Enter label

//Equivalent to <% =% > when value is empty, the default value is output; escapeXml is & lt; to determine whether an escape character is recognized
<c:out value="${null}"  default="000"  escapeXml="true" ></c:out>  

Set / remove labels

//The default domain is page
<c:set var="salary" scope="session" value="${2000*2}"/>
//You can also execute scope
<c:remove var="salary"  />

Judgment label

<c:if test="${salary>300}">test is true</c:if>

Select label

<c:choose>  
	<c:when test="${salary==100}">Pay is equal to 100</c:when>  
	<c:when test="${salary>100}">Pay is more than 100</c:when>  
	<c:otherwise>this is the third option</c:otherwise>  
</c:choose>  

Circular label

// The step step size of the items loop body specifically indicates that status also has current, and the current value is equivalent to num;index iterates from 0; count iterates from 1 and other loop body optional parameters
<c:forEach  items="${numbers}" var="num" step="2" varStatus="status"  >  
	<c:out value="${num}"></c:out>  
</c:forEach> 

Non array definition loop

//items is the content, delims is the delimiter, and name is the temporary variable
<c:forTokens items="google,runoob,taobao" delims="," var="name">  
   			<c:out value="${name}"/><p>  
</c:forTokens> 

Jump

//Will jump directly to the specified web page
<c:redirect url="http://www.runoob.com"/>

Format label

Processing numbers

//Specific use https://www.runoob.com/jsp/jstl-format-formatnumber-tag.html
<fmt:formatNumber value="${salary}" type="currency"></fmt:formatNumber>

Processing date

Label | rookie tutorial (runoob.com)

//2019-12-28 , 13:50:30 , type="time, date, both" displays time, date and both respectively
<c:set var="date" value="<%=new java.util.Date()%>"></c:set>

//June 26, 2016 , 11:19:43 am , E shows week
<fmt:formatDate value="${date}" type="both" pattern="yyyy-MM-dd HH:mm:ss E"/>

Topics: Java Back-end