Java Server Pages
User Rating: / 1
PoorBest 
technology

JSP Overview

Java Server Pages (JSP) is a technology that lets you mix regular, static HTML with dynamically-generated HTML. Many Web pages that are built by CGI programs are mostly static, with the dynamic part limited to a few small locations. But most CGI variations, including servlets, make you generate the entire page via your program, even though most of it is always the same. JSP lets you create the two parts separately. Here's an example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Welcome to Our Store</TITLE></HEAD>
<BODY>
<H1>Welcome to Our Store</H1>
<SMALL>Welcome,
<!-- User name is "New User" for first-time visitors -->
<% out.println(Utils.getUserNameFromCookie(request)); %>

To access your account settings, click

<A HREF="Account-Settings.html">here.</A></SMALL>
<P>

Regular HTML for all the rest of the on-line store's Web page.

</BODY></HTML>

What are the Advantages of JSP?

vs. Active Server Pages (ASP). ASP is a similar technology from Microsoft. The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS-specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers.

vs. Pure Servlets. JSP doesn't give you anything that you couldn't in principle do with a servlet. But it is more convenient to write (and to modify!) regular HTML than to have a zillion println statements that generate the HTML. Plus, by separating the look from the content you can put different people on different tasks: your Web page design experts can build the HTML, leaving places for your servlet programmers to insert the dynamic content.

vs. Server-Side Includes (SSI). SSI is a widely-supported technology for including externally-defined pieces into a static Web page. JSP is better because it lets you use servlets instead of a separate program to generate that dynamic part. Besides, SSI is really only intended for simple inclusions, not for "real" programs that use form data, make database connections, and the like.

vs. JavaScript. JavaScript can generate HTML dynamically on the client. This is a useful capability, but only handles situations where the dynamic information is based on the client's environment. With the exception of cookies, HTTP and form submission data is not available to JavaScript. And, since it runs on the client, JavaScript can't access server-side resources like databases, catalogs, pricing information, and the like.

vs. Static HTML. Regular HTML, of course, cannot contain dynamic information. JSP is so easy and convenient that it is quite feasible to augment HTML pages that only benefit marginally by the insertion of small amounts of dynamic data. Previously, the cost of using dynamic data would preclude its use in all but the most valuable instances.

Details

The Java|Pipe's JAVA machine is running the Apache web server (http://www.apache.org) with Apache Tomcat 5 (http://tomcat.apache.org/tomcat).The goal of the Apache Tomcat Project is to provide commercial-quality server solutions based on the JAVA Platform that are developed in an open and cooperative fashion.Tomcat is the Reference Implementation for the Java Servlet and Java Server Pages Technologies. Tomcat is the official reference implementation for these complementary technologies. Click here for more about Java|Pipe's JAVA hosting packages.

Usage

You normally give your file a .jsp extension, and typically install it in any place you could place a normal Web page.Although what you write often looks more like a regular HTML file than a servlet, behind the scenes, the JSP page just gets converted to a normal servlet, with the static HTML simply being printed to the output stream associated with the servlet's service method. This is normally done the first time the page is requested, and developers can simply request the page themselves when first installing it if they want to be sure that the first real user doesn't get a momentary delay when the JSP page is translated to a servlet and the servlet is compiled and loaded.

Note also that many Web servers let you define aliases that so that a URL that appears to reference an HTML file really points to a servlet or JSP page. Aside from the regular HTML, there are three main types of JSP constructs that you embed in a page: scripting elements, directives, and actions. Scripting elements let you specify Java code that will become part of the resultant servlet,directives let you control the overall structure of the servlet, and actions let you specify existing components that should be used, and otherwise control the behavior of the JSP engine. Click here for more about Java|Pipe's JAVA hosting packages.

Scripting Elements

There are three typess of JSP scripting elements:

Expressions - which have are evaluated independently and return a value (therefore have an = 'equals' sign:

Today's date is:

<%= new java.util.Date()%>

Declarations - which contain import statements and declare instance and class variables. These are indicated with a '!':

<%! private String learn = "JSPing"; %>

Scriptlets - a page may have a number of these and they are all placed in the service() method of the JSP servlet:

<% if (request.getParameter("dosomething") != null) %>
You are <%= learn %>
<% else %>
You don't want to JSP

The following are predefined variables your scriptlet code can access:

  • request - the HttpServletRequest object
  • response - the HttpServletResponse object
  • out - a PrintWriter.
  • session - a HttpSession object (unless sessions are turned off for your page using directives).
  • application - the ServletContext object which contains attributes settable and gettable for all servlets in the same context.
  • config - the ServletConfig object.
  • pageContext - like a ServletContext but for JSP pages.
  • page - equivalent to this operator.

Directives

There are three main types of directives:

  1. page
  2. include
  3. taglib

The page directive

To import classes use:

<%@ page import="mypackage.myclasses.*, myotherpackage.myotherclasses.MyClass" %>

To change the content type:

<%@ page content-type="text/plain" %>

To extend a class:

<%@ page extends="mypackage.MyClass"%>

To specify a page for handling exceptions:

<%@ page errorpage="/mywebfiles/myErrorPage.jsp"%>

This will send back the specified error page to the client whenever an unchecked exception occurs.

To specify a page can be used as an error page for another page:

<%@ page isErrorPage="true"%>

The Include directive

to include files into pages. One is to use:

<%@ include file="/myWebPages/fileToInclude %>

(or)

<jsp:include page="/myWebPages/fileToInclude" flush="true"/>

The taglib directive

As well as beans, we can use other classes called Tags. These are able to manipulate page content, and are accessed within a page in a similar way to beans.

Tag classes must have an XML descriptor and must implement the javax.servlet.jsp.tagext.Tag interface. Two classes which implement this interface, and can be extended by your classes are TagSupport and BodyTagSupport.

JSP to MySQL

ConnectorJ is installed in the shared tomcat classpath. You may install your own drivers in WEB-INF/lib/

To connect to MySQL with JSP you will need the Class.forName path to the MySQL Driver:

Class.forName("com.mysql.jdbc.Driver").newInstance()

To test your connection:

Class.forName("com.mysql.jdbc.Driver").newInstance();

java.sql.Connection conn;

conn = DriverManager.getConnection(
"jdbc:mysql://localhost/dbname?user=blah&password=blah");

For more information about connector|J click here: mysql.com

Click here for more about Java|Pipe's JAVA hosting packages.