Wednesday, July 29, 2009

Web Dev

c15c0 cafe

==========================================================

What is JSP?

In its basic form, a JSP page is simply an HTML web page that contains additional bits of code that execute application logic to generate dynamic content. This application logic may involve JavaBeans, JDBC objects, Enterprise Java Beans (EJB), and Remote Method Invocation (RMI) objects, all of which can be easily accessed from a JSP page.

Features

Because JSP pages are automatically compiled as needed, web authors can make changes to presentation code without recompiling application logic. This makes JSP a more flexible method of generating dynamic web content than Java servlets, whose functionality JavaServer Pages extend.

Relation JSP - Servlet

Enter JavaServer Pages, which are an extension of the Servlet API. In fact, JSP pages are compiled into servlets before they are used, so they have all of the benefits of servlets, including access to Java APIs. Because JSP pages are generally presentation code with application logic embedded in them, they can be thought of as "inside-out" servlets.

Content

HTML tags are processed by a user's web browser to display the page;

JSP tags are used by the web server to generate dynamic content.

Sample:

<HTML>
<%@ page language="java" imports="java.util.*" %>

<H1>Welcome</H1>

<P>Today is </P>
<jsp:useBean id="clock" class="jspCalendar" />
<UL>
<LI>Day: <%= clock.getDayOfMonth() %>
<LI>Year: <%= clock.getYear() %>
</UL>
<%-- Check for AM or PM --%>

<%! int time = Calendar.getInstance().get(Calendar.AM_PM); %>
<%

if (time == Calendar.AM) {
%>
Good Morning
<%

}
else {
%>
Good Afternoon
<%

}
%>
<%@ include file="copyright.html" %>
</HTML>

Format description

JSP actions (or JSP tags)

perform a variety of functions and extend the capabilities of JSP. JSP actions use XML-like syntax, and are used to (among other things) manage JavaBean components. In the sample page, a jsp:useBean action initializes a JavaBean that is used in subsequent portions of the page:

<jsp:useBean id=="clock" class=="jspCalendar" />

If the sample page had needed to get or set properties of this bean, other JSP actions would have been given, using the following syntax:

<jsp:getProperty name="bean" property="property" />
<jsp:setProperty name="bean" property="property" value="value" />

Directives

instructions that are processed by the JSP engine when the page is compiled to a servlet. Directives are used to set page-level instructions, insert data from external files, and specify custom tag libraries. Directives are defined between <%@ and %>. In the above example, directives define the language of the page (Java), import the Java classes needed by the embedded code, and specify that the contents of an HTML file should be inserted at the bottom of the page:

<%@ page language=="java" imports=="java.util.*" %>
<%@ include file=="copyright.html" %>

Declarations

similar to variable declarations in Java, and define variables for subsequent use in expressions or scriptlets. Declarations are defined between <%! and %>. In the above sample page an int is declared and given a value corresponding to the time of day (AM or PM):

<%! int time = Calendar.getInstance().get(Calendar.AM_PM); %>

Expressions

variables or constants that are inserted into the data returned by the web server, and are defined with the <%= and %>. In the sample page, expressions make calls on a JavaBean component and insert the resulting data into the page:

<%= clock.getDayOfMonth() %>
<%= clock.getYear() %>

Scriptlets

blocks of Java code embedded within a JSP page. Scriptlet code is inserted verbatim into the servlet generated from the page, and is defined between <% and %>. A scriptlet in the above sample determines the time of day and greets the user accordingly:

<%
if (time == Calendar.AM) {
%>
Good Morning
<%
}
else {
%>
Good Afternoon
<%
}
%>

Comments

similar to HTML comments, and are stripped from the page by the JSP engine when it is executed. This means that JSP comments are not returned to the user's browser. Unlike HTML comments, which are given between <!-- and --> tags, JSP comments are given between <%-- and --%>. For example:

<%-- Check for AM or PM --%>

Custom Tag

Tag handler (implements javax.servlet.jsp.Tag) & tag library descriptor.

Sample of using custom tag:

<HTML>
<%@ taglib uri="/tlds/menuDB.tld" prefix="menu" %>

<H1>Today's Menu</H1>

<P>Lunch</P>
<%@ include file="lunch_menu.html" %>

<P>Our Special of the Day</P>
<menu: insertCatchOfDay meal="lunch" >

</HTML>


Applet

Used in HTML, as a tag “applet”. The Applet class should impl the interface java.applet.Applet/javax.swing.JApplet, depends on weather need Swing support.

<html>

<head>

<title>Title</title>

</head>

<body onload="load()">

<applet code="HelloWorld.class" codebase="./" archive="helloWorld.jar" width="150" height="30"></applet>

</body>

</html>

JavaScript

No relation to Java, owned by Netscape but not Sun.

0 comments: