[Java Servlet] The road of Senri is also the fifth step from the first step

Progress

--Passing parameters between jsp ⇔ Servlet --Cookie / session management

What to do this time

--EL type --JSTL tag

Let's use EL type

EL expression (Expression Language) is a method that can briefly describe the passing of request parameters on the jsp side. Just write $ {parameter name}. (In the case of an object, $ {parameter name.property name})

When receiving and displaying the parameter named name on the jsp side,

mypage.jsp(until now)


<%
  String username=request.getAttribute("name").toString();
%>
<%=username%>Welcome

I was going through a two-step process, but when I used the EL formula

mypage.jsp


${name}Welcome

Just write. The amount of description is reduced and it is easier to see, but the biggest merit is that you do not need to write java logic. It is also possible to write a simple expression such as $ {age> 20}, as it is labeled with EL "expression".

Let's use JSTL tag

In the previous section, EL expressions can be used, but processing such as loops and conditional branching must embed java code. That's where the tag library called JSTL comes in. A tag library is a group of files that can express java logic using tags like HTML, such as <forEach>.

Introduction method

JSTL is a free open source library provided by Apache.

-Apache Taglibs Downloads

    1. Open the above link and download ʻimpl, spec, jstl` from the jar files at the bottom of the page.
  1. Store the above three jar files in WEB-INF / lib of the project folder.
    1. Describe the following in the jsp file.

mypage.jsp


<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  1. If the app version of web.xml is 3.0 or higher, rewrite it to 2.4. (I made it in 3.1 in this corner, but it seems that EL formula cannot be used in JSTL after 2.4. Inconvenient!)

web.xml


<!-- 3.1 is commented out-->
<!--
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1">
-->
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

By the way, the description method of web-app tag for each app version is described in detail on the following site. KATSUMI KOKUZAWA'S BLOG

How to use

--if statement

mypage.jsp


  <c:choose>
    <c:when test="${name == 'hato'}">Mr. Administrator, Hello</c:when>
    <c:when test="${name == ''}">Anonymous, Hello</c:when>
    <c:otherwise>General user's, Hello</c:when>
  </c:choose>
tag element Contents
choose - The content enclosed in this tag is a series of if statement blocks.
when - Equivalent to if else in an if statement.
when item If this item is true, the part enclosed in the tag is executed.
otherwise - Equivalent to else of if statement. Executed when there is no hit anywhere in when.

--for statement

LoginServlet.java


List<String> list=Arrays.asList("one","two","three","four");
req.setAttribute("list",list);

mypage.jsp



<c:forEach var="v" items="${list}" varStatus="list">
	${v} ${list.count}
	<!--
	one 1
	two 2
	three 3
	four 4
Outputs
	-->
</c:forEach>

<c:forEach begin="1" end="7" step="2" varStatus="c">
	hello ${c.count}
	<!--
	hello 1
	hello 2
	hello 3
Outputs
	-->
</c:forEach>
tag element Contents
forEach var Variables that access the elements retrieved from items
items Specify a collection (usually write an EL expression)
varStatus Variables that access the repeating state (such as the number of loops)
begin Loop start position
end Loop end position
step Increase amount (basically 1)

The above for Each is an extended for statement

exFor.java


for(String item:list){
  System.out.println(item);
}

The for Each below is an ordinary for statement

For.java


for(int i=1;i<=7;i=i+2){
  System.out.println("hello"+i);
}

It is easy to remember because the behavior is similar to.

About other tags

There are other core libraries such as <c: param> and <c: set>, but they are too long to explain, so I will omit them. In addition, there are abundant libraries such as format and xml.

Summary

--Graduation from <% request.getAttribute ("hogehoge ");%> with EL expression --Graduation from <% if (~)%>, <% for (~)%> in JSTL If you want to make it practical, you will definitely need javascript, but by making full use of EL expressions and JSTL, you do not need to write java logic, and you can simplify the jsp file.

Recommended Posts

[Java Servlet] The road of Senri is also the fifth step from the first step
[Java Servlet] The road of Senri is also the third step from the first step
[Java Servlet] The road of Senri is also the fourth step from the first step
[Java Servlet] The road of Senri is also one step to the first
[Java Servlet] Senri no Michi is one step to the second
The road from JavaScript to Java
From Java9, the constructor of the class corresponding to primitive types is deprecated.
The order of Java method modifiers is fixed
The story of learning Java in the first programming
[Java] Calculate the day of the week from the date (Calendar class is not used)
First touch of the Files class (or Java 8)
Java language from the perspective of Kotlin and C #
The story that .java is also built in Unity 2018
Get to the abbreviations from 5 examples of iterating Java lists
Java: The problem of which is faster, stream or loop
Road to Java Engineer Part2 What kind of language is Java?
[Java] Delete the specified number of characters from the end of StringBuilder
[Java] I felt as a java beginner. "Why is the first letter of the String type capitalized?" I faced the String type.
[Java beginner] Conversion from character string to numerical value-What is the parseInt method of the Integer class? ~
[Java] Beginner's understanding of Servlet-①
This is the first post.
Java is the 5th day
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
Read the first 4 bytes of the Java class file and output CAFEBABE
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
[Swift] The color of the Navigation Bar is different (lighter) from the specified color.
Is the version of Elasticsearch you are using compatible with Java 11?
Investigation method when the CPU of the server running java is heavy
Let's summarize the Java 8 grammar from the perspective of an iOS engineer