COSC 2956 Internet Tools Java Server Pages BGA
COSC 2956 Internet Tools Java Server Pages BGA
What is a Java Server Page n n n A JSP combines Java code and template HTML in a single file. This is similar to the way PHP works. Scripting elements are used to provide dynamic pages 10/25/2021 BGA 2
Connection with servlets n n n Each Java server page is compiled into a servlet before it can be used This is normally done when the first request is made so there could be a short wait. However, JSP's can be precompiled so there is no wait. Java Server Page translation Java servlet source code page compiler Java Servlet class file (show an example) 10/25/2021 BGA 3
Translated servlets n n n You can example the source code produced by the JSP translation process. There is a directory called work in the main tomcat directory where you can find the source code. Note that the _jsp. Service method corresponds to the servlet service method (which is called by do. Get or do. Post) 10/25/2021 BGA 4
JSP elements (overview) n n Directives of the form <%@. . . %> Scripting elements n n n Standard actions n n Expressions of the form <%= expr %> Scriptlets of the form <% code %> Declarations of the form <%! code %> JSP Comments <%--. . . --%> Example: <jsp: use. Bean>. . . </jsp: use. Bean> Implicit variables like request, response, out 10/25/2021 BGA 5
Directives n They have the form <%@ name attribute 1=". . . ", attribute 2=". . . %> Specify page properties page include Include a file at translation time taglib 10/25/2021 Specify custom tags BGA 6
Directives: examples n Import java packages n n Multiple import statements n n <%@ page import="java. util. *" %> <%@ page import="java. sql. *" %> including file at translation time n n <%@ page import="java. util. *, java. sql. *" %> <%@ include file="header. html" %> For include the path is relative to the jsp 10/25/2021 BGA 7
Scripting elements: expression n For an expression scripting element like <%= expr %>, expr is evaluated and the result is converted to a string and placed into the JSP's servlet output stream. In a Java servlet this would be equivalent to Print. Writer out = response. get. Writer(); . . . out. print(expr); 10/25/2021 BGA 8
Expression examples n Displaying request parameters (request is an implicit object available in a JSP) Your name is <%= request. get. Parameter("name") %> and your age is <%= request. get. Parameter("age") %> n Doing calculations The value of pi is <%= Math. PI %> and the square root of two is <%= Math. sqrt(2. 0) %> and today's date is <%= new java. util. Date() %>. 10/25/2021 BGA 9
Scripting elements: scriptlet n For a scriplet <% statements %> the Java statements are placed in the translated servlet's _jsp. Service method body (it's like the servlet service method which calls either do. Get or do. Post) public void _jsp. Service(Http. Servlet. Request request, Http. Servlet. Response response) throws java. io. IOException, Servlet. Exception {. . . statements. . . } 10/25/2021 BGA 10
Scriplet examples n Check a request parameter <% String name = request. get. Parameter("name"); if (name == null) { %> <h 3>Please supply a name</h 3> <% } else { %> <h 3>Hello <%= name %></h 3> <% } %> There are 3 scriptlets here and an expression element 10/25/2021 BGA 11
Scripting elements: declaration n For a declaration <%! declarations %> the Java statements are placed in the class outside the _jsp. Service method. Typical declarations can be Java instance variable declarations or Java methods // declarations would go here public void _jsp. Service(. . . ) {. . . } 10/25/2021 BGA 12
Declaration examples n Declaring instance variables <%! private int count = 0; %>. . . The count is <%= count++ %>. n Declaring methods <%! private int to. Int(String s) { return Integer. parse. Int(s); } %> 10/25/2021 BGA 13
Including files n Including files at translation time (when JSP is translated to a servlet) <%@ include file="filename" %> n Including files at request time <jsp: include page="filename" flush = "true" /> 10/25/2021 BGA 14
Where do you put JSP's ? n n n If you have a web application called c 2206 then create a directory called jsp below it and put JSP's there: For example a JSP called test. jsp would have the path c 2206/jsp/test. jsp To run this JSP use the URL n http: //localhost: 8080/c 2206/jsp/test. jsp 10/25/2021 BGA 15
A simple JSP <html> <head><title>JSP Test</title></head> <body> <h 1>JSP Test</h 1> Time: <%= new java. util. Date() %> </body> </html> The expression scripting element <%=. . . %> is equivalent to the scriptlet <% out. print(. . . ); %> 10/25/2021 BGA 16
The implicit out object n n In a scriptlet <%. . . %> you can use the out object to write to the output stream: Example: <% out. print("The sum is "); out. print("1 + 2 = " + (1+2)); %> 10/25/2021 BGA 17
The implicit request object n Example <html> <head><title>. . . </title></head> <body> <h 1>. . . </h 1> <p><%= request. get. Parameter("greeting") %></p> </body></html> Try this using http: //localhost: 8080/c 2206/jsp/simple/ greeting 0. jsp? greeting=Hello Frank 10/25/2021 BGA 18
Processing form using GET <html> <head><title>JSP Processing. . . </title></head> <body> <h 1>JSP Processing form with GET</h 1> <form action="do. Form 1. jsp" method="GET"> First name: <input type="text" name="first. Name"> Last name: <input type="text" name="last. Name"> <p><input type="submit" name="button" value="Submit. Name"></p> </form> </body> </html> c 2206/jsp/forms/form 1_get. html 10/25/2021 BGA 19
Processing form using POST <html> <head><title>JSP Processing. . . </title></head> <body> <h 1>JSP Processing form with POST</h 1> <form action="do. Form 1. jsp" method="POST"> First name: <input type="text" name="first. Name"> Last name: <input type="text" name="last. Name"> <p><input type="submit" name="button" value="Submit. Name"></p> </form> </body> </html> c 2026/jsp/forms/form 1_post. html 10/25/2021 BGA 20
do. Form 1. jsp <%@ include file=". . /doctype. html" %> <head> <title>JSP Form Results</title> </head> <body> <h 1>JSP Form Results</h 1> Hello <%= request. get. Parameter("first. Name") %> <%= request. get. Parameter("last. Name") %> </body> </html> c 2206/jsp/forms/do. Form 1. jsp Try this using http: //localhost: 8080/c 2206/jsp/forms/form 1_get. html 10/25/2021 BGA 21
Temperature conversion input output 10/25/2021 BGA 22
temperature. jsp (1) <%@ page import="java. text. Decimal. Format" %> <html> <head><title>Fahrenheit. . . Conversion</title></head> <body> <h 1>Fahrenheit to Celsius Conversion</h 1> <% String self = request. get. Request. URI(); if (request. get. Parameter("convert") == null) { %> <form action="<%= self %>" method="POST"> Fahrenheit temperature: <input type="text" name="fahrenheit" /> <p><input type="submit" name="convert" value="Convert to Celsius" /></p> </form> 10/25/2021 BGA 23
temperature. jsp (2) <% } else { double fahr = 0. 0; try { fahr = Double. parse. Double( request. get. Parameter("fahrenheit")); } catch (Number. Format. Exception e) { // do nothing, accept default value } 10/25/2021 BGA 24
temperature. jsp (3) double celsius = (fahr - 32. 0) * (5. 0/9. 0); Decimal. Format f 2 = new Decimal. Format("0. 00"); %> <%= f 2. format(fahr) %>F is <%= f 2. format(celsius) %>C <p><a href="<%= self %>">Another conversion</a> </p> <% } %> </body> </html> 10/25/2021 BGA 25
Java Beans n n Special classes that encapsulate some data They have a default constructor get and set methods for data fields (properties) A bean can be constructed in JSP using n n <jsp: use. Bean id = ". . . " class = ". . . " /> If the bean already exists this statement does nothing 10/25/2021 BGA 26
setting properties n To set a property of a bean use <jsp: set. Property name=". . . " property=". . . " value=". . . " /> n To set a property using the value of a request parameter use <jsp: set. Property name=". . . " property=". . . " param=". . . " /> 10/25/2021 BGA 27
getting properties n To get a property of a bean use <jsp: get. Property name=". . . " property=". . . " /> 10/25/2021 BGA 28
A Greeting bean package beans; public class Greeting { private String greeting; // the property public Greeting() { greeting = "Hello World"; } public String get. Greeting() { return greeting; } } public void set. Greeting(String g) { greeting = (g == null) ? "Hello World" : g; } beans/Greeting. java 10/25/2021 BGA 29
Naming convention n If the property name is greeting the get method must have the name get. Greeting the set method must have the name set. Greeting 10/25/2021 BGA 30
Creating a Greeting bean (1) n Create a bean and use default property <jsp: use. Bean id="hello" class="beans. Greeting" /> n Create a bean and set its property when it is constructed <jsp: use. Bean id="hello" class="beans. Greeting" > <jsp: set. Property name="hello" property="greeting" value="Hello JSP World" /> </jsp: use. Bean> n Here <jsp: set. Property> is in the body of the <jsp: use. Bean> element. 10/25/2021 BGA 31
Creating a Greeting bean (2) n Create a bean and set its property after it has been constructed <jsp: use. Bean id="hello" class="beans. Greeting" /> <jsp: set. Property name="hello" property="greeting" value="Hello JSP World" /> n The <jsp: set. Property> tag is now outside the <jsp: use. Bean> tag so it will always set the property, not just when the bean is constructed 10/25/2021 BGA 32
greeting 1. jsp <jsp: use. Bean id="hello" class="beans. Greeting" /> <jsp: set. Property name="hello" property="greeting" value="Hello JSP World" /> <html> <head> <title>Greeting JSP that uses a Greeting bean</title> </head> <body> <h 1>Greeting JSP that uses a Greeting bean</h 1> <p><jsp. get. Property name="hello" property="greeting" /> </p> </body> </html> test/jsp/greeting 1. jsp 10/25/2021 http: //localhost: 8080/test/jsp/greeting 1. jsp BGA 33
Two beans One initialized explicitly and the other is initialized using a request parameter BGA
greeting 2. jsp <jsp: use. Bean id="greet 1" class="beans. Greeting" /> <jsp: use. Bean id="greet 2" class="beans. Greeting" /> <jsp: set. Property name="greet 1" property="greeting" value="Hello JSP World" /> <jsp: set. Property name="greet 2" property="greeting" param="greeting" /> <html><head><title>Greeting JSP using two Greeting beans</title></head><body> <h 1>Greeting JSP using two Greeting beans</h 1> <p>1 st bean: <jsp: get. Property name="greet 1" property="greeting" /></p> <p>2 nd bean: <jsp: get. Property name="greet 2" property="greeting" /></p></body></html> test/jsp/greeting 2. jsp http: //localhost: 8080/test/jsp/greeting 2. jsp? greeting=Hello+Fred 10/25/2021 BGA 35
three beans and include file One initialized explicitly, one is initialized using a request parameter, and one is initialized using get. Parameter BGA
greeting 3. jsp (1) <jsp: use. Bean id="greet 1" class="beans. Greeting" /> <jsp: use. Bean id="greet 2" class="beans. Greeting" /> <jsp: use. Bean id="greet 3" class="beans. Greeting" /> <jsp: set. Property name="greet 1" property="greeting" value="Hello JSP World" /> <jsp: set. Property name="greet 2" property="greeting" param="greeting" /> <%-- Following works but param method is better --%> <jsp: set. Property name="greet 3" property="greeting" value="<%= request. get. Parameter("greeting") %>" /> 10/25/2021 BGA 37
greeting 3. jsp (2) <%-- Include file contains doctype and html tag --%> <jsp: include page="doctype. html" flush="true" /> <head><title>Greeting JSP</title></head><body> <h 1>Greeting JSP</h 1> <p>1 st bean: <jsp: get. Property name="greet 1" property="greeting" /></p> <p>2 nd bean: <jsp: get. Property name="greet 2" property="greeting" /></p> <p>3 rd bean: <jsp: get. Property name="greet 3" property="greeting" /></p> <p>request: <%= request. get. Parameter("greeting") %></p> </body></html> test/jsp/greeting 3. jsp http: //localhost: 8080/test/jsp/greeting 3. jsp? greeting=Hello+Fred 10/25/2021 BGA 38
Beans and forms Using a bean to represent the data of a form BGA
form 1_bean. html (1) <html> <head> <title>JSP Processong form using a bean</title> </head> <body> <h 1>JSP Processing form using a bean</h 1> <form action="do. Form 1 Bean. jsp" method="GET"> First name: <input type="text" name="first. Name"></ br> Last name: <input type="text" name="last. Name"> <p><input type="submit" name="button" value="Submit Name"></p> </form> </body> test/jsp/forms/form 1_bean. html </html> http: //localhost: 8080/test/jsp/forms/form 1_bean. html 10/25/2021 BGA 40
A Name bean package beans; public class Name. Bean { private String first. Name; // property private String last. Name; // property public String get. First. Name() { return first. Name; } public String get. Last. Name() { return last. Name; } } public void set. First. Name(String first) { first. Name = first; } public void set. Last. Name(String last) { last. Name = last; } beans/Name. Bean. java 10/25/2021 BGA 41
do. Form 1 Bean. jsp <jsp: use. Bean id="name" class="beans. Name. Bean" /> <jsp: set. Property name="name" property="*" /> <jsp: include page=". . /doctype. html" flush="true" /> <head> <title>JSP Form results using a bean</title> </head> <body> <h 1>JSP Form results using a bean</h 1> Hello <jsp: get. Property name="name" property="first. Name" /> <jsp: get. Property name="name" property="last. Name" /> </body> test/jsp/forms/do. Form 1 Bean. jsp </html> Note the use of property="*" to set all the bean properties using the form data vaules 10/25/2021 BGA 42
MVC approach n n MVC stands for Model-View-Controller: n n View: n n controls the application by getting the input and forwarding the request to the appropriate page visual appearance of the output data Model: n logic, calculations necessary to produce the data. Sometimes called business logic 10/25/2021 BGA 43
MVC example (Temperature) n Getting the input (Fahrenheit temperature) n n n Displaying the output (Celsius temperature) n n controller. jsp (dispatch (forward) request) form. jsp (user input of temperature) response. jsp Model (logic and calculations done by beans) n n Temperature. Bean. java (form data) Temperature. Calculator. Bean. java (does the temperature conversio) 10/25/2021 BGA 44
Temperature. Bean package beans; public class Temperature. Bean { private String temp; public String get. Temperature() { return temp; } public void set. Temperature(String t) { temp = t; } } beans/Temperature. Bean. java 10/25/2021 BGA 45
Temperature. Calculator. Bean package beans; import java. text. Decimal. Format; public class Temperature. Calculator. Bean { private double celsius; public String get. Temperature() { return new Decimal. Format("0. 00"). format(celsius); } // continued on next slide 10/25/2021 BGA 46
Temperature. Calculator. Bean public void set. Temperature(String fahr) { double f; try { f = Double. parse. Double(fahr); } catch (Number. Format. Exception e) { f = 0. 0; } celsius = (f - 32. 0) * (5. 0 / 9. 0); } } 10/25/2021 beans/Temperature. Calculator. Bean. java BGA 47
Forwarding a page n A JSP can forward the request to another page using n n <jsp: forward page="file. jsp" /> The important idea here is that this new page receives all the request parameters of the forwarding page. 10/25/2021 BGA 48
controller. jsp <jsp: use. Bean id="temp" class="beans. Temperature. Bean"> <jsp: set. Property name="temp" property="*" /> </jsp: use. Bean> <% if (request. get. Parameter("temperature") == null) { %> <jsp: forward page="form. jsp" /> <% } else { %> <jsp: forward page="response. jsp" /> <% } test/jsp/temperature/controller. jsp %> http: //localhost: 8080/test/jsp/temperature/controller. jsp 10/25/2021 BGA 49
form. jsp <jsp: include page=". . /doctype. html" flush="true" /> <head> <title>Fahrenheit to Celsius Conversion</title> </head> <body> <h 1>Fahrenheit to Celsius Conversion</h 1> <form action="controller. jsp" method="GET"> Fahrenheit temperature: <input type="text" name="temperature"></ br> <input type="submit" name="button" value="Submit Temperature"></p> </form> </body> test/jsp/temperature/form. jsp </html> 10/25/2021 BGA 50
response. jsp <jsp: use. Bean id="temp" class="beans. Temperature" scope="request" /> <jsp: use. Bean id="convert" class="beans. Temperture. Calculator. Bean" /> <jsp: set. Property name="convert" property="temperature" value="<%= temp. get. Temperature() %>" /> <jsp: include page=". . /doctype. html" flush="true" /> <head><title>Fahrenheit to Celsius Conversion</title> </head><body><h 1>Fahrenheit to Celsius Conversion</h 1> <jsp: get. Property name="temp" property="temperature" /> Fahrenheit is <jsp: get. Property name="convert" property="temperature" /> Celsius. <p><a href="controller. jsp">Convert another. . </a> </p></body></html> test/jsp/temperature/response. jsp 10/25/2021 BGA 51
Databases using JSP Display the books database table bookdisplay 1. jsp Book. Bean. java Book. Data. Bean. java DBConnector. Bean. java BGA
bookdisplay 1. jsp (1) <%@ page import="java. text. *, java. util. *, beans. *" %> <jsp: use. Bean id="db" class="DBConnector. Bean" > <jsp: set. Property name="db" property="driver" value="<%= application. get. Init. Parameter( "jdbc. driver") %>"/> <jsp: set. Property name="db" property="url" value="<%= application. get. Init. Parameter( "jdbc. url") %>"/> <jsp: set. Property name="db" property="username" value="<%= application. get. Init. Parameter( "jdbc. username") %>"/> <jsp: set. Property name="db" property="password" value="<%= application. get. Init. Parameter( "jdbc. password") %>"/> </jsp: use. Bean> 10/25/2021 BGA 53
bookdisplay 1. jsp (2) <jsp: use. Bean id="book" class="Book. Bean"/> <jsp: use. Bean id="book. Data. Bean" class="Book. Data. Bean"> <jsp: set. Property name="book. Data. Bean" property="connection" value="<%= db. get. Connection()%>"/> </jsb: use. Bean> <%! Decimal. Format cents = new Decimal. Format("0. 00"); %> <jsp: include page="doctype. html" flush="true" /> <head><title>Displaying the books database table </title></head> <body> <h 1>Displaying the books database table</h 1> 10/25/2021 BGA 54
bookdisplay 1. jsp (3) <table border="1"> <tr> <th>ISBN</th><th>TITLE</th><th>AUTHOR</th> <th>PUB</th><th>YEAR</th><th>PRICE</th> </tr> <% Iterator book. List. Iterator = book. Data. Bean. get. Iterator(); while (book. List. Iterator. has. Next()) { book = (Book. Bean) book. List. Iterator. next(); %> 10/25/2021 BGA 55
bookdisplay 1. jsp (4) <tr> <td><%= <td><%= </tr> book. get. Isbn() %></td> book. get. Title() %></td> book. get. Author() %></td> book. get. Pub() %></td> book. get. Year() %></td> cents. format(book. get. Price()) %></td> <% } %> test/jsp/bookdisplay 1. jsp </table></body></html> http: //localhost: 8080/test/jsp/bookdisplay 1. jsp 10/25/2021 BGA 56
- Slides: 56