You are on page 1of 18

Introduction to Java Server Pages technology

http://www.techinterviews.com /?p=92

What is JSP?
Java based technology that simplifies the developing of dynamic web sites JSP pages are HTML pages with embedded code that allows to access data from Java code running on the server JSP provides separation of HTML presentation logic from the application logic.

JSP Flow

JSP Technology
JSP technology provides a way to combine the worlds of HTML and Java servlet programming. JSP specs are built on the Java Servlet API. JSP supports two different styles for adding dynamic content to web pages:
JSP pages can embed actual programming code (typically Java) JSP supports a set of HTML-like tags that interact with Java objects on the server (without the need for raw Java code to appear in the page).

JSP Example: Hello World


1.

2.

SimpleJSP.jsp

SimpleJSP.jsp - the Bean edition


JSP includes tags for interacting with JavaBeans. JavaBean is a simply Java class that follow JavaBeans specs: rules for defining a Beans ctor & methods for accessing and setting their properties.

SimpleJSP.jsp - the Bean edition

JSP Example: Hello World


In both cases the http request is:
http://localhost:8080/SimpleJSP.jsp?name=Naomi

The response from JSP container would be:

How it is work?
Client request for a page ending with ".jsp. Web Server fires up the JSP engine. The JSP engine checks to see if the JSP file is new or changed. The JSP engine takes the page and converts it into a Java servlet (by JSP parser) The JSP engine compiles the servlet (by standard Java compiler). Servlet Engine executes the new Java servlet using the standard API. Servlets output is transferred by Web Server as a http response.

JSP Pages content


standard HTML tags & scripts (JavaScript/VBscript) new tags for scripting in the Java language.
Expressions:
<%=expression %> or XML variant: <jsp: expression>expression </jsp:expression>

For Example:
<%= fact(12) %> <%= (hours <12) ? AM : PM %> <%= Math.pow(radius, 2) %>

Scriptlets:
<% scriptlet %> or XML variant: <jsp:scriptlet> scriptlet </jsp:scriptlet>

Declarations:
<%! declaration (s) %> or XML variant: <jsp:declaration> declaration(s) </jsp: declaration>

For Example

JSP Pages content cont.


JSP directives is a set of tags for providing the JSP container with page specific instructions for how the document should be processed. Directives affect global properties of the JSP page.
<%@ page attr1=val1 attr2= %>

Comments for adding documentation


Comments that will be in the output: <!-- comment --> JSP comments <%-- comment --%> Scripting language comments: <% /* comment */ %>

JSP Pages content cont.


Actions and implicit objects
JSP implicit objects: page out config session request application response pageContext exception

JSP Pages content cont.


Beans tags
allows JSP pages to call reusable components called JavaBeans components. The tag <jsp:useBean> syntax is: <jsp:useBean id="Bean_name" scope="scope_value" class="class_name" beanName="ser_filename" type="class_or_interface_name" > properties tags </jsp:useBean> <jsp:setProperty> tag syntax is: <jsp:setProperty name="property_name" property="property_value" />

JSP benefits
Java-based technology Vendor-neutral Full access to underlying Java platform Performance Reusable components (JavaBeans) Separating presentation and implementation

Resources
Sun JSP 1.1 Specs and description Server Side Java Resource Site IBM education courses JSP resource index JSP insider

The end

Scriptlet Example

You might also like