Jsp basico

Solo disponible en BuenasTareas
  • Páginas : 7 (1744 palabras )
  • Descarga(s) : 0
  • Publicado : 25 de mayo de 2011
Leer documento completo
Vista previa del texto
Basic Principles of JSPs
Chapter 4, “Basic Principles of Servlets,” introduced you
to simple Web applications using servlets. Although very useful for writing dynamic server-side code, servlets suffer from some disadvantages. In particular, coding complex HTML pages is somewhat tedious and error prone (due to the need to pepper the code with out.println() statements), and a servlet developerhas to take on the dual roles of developer of application logic and designer of Web pages. JavaServer Pages (JSPs) were designed to address these disadvantages. In this chapter, you will study the JSP syntax and the translate-compile life cycle used to generate a servlet that services the HTTP request using the information in the JSP Web page.

• What Is a JSP? • Deploying aJSP in Tomcat • Elements of a JSP Page • Currency Converter JSP • Using JavaBeans in a JSP • The Currency Converter JSP Using a JavaBean • The JSP Life Cycle • JSPs and the Deployment Descriptor File

What Is a JSP?
A JSP is “just a servlet by another name” and is used in the same way to generate dynamic HTML pages. Unlike a servlet, which you deploy as a Java class, a JSP is deployed in atextual form similar to a simple HTML page. When the JSP is first accessed, it is translated into a Java class and compiled. The JSP then services HTTP requests like any other servlet. A JSP consists of a mix of HTML elements and special JSP elements. You use the JSP elements, among other things, to embed Java code in the HTML. For example, Listing 5.1 shows a simple JSP that prints out the currentdate. The special JSP scripting element is used to introduce the Java code.



Basic Principles of JSPs


A Simple JSP

Date JSP Today’s date is

Before examining JSP elements in more detail, you will see what Tomcat does when this JSP is deployed.

Deploying a JSP in Tomcat
Deploying a JSP is very simple. There are only a couple of rules: • JSPsare stored in their textual form in the application directory. • A JSP must have a .jsp suffix. So, to deploy the JSP in Listing 5.1:
1. Create a new Web application directory in Tomcat’s webapps directory; call it basic-jsp. 2. Create a subdirectory called WEB-INF in the basic-jsp directory. 3. Copy Listing 5.1 into a file called date.jsp in this application directory. 4. Stop and restart Tomcatso it recognizes the new Web application.

Access the JSP using the URL http://localhost:8080/basic-jsp/date.jsp. If there are no errors, you will see a screen similar to the one in Figure 5.1. For this simple example, it is unlikely that you experienced any problems, but a more complex JSP might have generated some errors. The next section on the JSP translate-compile cycle will explain how tohandle these errors. If you do not find the answer to your problem there, Chapter 6, “Troubleshooting Servlets and JSPs,” covers the subject in far greater detail.

Deploying a JSP in Tomcat



date.jsp displayed using Netscape 6.

JSP Translate—Compile Cycle
Unlike the servlets covered in Chapter 4, JSPs are not compiled before deployment.
NOTE Actually, it is possibleto compile JSPs and deploy them in exactly the same way as servlets, but this is not normally done and will not be covered.

The JSP file is stored in its textual form in the Web application. When a client first requests the page, Tomcat (or more correctly the Jasper JSP container) automatically initiates the translate-compile cycle illustrated in Figure 5.2. This diagram assumes the translationand compilations are both successful. Later in this chapter we discuss how to fix any failures in this cycle. The JSP is first translated into Java source code, and if there are no translation errors, it is then compiled into a servlet class file. The translation and compilation obviously causes an initial delay for the first access. If all is well, the JSP will be displayed, but if the...