Java Reference
In-Depth Information
<HTML><HEAD>
<TITLE>Enhanced Welcome Page</ TITLE >
</HEAD>
<BODY>
<%
try
{
String userId = session.getAttribute("userId");
%>
<h1>Enhanced Welcome Page</H1>
<%
out.println("Welcome to the Web site, " + userId);
}
catch(Exception e)
{
out.println("Failure:" + e);
}
%>
</BODY></HTML>
The tags <% and %> are special tags that tell the servlet engine (the servlet engine
processes JSPs, too; you'll see why in a moment) that this is Java code, not HTML.
The session and out variables are automatically allocated and initialized in a JSP,
although session could be null if, in fact, no session exists.
When it runs, this JSP obtains the user ID from the session and writes the
phrase “Welcome to the Web site” followed by the user ID after a heading that says
“Enhanced Welcome Page.”
Although JSPs appear to be extensions of HTML, in reality JSPs are servlets in
disguise. When the Web server invokes the servlet engine and the servlet engine de-
tects that the file name has a .jsp extension, it knows the page is a JSP. The servlet
engine finds the page that contains the text above and parses it. As the engine is
parsing, it converts the page into the source code of a servlet. The static HTML el-
ements are converted to out.println() method calls containing the HTML. The
Java code is simply copied into the servlet source code. It wraps the generated code
Search WWH ::




Custom Search