Figure 513 The Hello World servlet Hello World JSP A JavaServer Page JSP is an

Figure 513 the hello world servlet hello world jsp a

This preview shows page 37 - 40 out of 74 pages.

Figure 5.13 The Hello World servlet Hello World JSP A JavaServer Page (JSP) is an alternative way to handle HTTP requests. Contrary to Java servlets, the basic structure of a JSP is an HTML file. The dynamic contents are created by Java statements sprinkled in
Image of page 37
34 COMP S311 Java Application Development and Programming Languages appropriate places in the HTML file. Following is the HelloWorld.jsp file that has the same effect as the Hello World servlet. <HTML> <HEAD> <TITLE>Hello World JSP</TITLE> </HEAD> <BODY> <H2>Hello World</H2> Time is: <%= new java.util.Date().toString() %> </BODY> </HTML> When you run the servlet, it may get you a browser screen that looks like this. Figure 5.14 The HelloWorld.jsp The HellowWorld.jsp file looks almost identical to an HTML file except for the JSP expression enclosed in <%= and %> responsible for producing the time. <%= new java.util.Date().toString() %> In the following reading you will learn the basics of JSP. Reading (online) JSP Basics — Study Sections 38.1 to 38.4 of the online version of the textbook. Section 38.2 shows you how to prepare and execute a JSP in NetBeans. Section 38.3 shows how a JSP is processed by the server. Section 38.4 discusses JSP scripting elements. JSP translation The Web server must be configured to support JSP. When a request for a JSP is received by the server, it will see if the JSP has been translated before. If not, the JSP will be translated into a Java servlet.
Image of page 38
Unit 5 35 The translation only needs to be done once per JSP. As long as the JSP has not been modified, the same translated servlet can be used for subsequent requests of the JSP. No new translation is necessary until the JSP is modified. The Web server does not execute the JSP directly. It executes the servlet translated from the JSP. Why do we bother with the additional processing of JSP pages if they will be executed as servlets anyway? When a HTTP response has a large amount of HTML code, the output statements and string operations in the servlet are unwieldy. The corresponding JSP page will be more manageable. JSP scripting elements Scripting elements are used within JSP pages mainly to generate dynamic contents. JSP scriptlets <% . . . %> Scriptlets are fragments of Java code that appear in a JSP enclosed in <% and %> . Java code in scriptlets can be intermixed with HTML elements to produce the desirable effect. For example, the closing brace of the for -loop in line 9 does not appear on the same line. The closing brace appears on line 12 after the insertion of static texts and dynamic expressions on lines 10 and 11. 9 <% for (int i=0; i <= 10; i++) { %> 10 Factorial of <%= i %> is 11 <%= computeFactorial(i) %> <br /> 12 <% } %> How to interpret this scriptlet? The hard-coded text Factorial of on line 10 and the HTML tag <br /> on line 11 appear inside the for -loop. Since the loop iterates ten times, there will be ten occurrences of the text and the tag in the HTTP response.
Image of page 39
Image of page 40

You've reached the end of your free preview.

Want to read all 74 pages?

  • Fall '19
  • Web server, HTTP cookie

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture