Java Server Pages
April 5, 2000
As described earlier JSP build on the ASP technology and use Java
as a programming language.
We saw the WelcomeServlet which takes myName as a parameter from
HTML-form and displays a personalized welcome message. Now let's
see how to achieve the same with a JSP:
Welcome.jsp:
<HTML>
<HEAD>
<B> Welcome <%= request.getParameter("myName")%>
</B>
</HEAD>
</HTML>
That's all ! Suddenly life's become easy huh?
The code between "<%" and "%>" is executed at the server and
the HTML, with the results, is passed back to the client.
One important fact about JSP one should always keep in mind is:
every JSP is internally converted into a servlet. So even
if it looks simple it it is probably going to put more 'load' on
your m/c. A rule of thumb is 'if you have a lot of HTML code and
some 'run time variables' use JSP. If you need a lot of processing,
a servlet is the best choice. Actually one can 'interleave'
servlets and JSP. We will see that in the next section.
There are some pre-defined objects (such as request, response and
session) available to each JSP. We can use all the normal
programming constructs such as:
<% if ((request.getParameter("myName") == "Harshal") {%>
<B> Hey we have the same name !</B>
<% } else { %>
<B> Welcome <%= request.getParameter("myName") %> </B>
<%} %>
Finally, we can call methods of an external program -
Java bean.
Additional JSP resources:
HTML Generation
Building Web Applications Using Servlets and JSP
The Session Object
|