--Passing parameters between jsp ⇔ Servlet --Cookie / session management
--EL type --JSTL tag
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".
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>
.
JSTL is a free open source library provided by Apache.
WEB-INF / lib
of the project folder.mypage.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
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
--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.
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
.
--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