Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Java Server Pages Allows the embedding of Java commands in a page of HTML. Popular for UI heavy solutions. These commands are then interpreted by a JSP.

Similar presentations


Presentation on theme: "1 Java Server Pages Allows the embedding of Java commands in a page of HTML. Popular for UI heavy solutions. These commands are then interpreted by a JSP."— Presentation transcript:

1 1 Java Server Pages Allows the embedding of Java commands in a page of HTML. Popular for UI heavy solutions. These commands are then interpreted by a JSP engine in the webserver (like ASP/PHP). Therefore any webserver must have a JSP engine installed and attached in order to process JSP pages (IIS does not have a default engine installed but one such as TomCat can be attached).

2 2 JSP JSP is a Server Side Include (SSI) mechanism. A JSP can contain standalone Java code, i.e. no other elements are needed to interpret that page. A JSP can contain Java code which contacts other elements, e.g. the Java code in your JSP page could create objects, contact a database, etc.

3 3 JSP vs Servlet A servlet is a Java class which is instantiated and executed when its URL is called. A JSP is a web page which has Java commands embedded in it. Servlets are used for situations where a great deal of functionality is required. Servlets can also perform some operations and access resources JSPs can’t. JSPs are used in situations where relatively simple code is needed and a servlet would be viewed as overkill.

4 4 Procedure 1. Name your page with the extension JSP. 2. Put in Java expressions which will be evaluated into the tag format: e.g.. The result of the evaluated expression will replace this tag in the final page. (The result of the expression can also be discarded). HTML comments can be used to comment the JSP code.

5 5 Objects in a JSP Can create, and use, an object of any available Java class type in a JSP Example, assume the class ‘classname’ has been compiled and the bytecode is visible to the page containing the following tag A session is a continuous communication session between a client (e.g. Browser) and the server (e.g. Tomcat/Glassfish)

6 6 Using an Object in a JSP To call a method in the object which will return no result use the tag: Note that the statement ends with a semi-colon. If the method returns a value which you would like inserted into the web page then use the tag: Note there is no semi-colon in this statement.

7 7 Example Name:

8 8 Example public class classUsedByJSP { String name; public String getName() { return name; } // End returnName public void setName(String nameSupplied) { name = nameSupplied; } // End setName } // End classUsedByJSP

9 9 Using JavaBeans in JSP A Java Bean is a Java class that’s written in a particular way. One aspect of a Bean is that methods which obtain the value of a variable and alter the value of a variable are written using the words ‘get’ and ‘set’ which is always followed by the name of the variable which has the first letter capitalised.

10 10 Example of a basic JavaBean class beanEg { private int amount; // Note the capital letter in the get and set methods public void setAmount(int value) {} public int getAmount() {} } // End class beanEg

11 11 JSP Forms and Beans Can write a class using some of the characteristics of a Bean. This will allow form component values to be automatically placed in an object of this class by the VM. Conversion to different data types is also done automatically.

12 12 Procedure Write the HTML form and name the form elements. Write the class definition and use same names in the class for variables as was used in the form. Methods which alter and access the elements should be named get and set. Write the JSP file and tell VM to setup an object of the class type

13 13 Procedure Insert an instruction in the JSP file to tell the VM to call the methods in the object which match the names of the form components supplied. The VM automatically inserts values using methods according to names and you are now free to use the relevant object in your JSP page.

14 14 Example – Basic JSP

15 15 Example – Class definition public class dataClass { private String string; private int value; public void setString(String stringSupplied) { string = stringSupplied;} // End setString public String getString() { return string; } // End getString public void setValue(int valueSupplied) { value = valueSupplied; } // End setValue public int getValue() { return value; } // End getValue } // End dataClass

16 16

17 17 Cookies Can set and get cookie via JSP pages (and servlets) Cookies not popular due to prejudice on consumers part.

18 18 Cookies-setting <% Cookie authorised = new Cookie("makeCookie", "true"); authorised.setMaxAge(60*60*24*365); response.addCookie(authorised); %>

19 19 Cookies-getting <% Cookie[] cookieList = request.getCookies(); for(int count=0;count<cookieList.length;count++) out.println(cookieList[count].getName()+" "); %>

20 20 Tag Libraries Custom JSP tags available if JSP is run on specific web server Tag libraries generally offer useful features which allow rapid development of JSP pages, like simplified database access.

21 21 JSP Execution JSP pages are actually executed as servlets. When a JSP engine receives a JSP page it creates a standard servlet from a template and inserts the embedded code into this servlet. It then executes the servlet and replaces the JSP code with the results.


Download ppt "1 Java Server Pages Allows the embedding of Java commands in a page of HTML. Popular for UI heavy solutions. These commands are then interpreted by a JSP."

Similar presentations


Ads by Google