What Is JSP? How Does It Compare To ASP?

JSP (Java Server Pages) is a server side scripting technology that is similar to ASP (Active Server Pages) is many respects. But where ASP relies on the windows specific COM standard for embedded objects (e.g. ADO), JSP uses platform neutral java beans. And whereas ASP scripts can be written in either VBScript (the default) or JScript, JSP scripts can be written in java, a true object oriented language with strong typing (no variants!), exception handling, automatic memory management, encapsulation, and subclassing. Like ASP and PHP, code which will be executed on the server (not returned to the browser) can simply be imbedded directly into JSP pages.

With ASP, when a browser requests a page with a '.asp' extension, the web server passes the request on to the ASP interpreter which loads and runs the script. With JSP, when a browser requests a page with a '.jsp' extension, the JSP container on the web server runs the script as a servlet (each instance in a separate thread), after checking to see whether the jsp page needs compiling.

JSP Performance

Multiple (simultaneous) requests for the same JSP page result in multiple threads being created (not multiple processes). Since threads require significantly fewer resources to create than processes, JSP pages are very efficient. Some JSP containers support "in process" execution which allows the JSP servlet to run as part of the HTTP server itself, which results in even greater efficiency. By implementing JSP pages as compiled servlets which run in multiple threads, JSP scales upwards quite well making it's performance compare very favorably with other server side technologies such as CGI and ASP.

JSP Methodologies -- Method One (Imbedded Script)

You can imbed your java code directly into your JSP page. This is similar to the way it is done in ASP. For example, the following code would check the URL of this JSP page and if a parameter called username is found, it assigns that parameter to the variable sUserName and displays it on the page.

<HTML>
<BODY>
<% String sUserName = request.getParameter("userName");
if (sUserName == null)
sUserName = "guest"; %>
<b>
Welcome to our website, <%= sUserName %>
</b>
</BODY>
</HTML>

If the URL requesting this page was 'http://www.mydomain.com/login.jsp?username=fred', the following would appear in the user's browser:
Welcome to our website, fred

If the URL requesting this page was 'http://www.mydomain.com/login.jsp', the following would appear in the user's browser:
Welcome to our website, guest

This method of writing JSP pages is the quickest and easiest. But tightly coupling the java code and HTML in the same file results in two disadvantages:
A) The java code which cannot be easily reused in other pages
B) The presentation (HTML) and implementation (java) cannot be worked on separately. Often times java programmers aren't the best layout people and web page designers often don't know java.

Two JSP Methodologies -- Method Two (Separate Beans From HTML)

Alternatively, you can separate your HTML from your java code by writing java beans and using the jsp tags to access the bean from your HTML. Here is what your java bean (class) might look like:

package mystuff;
public class LoginBean implements java.io.Serializable
{
String username;

public LoginBean() //constructor takes no arguments
{
this.username = "guest"; //assign default value
}

public String getUsername()
{
return username;
}

public void setUsername(String username)
{
this.username = username;
}
}

This simple bean creates a single property (username) which is defaulted to "guest" in the constructor. Your JSP page which then uses this java bean would look like:

<HTML>
<BODY>
<jsp:useBean id="login" scope="session" class="mystuff.LoginBean"/>
<jsp:setProperty name="login" property="username" value= "username"/>
<b>
Welcome to our website, <jsp:getProperty name="login" property="username"/>
</b>
</BODY>
</HTML>


The <jsp:useBean> tag declares the bean to be used with this page, and identifies this bean as "login". The <jsp:setProperty> tag provides access to the "username" property and assigns it the value of the "username" parameter passed into the page via a URL query string.

For example, if the URL requesting this page was 'http://www.mydomain.com/login.jsp?username=fred', the query string is "username=fred" and the following HTML would appear in the user's browser:
Welcome to our website, fred

If the URL requesting this page was 'http://www.mydomain.com/login.jsp', no parameter is passed in and the default value set in the bean's constructor would be used. The following HTML would appear in the user's browser:
Welcome to our website, guest

The drawback of this second method of writing JSP pages is that it requires more upfront time to write the bean class and to write the separate HTML file. However, by isolating the java code (implementaion) from the HTML (presentation) we
A) create a bean which can reused in other JSP pages
B) Make it possible to have one person write the bean and a different person (perhaps a non-java web designer) create the layout without worrying about overwriting the other person's code.

Where Can I Find Out More About JSP?

http://www.java.sun.com/products/jsp
http://www.java.sun.com/products/jsp/faq.html
http://www.burridge.net/jsp/jspinfo.html



   
Website copyright © 2002, SoftTech Design, Inc. All rights reserved worldwide.