01.01.2015 Views

UML Weekend Crash Course™ - To Parent Directory

UML Weekend Crash Course™ - To Parent Directory

UML Weekend Crash Course™ - To Parent Directory

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

284<br />

Sunday Afternoon<br />

and time. A JSP page is composed of your Web content, such as HTML, with Java code intermixed<br />

to insert the dynamic content. Comparing the Java servlet in Listing 27-2 and the<br />

JSP page in Listing 27-3, you can probably see why JSP code is usually far easier to write<br />

and develop than a servlet for producing Web content. A JSP page generally has less complicated<br />

Java logic, is usually easier to read, and is much easier to maintain. A JSP page also<br />

doesn’t need to be compiled by the programmer and may be easier to deploy into the Web<br />

server, which makes the development and maintenance process a bit simpler. Other template<br />

page technologies like ASP, PHP, and Cold Fusion work in a similar way, although each has<br />

its own specific features, advantages, and disadvantages.<br />

It is worth noting that JavaServer Pages turn into servlets. The JSP container class automatically<br />

writes a Java servlet much like the one in Listing 27-2 (although significantly<br />

harder to read) that has the same functionality as the JSP page that you wrote. All requests<br />

for your JSP page will actually be handled by the Java servlet that represents it. Thus, JSP<br />

is simply an easy way to write a Java servlet without having to write as much Java code.<br />

The <strong>UML</strong> state diagram in Figure 27-4 explains this. The programmer writes a JSP. At or<br />

before the first request for that JSP, the JSP container automatically writes the servlet that<br />

represents that JSP and compiles it. From this point on, the lifecycle of a JSP is the same as<br />

a servlet. When a request comes in, the servlet will be instantiated and the same instance of<br />

the servlet will be used for all requests until the JSP container decides that the servlet<br />

should be unloaded, usually due to the container shutting down or a long period with no<br />

requests for that JSP. If the programmer changes the JSP, that servlet will be permanently<br />

unloaded and the lifecycle starts over with the new version of the JSP being translated.<br />

JSP Written By Programmer<br />

JSP Page<br />

JSP Container Translates JSP<br />

Servlet Class<br />

(Not Instantiated)<br />

Final<br />

State<br />

JSP Removed Or Modified<br />

Servlet Class (Instantiated<br />

and initialized)<br />

HTTP Request Received<br />

JSP Container Unloads JSP<br />

HTTP Request Received/Request Serviced<br />

Figure 27-4 <strong>UML</strong> state diagram, JSP Lifecycle<br />

You may think that writing only JSPs, as opposed to writing servlets, would be easier,<br />

but that isn’t actually the case. If you need to write a servlet or JSP that primarily generates<br />

Web content, then it will almost always be easier to write it as a JSP. If you need to<br />

write a servlet or JSP that has a lot of logic and generates very little or no content, then it<br />

will usually be easier to write it as a servlet.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!