300 likes | 377 Views
JavaServerPages. Some examples. About JSP. Java server pages are a combination of html (or xml), java code appearing within tagged regions, and structures and functions called via other pages or tag libraries.
E N D
JavaServerPages Some examples
About JSP • Java server pages are a combination of html (or xml), java code appearing within tagged regions, and structures and functions called via other pages or tag libraries. • A JSP is compiled when a request is made for the page. You’ll notice that the first request takes longer than subsequent requests. • Otherwise, a JSP is “human-readable” and is placed somewhere in your webapp, in the root or some subdirectory. • Reference to JSP does not need to be made in web.xml… The page is found via the URL, compiled and delivered (or error messages appear).
About JSP • JSP are compiled and executed as servlets. • You can’t implement JSP technology without a servlet container. • Tomcat comes with many sample JSP and you can run them or view their code.
A little of the EL arithmetic example: note use of backslash Expression</b></td> <td><b>Result</b></td> </thead> <tr> <td>\${1}</td> <td>${1}</td> </tr> <tr> <td>\${1 + 2}</td> <td>${1 + 2}</td> </tr> <tr> <td>\${1.2 + 2.3}</td> <td>${1.2 + 2.3}</td> </tr> <tr> <td>\${1.2E4 + 1.4}</td> <td>${1.2E4 + 1.4}</td> </tr>
There are some cute and engaging examples, all with source code
The jsp <%@ taglib prefix="mytag" uri="/WEB-INF/jsp2/jsp2-example-taglib.tld" %> <html> <head> <title>JSP 2.0 Examples - Hello World SimpleTag Handler</title> </head> <body> <h1>JSP 2.0 Examples - Hello World SimpleTag Handler</h1> <hr> <p>This tag handler simply echos "Hello, World!" It's an example of a very basic SimpleTag handler with no body.</p> <br> <b><u>Result:</u></b> <mytag:helloWorld/> </body> </html>
The tag handler java class package jsp2.examples.simpletag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; import java.io.IOException; /** * SimpleTag handler that prints "Hello, world!" */ public class HelloWorldSimpleTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { getJspContext().getOut().write( "Hello, world!" ); } }
Java and jsp • It is quite common to combine java classes (especially beans) with jsp. • Beans typically handle the logic and jsp handles presentation.
Temp converter v0: Html points to a jsp <!-- tempconvert0.html A document that displays a form that collects a Celsius temperature from a client and calls a scriptlet to convert it to Fahrenheit --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Get a Celsius temperature </title> </head> <body> <p> <!-- Display a form to collect a Celsius temperature --> <form action = "tempconvert0.jsp" method = "get" > Celsius temperature: <input type = "text" name = "ctemp" /> <input type = "submit" /> </form> </p> </body> </html>
tempconvert0.jsp <!-- tempconvert0.jsp A document that converts a Celsius temperature received from tempconvert0.html to Fahrenheit --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Temperature converter </title> </head> <body> <p> <% // embedded java code…Get the Celsius temperature from the form String strCtemp = request.getParameter("ctemp"); float ftemp; // convert the value to Fahrenheit ftemp = 1.8f * Integer.parseInt(strCtemp) + 32.0f;// I changed this a little %> <!-- Use an expression to display the value of the Fahrenheit temperature --> Fahrenheit temperature: <%= ftemp %> </body> </html>
scriptlets • You can start and end java control structures between <%...%> jsp tags, as in <%if(…)%> ….. <%else %> • This is a handy way to embed run-time structure into html
tempconvert1.jsp part one <!-- tempconvert1.jsp A document that collects a Celsius temperature from a client and uses a scriptlet to convert it to Fahrenheit --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Temperature converter </title> </head> <body> <p> <% // Get the Celsius temperature from the form String strCtemp = request.getParameter("ctemp"); float ftemp; // If this is not the first request (there was a form value), // convert the value to Fahrenheit if (strCtemp != null) { ftemp = 1.8f * Integer.parseInt(strCtemp) + 32.0f; %>
tempconvert1.jsp part 2 <!-- Use an expression to display the value of the Fahrenheit temperature --> Fahrenheit temperature: <%= ftemp %> <!-- Code for the end of the then clause compound statement --> <% } //** end of if (strCtemp != ... else { %> <!-- This is the first request, so display the form to collect the Celsius temperature --> <form action = "tempconvert1.jsp" method = "get" > Celsius temperature: <input type = "text" name = "ctemp" /> <input type = "submit" /> </form> </p> <!-- Code for the end of the else clause compound statement --> <% } //** end of else clause %> </body> </html>
The standard tag library… same thing again <!xml version = "1.0" encoding = "utf-8"?> <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <!-- tempconvert2.html Get a temperature in Celsius and call a JSTL JSP document (tempconvert2.jsp) to convert it to Fahrenheit --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Get a Celsius temperature </title> </head> <body> <form action = "tempconvert2.jsp" method = "post" > <p> Celsius temperature: <input type = "text" name = "ctemp" /> <br /> <input type = "submit" value = "Convert to Fahrenheit" /> </p> </form> </body> </html>
tempconvert2.jsp… note tag lib prefix “c” <!-- tempconvert2.jsp Convert a given temperature in Celsius to Fahrenheit. Called by tempconvert2.html --> <%@ page contentType = "text/html" %> <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Temperature converter </title> </head> <body> <p> Given temperature in Celsius: <c:out value = "${param.ctemp}" /> <br /> <br /> Temperature in Fahrenheit: <c:out value = "${(1.8 * param.ctemp) + 32}" /> </p> </body> </html>
Page can post to itself, e.g., using jstl “if” Check if this is post or get: <c:if test = "${pageContext.request.method != 'POST'}"> <!--build form if not post -->. <form action = "tempconvert3.jsp" method = "post" > <!--….form goes here --> </form> </c:if> <!--….don’t forget close form and if structure --> <c:if test = "${pageContext.request.method == 'POST'}"> <!--….handle post --> </c:if>
Same again, but a jsp page has itself as action target…looks same a previous <!-- tempconvert3.jsp Convert a given temperature in Celsius to Fahrenheit This is both the request and the response document --> <%@ page contentType = "text/html" %> <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Temperature converter </title> </head> <body> <c:if test = "${pageContext.request.method != 'POST'}"> <form action = "tempconvert3.jsp" method = "post" > Celsius temperature: <input type = "text" name = "ctemp" /> <br /> <input type = "submit" value = "Convert to Fahrenheit" /> </form> </c:if> <c:if test = "${pageContext.request.method == 'POST'}"> Given temperature in Celsius: <c:out value = "${param.ctemp}" /> <br /> <br /> Temperature in Fahrenheit: <c:out value = "${(1.8 * param.ctemp) + 32}" /> </c:if> </body> </html>
Same, but using radio buttons <!-- tempconvert4.jsp Convert a given temperature in Celsius to Fahrenheit This is both the request and the response document --> <%@ page contentType = "text/html" %> <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Temperature converter </title> </head> <body> <c:if test = "${pageContext.request.method != 'POST'}"> <form action = "tempconvert4.jsp" method = "post" > <input type = "radio" name = "convert" value = "fahrenheit" checked = "checked" /> to Fahrenheit <input type = "radio" name = "convert" value = "celsius" /> to Celsius temperature to convert: <input type = "text" name = "temp" /> <br /> <input type = "submit" value = "Convert" /> </form> </c:if>
tempconvert4.jsp continued <c:if test = "${pageContext.request.method == 'POST'}"> <c:choose> <c:when test = "${param.convert == 'fahrenheit'}"> Temperature in Fahrenheit: <c:out value = "${(1.8 * param.temp) + 32}" /> </c:when> <c:otherwise> Temperature in Celsius: <c:out value = "${(5 * (param.temp-32)/9) }" /> </c:otherwise> </c:choose> </c:if> </body> </html>
More radio buttons <!-- testradio4.jsp test radio buttons... post self --> <%@ page contentType = "text/html" %> <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Payment type </title> </head> <body> <c:if test = "${pageContext.request.method != 'POST'}"> <form action = "testradio4.jsp" method = "post" > <input type = "radio" name = "payment" value = "visa" checked = "checked" /> Visa <input type = "radio" name = "payment" value = "mc" /> Master Charge <input type = "radio" name = "payment" value = "discover" /> Discover <input type = "radio" name = "payment" value = "check" /> Check <br/> <input type = "submit" value = "Submit" /> </form> </c:if>
continued <c:if test = "${pageContext.request.method == 'POST'}"> <c:choose> <c:when test = "${param.payment == 'mc'}"> mc </c:when> <c:when test = "${param.payment == 'discover'}"> discover </c:when> <c:when test = "${param.payment == 'visa'}"> visa </c:when> <c:otherwise> check </c:otherwise> </c:choose> </c:if> </body> </html>