In this section, we address some of the most common misunderstandings about JSP.
Forgetting JSP Is Server-Side Technology
The book's Web site lists the lead author's email address: firstname.lastname@example.org. Furthermore, Marty teaches JSP and servlet training courses for various companies and at public venues . Consequently, he gets a lot of email with servlet and JSP questions. Here are some typical questions he has received (most of them repeatedly).
All of these questions are based upon the assumption that browsers know something about the server-side process. But they do not. Thus:
Confusing Translation Time with Request Time
A JSP page is converted into a servlet. The servlet is compiled, loaded into the server's memory, initialized , and executed. But which step happens when? To answer that question, remember two points:
Table 10.1 gives some common scenarios and tells whether or not each step occurs in that scenario. The most frequently misunderstood entries are highlighted. When referring to the table, note that servlets resulting from JSP pages use the _jspService method (called for both GET and POST requests ), not doGet or doPost . Also, for initialization, they use the jspInit method, not the init method.
Table 10.1. JSP Operations in Various Scenarios
Thinking JSP Alone Is Sufficient
There is a small community of developers that are so enamored with JSP that they use it for practically everything. Most of these developers never use servlets; many never even use auxiliary helper classesthey just build large complex JSP pages for each and every task.
This is a mistake. JSP is an excellent tool. But the fundamental problem it addresses is presentation : the difficulty of creating and maintaining HTML to represent the result of a request. JSP is a good choice for pages with relatively fixed formats and lots of static text. JSP, by itself, is less good for applications that have a variable structure, is poor for applications that have mostly dynamic data, and is totally unsuitable for applications that output binary data or manipulate HTTP without generating explicit output (as with the search engine servlet of Section 6.4). Still other applications are best solved with neither servlets alone nor JSP alone, but with a combination of the two (see Chapter 15).
JSP is a powerful and widely applicable tool. Nevertheless, other tools are sometimes better. Choose the right tool for the job.
Thinking Servlets Alone Are Sufficient
At the other end of the spectrum from the JSP-only camp is the servlets-only camp. Adherents of this camp state, quite rightly, that JSP pages are really just dressed up servlets, so JSP pages cannot accomplish anything that could not also be done with servlets. From this, they conclude that you should stick with servlets, where you have access to the full underlying power, have complete control, and can see exactly what is happening. Hmm, have you heard this argument before? It sounds a lot like the position of the "don't be a wimp; write all your code in assembly language" crowd .
Yes, you could use servlets for any task for which JSP is used. But it is not always equally convenient to do so. For tasks that involve a lot of static HTML content, use of JSP technology (or a combination of JSP and servlets) simplifies the creation and maintenance of the HTML, permits you to use industry standard Web site creation tools, and lets you "divide and conquer" by splitting your effort between the Java developers and the Web developers.
Servlets are powerful and widely applicable tools. Nevertheless, other tools are sometimes better. Choose the right tool for the job.