Using JavaServer Pages Technology for Java Web Applications – Develop Java Web Applications to Access Databases

8.1.2   Using JavaServer Pages Technology for Java Web Applications

In fact, the JavaServer Pages technique provides a way of using Java code within an HTML page, which means that you can embed a piece of Java code or part of Servlet’s functions into the code on the client side with appropriate tags.

The embedded Java code will be compiled and executed by the JSP engine on the server side as the application runs. From this point of view, the JavaServer Pages can be considered as a part of a Servlet or as an extension of an application server located at the client side. Although the JSP provides a lot of advantages over Servlets, it is actually a subclass of the Servlet class and built based on Servlets technology.

JavaServer Pages can be implemented not only in HTML files but also in the following files:

  • Script language files, which allow you to specify a block of Java code
  • JSP directives, which enable you to control the JSP structure and environment
  • Actions, which allow you to specify executing commands such as loading a parameter from a client page

The JSP provides some useful built-in or implicit objects to perform most interfacing functions with clients and servers. The so-called implicit objects in JSP are objects that are automatically available in JSP. Implicit objects are Java objects that the JSP Container provides to a developer to access in their applications using JavaBeans and Servlets. These objects are called implicit objects because they are automatically instantiated. Some popular implicit JSP objects include:

  • request
  • response
  • out
  • session
  • application
  • pageContext
  • page
  • exception

Among those objects, request, response and session are most popular objects and are often used in the interface between clients and servers. Some other objects, such as out and page-Context, are mainly used to write output to the client and to access most built-in objects.

Figure 8.4 shows an example of using the out and pageContext objects to write feedback to the client (top section) and to get a session object (bottom section).

Two popular tags used by JSP to distinguish it from other languages are:

  • <%….%>
  • <jsp: . . . . />

FIGURE 8.4   An example of using the out and pageContext objects.

FIGURE 8.5   An example of a JavaServer Page file.

Between these two tags, you can put any Java code you want to improve the execution of Servlet techniques for Java Web applications.

In fact, you can get a JSP file or page easily by just changing the extension of the Login.html file, such as from Login.html to Login.jsp. Yes, it is this easy to get a JSP file.

An example of using a JSP file to display the received user login data is shown in Figure 8.5.

Within the tags <% . . .%>, two lines of Java code are written, and they are used to call the getParameter() method to pick up the username and password entered by the user from the cli-ent Web page. You can directly display the received login data on the client side with the Java local variables uname and pword enclosed within the JSP tags.

Leave a Comment



                    Blog, Contribute ,Privacy,Terms of Use,Trademarks,© estherknow 2024