Presentation is loading. Please wait.

Presentation is loading. Please wait.

DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.

Similar presentations


Presentation on theme: "DAT602 Database Application Development Lecture 15 Java Server Pages Part 1."— Presentation transcript:

1 DAT602 Database Application Development Lecture 15 Java Server Pages Part 1

2 Static webpage VS. Dynamic webpage Classical hypertext navigation, with HTML or XHTML alone, provides "static" content, meaning that the user requests a web page and simply views the page and the information on that page. Database Application Development - Lecture 15

3 Static webpage VS. Dynamic webpage However, web navigation can also provide an "interactive experience" that is termed "dynamic". Content (text, images, form fields, etc.) on a web page can change, in response to different contexts or conditions. Database Application Development - Lecture 15

4 There are two ways to create dynamic webpage 1. Using client-side scripting 2. Using server-side scripting Web pages that adhere to the second definition are often created with the help of server- side languages such as PHP, Perl, ASP, ASP.NET, JSP. Database Application Development - Lecture 15

5 What is JSP? Java Server Pages (JSP) is a server side Java technology that allows software developers to create dynamically generated web pages, with HTML, XML, or other document types, in response to a Web client request to a Java Web Application container (server). Database Application Development - Lecture 15

6 JSP Server You must have a JSP capable web-server or application server for running your JSP webpages. There are many such servers available: TomCat from Apache WebLogic from BEA Systems WebSpherefrom IBM Database Application Development - Lecture 15

7 How to install Tomcat in Windows OS? Before installing Tomcat, please ensure you already install the Java successfully in your Windows. Step 1:Download Tomcat. The latest version is 6.0.20, you can download it from following link: http://apache.etoak.com/tomcat/tomcat- 6/v6.0.20/bin/apache-tomcat-6.0.20.zip Database Application Development - Lecture 15

8 How to install Tomcat in Windows OS? Step 2: Unzip this downloaded zip file. You can put it in any place you want. Step 3: Configure environment variable. Add a path “TOMCAT_HOME” which value is the path of Tomcat directory. Step 4: Startup Tomcat server. Run “startup.bat” under../Tomcat/bin. Step 5: Open a web browser, type Http://localhost:8080, there should be a webpage if you install Tomcat successfully.Http://localhost:8080 Database Application Development - Lecture 15

9 Your first JSP JSP simply puts Java inside HTML pages. You can take any existing HTML page and change its extension to ".jsp" instead of ".html". What is happening behind the scenes is that your JSP is being turned into a Java file, compiled and loaded. NOTE: you must deploy your jsp file in your jsp server. For tomcat, there is a directory “webapps” for your jsp. Database Application Development - Lecture 15

10 Adding dynamic content via expressions Change extension from.html to.jsp, that makes JSP useful is the ability to embed Java. Add following code into index.jsp The character sequences enclose Java expressions, which are evaluated at run time. Database Application Development - Lecture 15 Hello! The time is now

11 Scriptlet JSP also allows you to write blocks of Java code inside the JSP. You do this by placing your Java code between characters. This block of code is known as a "scriptlet". Database Application Development - Lecture 15

12 Scriptlet Here is a modified version of our JSP from previous section, adding in a scriptlet. Database Application Development - Lecture 15 <% // This is a scriptlet. Notice that the "date" // variable we declare here is available in the // embedded expression later on System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is

13 Output in JSP By itself a scriptlet does not generate HTML. If a scriptlet wants to generate HTML, it can use a variable called "out". This variable does not need to be declared. Database Application Development - Lecture 15

14 Example of using “out” in JSP The "out" variable is of type javax.servlet.jsp.JspWriter.javax.servlet.jsp.JspWriter Database Application Development - Lecture 15 <% // This scriptlet declares and initializes “date” System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now <% // This scriptlet generates HTML output out.println( String.valueOf( date )); %>

15 Variable “request” (javax.servlet.http.HttpServeletRequest) It is a pre-defined variable. A "request" in server-side processing refers to the transaction between a browser and the server. When someone clicks or enters a URL, the browser sends a "request" to the server for that URL, and shows the data returned. Database Application Development - Lecture 15 request Browser Server

16 Variable “request” The JSP "request" variable is used to obtain information from the request as sent by the browser. Database Application Development - Lecture 15 <% // This scriptlet declares and initializes "date" System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now <% out.println( date ); out.println( " Your machine's address is " ); out.println( request.getRemoteHost()); %> Example of getting requester’s IP from “request”

17 Variable “response” A similar variable is "response". This can be used to affect the response being sent to the browser. For instance, you can call response.sendRedirect( anotherUrl ); to send a response to the browser that it should load a different URL. This response will actualy go all the way to the browser. The browser will then send a different request, to "anotherUrl". Database Application Development - Lecture 15 response Browser Server

18 Mixing Scriptlets and HTML For more complicated HTML, using the “out” variable all the time loses some of the advantages of JSP programming. It is simpler to mix scriptlets and HTML. In following example, we generate a table containing the numbers from 1 to N. Database Application Development - Lecture 15 Number

19 Mixing Scriptlets and HTML The important things to notice are how the %> and <% characters appear in the middle of the "for" loop, to let you drop back into HTML and then to come back to the scriptlet. The concepts are simple here -- as you can see, you can drop out of the scriptlets, write normal HTML, and get back into the scriptlet. Database Application Development - Lecture 15

20 Mixing Scriptlets and HTML Another example of mixing scriptlets and HTML is shown below -- here it is assumed that there is a boolean variable named "hello" available. If you set it to true, you will see one output, if you set it to false, you will see another output. Database Application Development - Lecture 15 Hello, world Goodbye, world

21 JSP Directives It is possible to use "import" statements in JSPs, but the syntax is a little different from normal Java. Try the following example: Database Application Development - Lecture 15 <% System.out.println( "Evaluating date now" ); Date date = new Date(); %> Hello! The time is now

22 JSP Directives A JSP "directive" starts with <%@ characters. To import more than one item, separate the package names by commas, e.g. Database Application Development - Lecture 15

23 JSP Directives The include directive is used to physically include the contents of another file. The included file can be HTML or JSP or anything else -- the result is as if the original JSP file actually contained the included text. To see this directive in action, create a new JSP. Database Application Development - Lecture 15 Going to include hello.jsp...

24 Reference: http://www.jsptut.com/ Database Application Development - Lecture 15


Download ppt "DAT602 Database Application Development Lecture 15 Java Server Pages Part 1."

Similar presentations


Ads by Google