Web Applications with Java/JSP
Java Server Pages (JSPs) is a technology for dynamic content generation for things like Web pages. JSPs are analogous to PHP pages, where static text can be mixed with Java code, and the result is sent to the client. Technically speaking, JSPs are translated on the fly by a special servlet (provided by the Servlet Container) into their own servlets and compiled into bytecode, and then run just like “normal” servlets. Listing 4 shows the code for tasks.jsp—the page referenced in GetTasksServlet's doGet() method above.
Listing 4. tasks.jsp
<%@ page
pageEncoding="UTF-8"
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<fmt:setBundle basename="ApplicationResources" />
<html>
<head>
<title><fmt:message key="tasks.title" /></title>
</head>
<body>
<h1><fmt:message key="tasks.title" /></h1>
<table>
<tr>
<th><fmt:message key="tasks.header.date" /></th>
<th><fmt:message key="tasks.header.client" /></th>
<th><fmt:message key="tasks.header.description" /></th>
<th><fmt:message key="tasks.header.duration" /></th>
</tr>
<c:forEach var="item" items="${tasks}">
<tr>
<td>
<fmt:formatDate dateStyle="short" value="${item.date}" />
</td>
<td><c:out value="${clientMap[item.clientId].name}" /></td>
<td><c:out value="${item.description}" /></td>
<td><c:out value="${item.duration}" /></td>
</tr>
</c:forEach>
</table>
<form method="POST" action="<c:url value="/save-task" />">
<fieldset>
<legend>Add New Task</legend>
<div class="form-field">
<label for="clientId">
<fmt:message key="tasks.header.client" />
</label>
<select name="clientId" id="clientId">
<option value="">Please choose…</option>
<c:forEach var="client" items="${clients}">
<option value="${client.id}">
<c:out value="${client.name}" />
</option>
</c:forEach>
</select>
</div>
<div class="form-field">
<label for="description">
<fmt:message key="tasks.header.description" />
</label>
<input type="text" name="description"
id="description" size="50" />
</div>
<div class="form-field">
<label for="duration">
<fmt:message key="tasks.header.duration" />
</label>
<input type="text" name="duration" id="duration" size="4" />
</div>
<div class="buttons">
<input type="submit"
value="<fmt:message key="task.save" />" />
</div>
</fieldset>
</form>
</body>
</html>
The page begins with a page declaration that includes some metadata about the page, including the output character encoding, and then some “taglib” tags that tell the JSP compiler I want to use some “tag libraries”. Tag libraries are helper libraries that allow JSP scripts to wield powerful tools using very simple syntax. After the DOCTYPE, there is a <fmt:setBundle> element, and in the <title> of the page, there is a <fmt:message> element. These two tags, defined by the “fmt” tag library, work together to provide internationalization capabilities to this page. The <fmt:setBundle> tag defines the string resource bundle to be used by the page, and the <fmt:message> tag uses that bundle to pull localized text from the appropriate file to display in the page. The result is, when I visit this page with my Web browser set to the en_US locale, I get text in English, but if I switch the locale to fr_BE and reload the page, the page will switch into French without any further programming.
The standard Java API actually provides all this capability out of the box, but the JSTL (Java Standard Template Library) “fmt” tag library gives us access to Java's internationalization APIs without having to write any Java code. By providing a Java property file (a text file with simple key=value syntax) for each locale I want to support, I get text localization practically for free. Further down in the JSP file, you can see the use of another “fmt” tag, <fmt:formatDate>. This tag formats a date object using the user's locale and a simple name for the format (“simple” in this case). This results in MM/dd/yy in the US and dd/MM/yy in Belgium.
The next JSTL tag is <c:forEach>. This tag actually encloses a body, which is evaluated multiple times: once for each item it finds in the “items” attribute. The value of ${items} means that the value is not just a simple literal value, but an expression that should be evaluated. The object “items” is found in the request object's “attributes”—remember I put it there in the servlet code—and used here as the data for the loop. Within the body of <c:forEach>, the “item” object is defined and can be used by any JSTL tags.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
Web Development News
Developer Poll
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Reply to comment | Linux Journal
39 min 8 sec ago - Reply to comment | Linux Journal
7 hours 33 min ago - Reply to comment | Linux Journal
7 hours 49 min ago - Favorite (and easily brute-forced) pw's
9 hours 40 min ago - Have you tried Boxen? It's a
15 hours 32 min ago - seo services in india
20 hours 4 min ago - For KDE install kio-mtp
20 hours 4 min ago - Evernote is much more...
22 hours 4 min ago - Reply to comment | Linux Journal
1 day 6 hours ago - Dynamic DNS
1 day 7 hours ago








Comments
Helpful article.
Thank you for bringing greater clarity to the Java Web world.
Cool article! Very insghtful.
This is an insightful article that expands horizons for Java users!