Download presentation
Presentation is loading. Please wait.
Published byKatelynn Summers Modified over 9 years ago
1
JavaServerPages Some examples
2
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).
3
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.
4
Tomcat jsp examples
5
EL: the expression language
6
A little of the EL arithmetic example: note use of backslash Expression Result \${1} ${1} \${1 + 2} ${1 + 2} \${1.2 + 2.3} ${1.2 + 2.3} \${1.2E4 + 1.4} ${1.2E4 + 1.4}
7
There are some cute and engaging examples, all with source code
8
panels
9
Hello World? It doesn’t look like much but…
10
The jsp <%@ taglib prefix="mytag" uri="/WEB-INF/jsp2/jsp2-example-taglib.tld" %> JSP 2.0 Examples - Hello World SimpleTag Handler JSP 2.0 Examples - Hello World SimpleTag Handler This tag handler simply echos "Hello, World!" It's an example of a very basic SimpleTag handler with no body. Result:
11
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!" ); } }
12
Java and jsp It is quite common to combine java classes (especially beans) with jsp. Beans typically handle the logic and jsp handles presentation.
13
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 --> Get a Celsius temperature Celsius temperature:
14
tempconvert0.jsp <!-- tempconvert0.jsp A document that converts a Celsius temperature received from tempconvert0.html to Fahrenheit --> Temperature converter <% // 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:
15
Running the example
16
scriptlets You can start and end java control structures between jsp tags, as in ….. This is a handy way to embed run-time structure into html
17
Using scriptlets to do temp conversion
18
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 --> Temperature converter <% // 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; %>
19
tempconvert1.jsp part 2 <!-- Use an expression to display the value of the Fahrenheit temperature --> Fahrenheit temperature: <!-- 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 --> Celsius temperature: <!-- Code for the end of the else clause compound statement --> <% } //** end of else clause %>
20
The standard tag library… same thing again <!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 --> Get a Celsius temperature Celsius temperature:
21
tempconvert2.jsp… note tag lib prefix “c” <!-- tempconvert2.jsp Convert a given temperature in Celsius to Fahrenheit. Called by tempconvert2.html --> <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> Temperature converter Given temperature in Celsius: Temperature in Fahrenheit:
22
Running tempconvert2
23
Page can post to itself, e.g., using jstl “if” Check if this is post or get:.
24
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 --> <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> Temperature converter Celsius temperature: <input type = "submit" value = "Convert to Fahrenheit" /> Given temperature in Celsius: Temperature in Fahrenheit:
25
Same, but using radio buttons <!-- tempconvert4.jsp Convert a given temperature in Celsius to Fahrenheit This is both the request and the response document --> <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> Temperature converter to Fahrenheit to Celsius temperature to convert: <input type = "submit" value = "Convert" />
26
tempconvert4.jsp continued Temperature in Fahrenheit: Temperature in Celsius:
27
run
28
More radio buttons <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> Payment type <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
29
continued mc discover visa check
30
Post merely outputs the payment (rb) selected
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.