<% ... %>
contains Java code
<%= value %>
inserts the value
of a Java expression into the HTML document
<%@ page contentType="text/html; charset=ISO-8859-5" %>
<html>
<head>
<title> Hello </title>
</head>
<body>
<p>
<%
String hello = "Hello world";
%>
This JSP page is saying <br>
<%= hello %>
</p>
</body>
</html>
webapps
jan
jan/jsp
jan/WEB-INF
jan/WEB-INF/classes
jan/WEB-INF/classes
jan/jsp
HelloWWW
is referenced by
http://jan.newmarch.name:8080/jan/servlet/helloWWW
hello.jsp
is referenced by
http://jan.newmarch.name:8080/jan/jsp/hello.jsp
It is customary in a servlet to call the HttpServletRequest
request parameter
request
. In JSP, that is its name:
<%@ page import="java.util.*,java.text.NumberFormat" %>
<%@ page contentType="text/html; charset=ISO-8859-1" %>
<html>
<head>
<title>Localized Numbers</title>
</head>
<body>
<%
Locale preferredLocale = request.getLocale();
NumberFormat nf = NumberFormat.getInstance(preferredLocale);
String numberStr = nf.format(123456.789);
%>
The number 123456.789 in the user's preferred locale is
<%= numberStr %>
</body>
</html>
package jan;
import java.text.NumberFormat;
import java.util.Locale;
import javax.servlet.http.*;
public class MyNumber {
private double value = 123456.789;
private NumberFormat nf;
public MyNumber(HttpServletRequest request) {
Locale preferredLocale = request.getLocale();
nf = NumberFormat.getInstance(preferredLocale);
}
public String toString() {
return(nf.format(value));
}
}
TOMCAT_HOME/webapps/jan/WEB-INF/classes
<%@ page import="jan.MyNumber" %>
<%@ page contentType="text/html; charset=ISO-8859-5" %>
<html>
<head>
<title> Hello </title>
</head>
<body>
<p>
My number in the current locale is <br>
<%=
new MyNumber(request).toString()
%>
</p>
</body>
</html>
RequestDispatcher dispatcher = request.
getRequestDispatcher("/template.jsp");
if (dispatcher != null)
dispatcher.forward(request, response);
<jsp:forward page="/main.jsp" />
<%@ include file="relativeURL" %>
includes a file at that point, which could be another JSP page
<%! ... %>
such as
<%! int i = 0; %>
<%! public String f(int i) {
if (i < 3) return("...");
...
}
%>
<% ... >
<% if (Calendar.getInstance().get(Calendar.AM_PM) == Calendar.AM) { %>
<p> Good Morning </p>
<% } else { %>
<p> Good Afternoon </p>
<% } %>
<%@ taglib prefix="c" url="http://java.sun.com/jsp/jstl/core" %>
defines the Core namespace "c".
Funtional Area | URI | Prefix | Example |
---|---|---|---|
Core | http://java.sun.com/jsp/jstl/core |
c
|
<c:tagname ...> |
XML processing | http://java.sun.com/jsp/jstl/xml |
x
|
<x:tagname ...> |
I18N capable formatting | http://java.sun.com/jsp/jstl/fmt |
fmt
|
<fmt:tagname ...> |
Database access (SQL) | http://java.sun.com/jsp/jstl/sql |
sql
|
<sql:tagname ...> |
Functions | http://java.sun.com/jsp/jstl/functions |
fn
|
fn:functionName(...) |
From The JavaTM Web Services Tutorial - Using JSTL
<%@ page contentType="text/html; charset=ISO-8859-5" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:formatNumber value="123456789" />
<c:if test="${1 == 1}">
1 == 1 is true
</c:if>
<c:if test="${1 == 2}">
1 == 2 is true
</c:if>
<br>
<c:set var="name" value="Fred" />
His name is ${name}
<br>
<c:forEach var="current" items="${headerValues}">
Header key is: ${current.key},
Value is: ${header[current.key]}
<br>
</c:forEach>
${ ... }
The EL (expression language) has a number of inbuilt variables including
pageContext
pageContext.request
param
paramValues
header
headerValues
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
fmt:setLocale
fmt:formatNumber
fmt:formatDate
fmt:timeZone
fmt:setTimeZone
fmt:parseDate
fmt:parseNumber
fmt:requestEncoding
fmt:bundle
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html; charset=ISO-8859-1" %>
<c:set var="locale" value="${pageContext.request.locale}" />
<fmt:setLocale value="${locale}" />
12356.678 in the ${locale} locale is
<fmt:formatNumber value="12345.678" />
package jan;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
public class LocaleServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
Locale[] availableLocales = new Locale[] {
Locale.CANADA_FRENCH,
Locale.FRANCE,
Locale.SIMPLIFIED_CHINESE};
session.setAttribute("availableLocales", availableLocales);
RequestDispatcher dispatcher = request.
getRequestDispatcher("/jsp/availableLocales.jsp");
if (dispatcher != null)
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.Locale" %>
<%@ page contentType="text/html; charset=ISO-8859-5" %>
Locales are ${availableLocales}
Looping:
<c:forEach var="index" items="${availableLocales}">
Locale is ${availableLocales[index]}
<br>
</c:forEach>
Java Server Pages act as the presentation end of a backend web application. They allow a mix of HTML and Java in various ways