Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaServerPages Some examples. About JSP Java server pages are a combination of html (or xml), java code appearing within tagged regions, and structures.

Similar presentations


Presentation on theme: "JavaServerPages Some examples. About JSP Java server pages are a combination of html (or xml), java code appearing within tagged regions, and structures."— Presentation transcript:

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 Get a Celsius temperature Celsius temperature:

14 tempconvert0.jsp 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 %> 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 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 Fahrenheit temperature: <% } //** end of if (strCtemp !=... else { %> Celsius temperature: <% } //** end of else clause %>

20 The standard tag library… same thing again Get a Celsius temperature Celsius temperature:

21 tempconvert2.jsp… note tag lib prefix “c” <%@ 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 <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> Temperature converter Celsius temperature: Given temperature in Celsius: Temperature in Fahrenheit:

25 Same, but using radio buttons <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> Temperature converter to Fahrenheit to Celsius temperature to 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 Visa Master Charge Discover Check

29 continued mc discover visa check

30 Post merely outputs the payment (rb) selected


Download ppt "JavaServerPages Some examples. About JSP Java server pages are a combination of html (or xml), java code appearing within tagged regions, and structures."

Similar presentations


Ads by Google