So you want to learn more about using Java from your server, but you don’t know whether Java Server Pages (JSPs) or servlets are the way to go? Well, we will look at both of them here. If you already have designer or content development experience building static HTML pages, Macromedia’s Cold Fusion, or Microsoft’s Active Sever Pages (ASP) applications, building JSPs may be the next logical skill to acquire. On the other hand, if you have a higher level of Java skill, you might be ready for servlets.
Let’s look at what is involved in these technologies and see where they fit on our server.
What is a JSP, and what can it do?
JSP technology is a component of the Java programming language used to deploy dynamic Web pages. Hierarchically, JSP pages occupy the first rung of the platform-independent Java. An extension of Java servlet technology, JSP is best used for separating Web page layout from Web site content, and it’s especially useful for dynamic information—intensive Web sites that may or may not include static content. Just like HTML or ASP and Cold Fusion, you can modify JSP text using a text editor, such as NotePad.
JSPs are the recommended Web access layer for n-tier architecture, according to a jGuru.com JSP tutorial. Content specialists and designers will find JSP similar to tag-based languages, such as ASP or Cold Fusion.
The following code examples demonstrate a survey form request/response written as JSP pages and one JavaBean. JSP commands are included among the standard HTML formatting tags in the JSP pages. In the survey input page, SurveyInput.jsp, shown in Listing A, the only JSP command is <jsp:include.… This calls another JSP file, SurveyAppHeader.jsp (not included), which returns the HTML for a standard header used on all pages of the survey application. The form includes a drop-down list of items to choose from and a text box where the user can include an explanation for his or her choice of the toughest superhero.
Notice that the HTML form posts to the JSP file SurveyPost.jsp, which you can see in Listing B. This JSP captures the values entered in the survey, writes the values to a database, and responds to the client that the survey was successfully accepted. The SurveyPost.jsp page works in conjunction with a JavaBean, SurveyBean, shown in Listing C. The <use:bean... command in the post page tells the container to find and load SurveyBean. The <jsp:setProperty... command calls each set command in the JavaBean, passing in the values entered in the HTML form.
Notice we use the asterisk character (*) in the property attribute. This tells the container to set each property. We could have also called <jsp:setProperty... for each form field. For example:
<jsp:setProperty name="beanID" property="toughest" />
The JSP code section <% beanID.saveToDB(); %> calls the JavaBean’s saveToDB(); method. This is where a more experienced Java programmer would most likely use Java Database Connectivity (JDBC) to save the values to a database.
The last section of the SurveyPost.jsp page tells the user what he or she entered by calling <jsp:getProperty... for each form field. This in turn calls the corresponding get methods of the SurveyBean.
The SurveyBean itself could be written by an experienced Java developer, since it handles the more complicated issues of saving the data to a database and has no HTML presentation information. In that case, it is designed to be used by an HTML content specialist within a JSP page.mits.
- · "Answers are never far away at jGuru.com”
- · jGuru.com: JavaServer Pages Fundamentals
- · "Transitioning into object-oriented programming using Java”
What is a servlet, and what can it do?
Servlets should be used to extend the capabilities of the Web server. According to Sun Blueprints, servlets “are best suited for low-level application functions that don’t require frequent modification.” They are compiled pieces of code that must be recompiled whenever a change is made.
Located in the middle tier, servlets process HTTP requests from a Web browser and can return the content generated at the server, much like CGI scripts. Unlike a CGI script, however, with a servlet, the request loads, is compiled into the server memory, initializes, and stays loaded for subsequent requests. CGI scripts end after the request is fulfilled, and information must load into memory with each request.
Designers and developers learning to program using servlets must have a good understanding of Java. If the servlets are to share data, they must also understand threading and be able to write synchronized calls. If your specialty is content creation or page design, you will have a larger learning curve than you will have with JSP. If building servlets continues to remain outside of your knowledge set, you’ll at least need to understand servlet technology well enough to be able to include the proper requests within the JSP text.
The servlet code example shown in Listing D demonstrates the superhero form request/response shown in the JSP example above. The same functionality exists as in the JSP example, but each line of HTML returned to the client is handled by calling out.println. As you can see in the doGet method, more code is required to display the same form. The doPost method is equivalent to the SurveyPost.jsp page. Here we use request.getParameter("toughest"); to get the HTML form field value.
The choice is yours
As you can see, the servlet code requires a more experienced Java programmer. It must be compiled by the programmer, and the class file generated must be located in a directory that the servlet server knows about. A JSP page must also be copied to a directory that the server knows about, but the servlet container handles compilation. Either can get the job done; the choice is yours.