[Java] [Java Servlet] One step from Senri Road to the fifth step

3 minute read


  • Passing parameters between jsp and servlet
  • Cookie/session management

What to do this time

  • EL expression
  • JSTL tag

EL expression Let’s use

EL expression (Expression Language) is a method that roughly describes the passing of request parameters on the jsp side. Just write ${parameter name}. (${parameter name.property name} for an object)

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


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

I was going through a two-step process, but when I use EL expressions


Welcome, ${name}

Just write The amount of description is reduced and it is easier to see, but the biggest advantage is that no need to write java logic. It is also possible to write a simple expression such as ${age> 20} as it is called EL “expression”.

JSTL Tag Let’s use

You can use EL expressions in the previous section, but you have to embed java code for processing such as looping and conditional branching. Then there is a tag library called JSTL. The tag library is a group of files that collect the functions that can express java logic by using tags such as HTML like <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.
  2. Store the above 3 jar files in WEB-INF/lib of the project folder.
  3. Describe the following in the jsp file.


<%@ 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 as 2.4. (I made it in 3.1 in this corner, but it seems that EL expressions cannot be used in JSTL after 2.4. Inconvenient!)


<!-- 3.1 commented out -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- ->
<web-app version="2.4"

By the way, how to write web-app tag for each app version is described in detail on the following sites. KATSUMI KOKUZAWA’S BLOG

How to use

  • if statement


    <C: when test = "$ {name == 'hato'}"> administrator's, Hello </ c: when>
    <C: when test = "$ {name == ''}"> Anonymous, Hello </ c: when>
    <c:otherwise>Hi, general user</c:when>
Tags Elements Contents  
choose - A series of if statement blocks enclosed in this tag.  
Equivalent to if else of when - if statement.
when item When this item is true, the part enclosed by the tags is executed.  
Equivalent to else in otherwise - if statement. Execute when there is no hit in when.
  • for statement


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


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

<c:forEach begin="1" end="7" step="2" varStatus="c">
hello ${c.count}
hello 1
hello 2
hello 3
- ->
Tags Elements Contents  
Variables for accessing elements retrieved from forEach var items
  items Specify collection (generally write EL expression)  
  varStatus Variable to access the iteration status (loop count etc.)  
  begin Loop start position  
  end Loop end position  
  step Increment (basic is 1)  

The above forEach is an extended for statement


for(String item:list){

The below forEach is an ordinary for statement


for(int i=1;i<=7;i=i+2){

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

About other tags

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


  • EL graduation from <% request.getAttribute("hogehoge"); %>
  • Graduated from <% if(~) %> and <% for(~) %> with JSTL When it comes to practical use, javascript is absolutely necessary, but by making full use of EL expressions and JSTL, it is not necessary to write java logic and the jsp file can be simplified.