Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Server Pages (JSP). A Java Servlets Example A simple "HelloWorld" servlet, that also prints the current date. import java.io.*; import javax.servlet.*;

Similar presentations


Presentation on theme: "Java Server Pages (JSP). A Java Servlets Example A simple "HelloWorld" servlet, that also prints the current date. import java.io.*; import javax.servlet.*;"— Presentation transcript:

1 Java Server Pages (JSP)

2 A Java Servlets Example A simple "HelloWorld" servlet, that also prints the current date. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println(" "); out.println(" Hello World "); out.println(" "); out.println(" Hello World "); out.println("Today is: " + (new java.util.Date().toString()) ); out.println(" "); } // doGet } // HelloWorld In order to run it, do the following: –Place it in a file, HelloWorld.java –Compile it. –Place the resulting HelloWorld.class file in the "servlets" directory. –Run it by pointing browser to: http://server.address/servlets/HelloWorld

3 A JSP Example The previous page can be written using JSP as shown below: Hello World Hello World Today is: In order to run it, do the following: –Place it in a file, HelloWorld.jsp in the same directory as your.html files –Run it by pointing browser to: http://server.address/~youraccount/HelloWorld.jsp

4 Elements of JSP Whenever a.jsp is requested for the first time, the server does the following: 1. Translates the.jsp page into a servlet 2. Compiles the servlet into a class file 3. Executes the servlet (response is sent to the client) Subsequent requests (as long as the.jsp page is unchanged) use the same loaded class file. Anatomy of a JSP Page –A JSP page is a mixture of standard HTML tags, web page content, and some dynamic content that is specified using JSP constructs. Everything except the JSP constructs is called Template Text.

5 JSP Constructs: JSP Comments JSP Comments: Different from HTML comments JSP comments are used for documenting JSP code and are not visible client-side (using browser's View Source option) where as HTML comments are visible.

6 JSP Constructs: JAVA Expressions Format: Example: Output of the expression is placed in the HTML template at the same location.

7 JSP Constructs: Variables in JAVA Expressions There are some pre-defined Java variables/objects available for use in expressions (provide access to important servlet functionality): –request This is the same object as HttpServletRequest parameter in th get/post methods. Same methods (like, getParameter, getAttribute, etc) can be applied to it. –out The servlet printwriter. –session Same as servlet session object. Example: JSP Expressions: Predefined Objects Using Predefined Objects Your Hostname: Your Session ID: The value of INFO parameter:

8 JSP Constructs: Scriptlets Scriptlets are arbitrary pieces of Java code inserted in the page using the format: Example 1 JSP: Scriptlets <% String bgColor = request.getParameter("COLOR"); if (bgColor == null) bgColor = "WHITE"; %> " > Example Scriptlet: Sets background color Example 2 JSP: Scriptlets 2 " > Example Scriptlet: Conditionally sets background color You did not supply a color, I used white. Here is the color you requested. { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/3385060/slides/slide_8.jpg", "name": "JSP Constructs: Scriptlets Scriptlets are arbitrary pieces of Java code inserted in the page using the format: Example 1 JSP: Scriptlets <% String bgColor = request.getParameter( COLOR ); if (bgColor == null) bgColor = WHITE ; %> > Example Scriptlet: Sets background color Example 2 JSP: Scriptlets 2 > Example Scriptlet: Conditionally sets background color You did not supply a color, I used white.", "description": "Here is the color you requested.

9 JSP Constructs: Scriptlets, contd. Using Arrays: One can easily use scriptlets to loop over arrays. In this example, the user is presented with choice boxes. When s/he presses the submit button, the choices are displayed. Example Classical Rock Jazz Blues DC GoGo <% String[] selected = request.getParameterValues("music"); if (selected != null && selected.length != 0) { %> You like the following kinds of music: <% for (int i = 0; i < selected.length; i++) { out.println(" " + selected[i]); } %>

10 JSP Constructs: Declarations You can define variables and/or methods: Example <%! private int hitCount = 0; String randomColor() { java.util.Random random = new java.util.Random(); int R = (int) (random.nextFloat() * 255); int G = (int) (random.nextFloat() * 255); int B = (int) (random.nextFloat() * 255); return "#" + Integer.toString(R, 16) + Integer.toString(G, 16) + Integer.toString(B, 16); } %> " > This page has been accessed times.

11 JSP Constructs: Summary Expressions Scriptlets Declarations Availability of pre-defined objects

12 JSP Directives Format: – Directives are used to specify the structure of the resulting servlet. There are three directives: page, include, and taglib

13 JSP: Page Directive There are 11 specifiable attributes for this directive: import, contentType, isThreadSafe, session, buffer, autoflush, extends, info, errorPage, and language Example Directives can be placed anywhere in the document, but are often placed at the very top. Hello World Hello World Today is:

14 JSP: Other Directives The include Directive –Is used to include a file in the main document. –There are two versions of this. The first one, shown below, includes the file at translation time. –The second version, includes the file at request time. The taglib directive –This is used to define custom markup tags. Refer to JSP documentation for details on this.


Download ppt "Java Server Pages (JSP). A Java Servlets Example A simple "HelloWorld" servlet, that also prints the current date. import java.io.*; import javax.servlet.*;"

Similar presentations


Ads by Google