Download Java Servlets and Java Server Pages (JSP)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts

URL redirection wikipedia , lookup

Transcript
CIS 3052 [Part 2]
Java Servlerts & JSP
Java Servlets and Java Server Pages (JSP)
In order to develop web pages with JSP and servlets using NetBeans, a Web Application
project should be initiated as explained below:
1. Create a new project and select Web Application and click on Next as shown in the
screen shots below:
2. Enter the Project Name and click on Next as shown in the screen shot below:
Matthew Xuereb © 2012/2013
Page 1 of 7
CIS 3052 [Part 2]
Java Servlerts & JSP
3. Select the Web Server (In our case GlassFish Server 3) and click on Finish.
4. The NetBeans environment should then look as shown in the screen shot below,
with the Projects section (as illustrated on the left hand side of the screen shot)
illustrating a web project rather than a Java application.
Matthew Xuereb © 2012/2013
Page 2 of 7
CIS 3052 [Part 2]
Java Servlerts & JSP
5. In order to execute the web application, right click on the source code window and
click on Run File as shown in the screen shot below:
Note that this time, when the application is executed, first the web server is initiated and
the application is executed on the default web browser.
Java Servlet Overview
The Java Servlet technology provides Web developers with a simple, consistent mechanism
for extending the functionality of a Web server and for accessing existing business systems.
A servlet can almost be thought of as an applet that runs on the server side however
without a face. Java servlets make many Web applications possible.
Matthew Xuereb © 2012/2013
Page 3 of 7
CIS 3052 [Part 2]
Java Servlerts & JSP
Servlets are the Java platform technology of choice for extending and enhancing Web
servers. Servlets provide a component-based, platform-independent method for building
Web-based applications, without the performance limitations of CGI programs. And unlike
proprietary server extension mechanisms (such as the Netscape Server API or Apache
modules), servlets are server and platform independent. This leaves you free to select a
"best of breed" strategy for your servers, platforms, and tools.
Servlets have access to the entire family of Java APIs, including the JDBC API to access
enterprise databases. Servlets can also access a library of HTTP-specific calls and receive all
the benefits of the mature Java language, including portability, performance, reusability,
and crash protection.
Today servlets are a popular choice for building interactive Web applications. Third-party
servlet containers are available for Apache Web Server, Microsoft IIS, and others. Servlet
containers are usually a component of Web and application servers, such as BEA WebLogic
Application Server, IBM WebSphere, Sun Java System Web Server, Sun Java System
Application Server, and others.
http://www.oracle.com/technetwork/java/overview-137084.html
JSP Overview
Java Server Pages (JSP) technology enables Web developers and designers to rapidly
develop and easily maintain, information-rich, dynamic Web pages that leverage existing
business systems. As part of the Java technology family, JSP technology enables rapid
development of Web-based applications that are platform independent. JSP technology
separates the user interface from content generation, enabling designers to change the
overall page layout without altering the underlying dynamic content.
http://www.oracle.com/technetwork/java/overview-138580.html
A JSP page can consists of four main elements. These are:
1. Template text. Generally consists of HTML code which consist scripting tags to be
interpreted by the browser rather than server.
2. Scripting Elements. Specifically Java code which will be embedded in servlet.
3. JSP Directives. Enable the programmer to control the overall structure of the
generated servlet.
4. JSP Actions. Enable the programmer to make use of existing components and to
control behavior of the JSP engine.
Apart from this, JSP provides access to a number of predefined variables such as request,
response and session.
JSP Scriptlets
There are three main scripting elements in JSP – Expressions, Scriptlets and Declarations.
These are explained below.
Matthew Xuereb © 2012/2013
Page 4 of 7
CIS 3052 [Part 2]
Java Servlerts & JSP
Expressions
An expression is a term which can be evaluated and which has a return of some type. Java
expressions can be inserted into a JSP such that this expression is evaluated at the moment
when the page is being serviced to a client. The syntax for a JSP expression is: <%=
..expression.. %>
Example:
Note the use of predefined variable request used to get the host name of client in the JSP
expression. A JSP page provides access to a number of variables which are usually available
in a servlet. These are request, response and session.
Matthew Xuereb © 2012/2013
Page 5 of 7
CIS 3052 [Part 2]
Java Servlerts & JSP
Scriptlets
A scriptlet enable the programmer to insert arbitrary code, possibly more complex than
expressions into the service method. The syntax for a JSP scriptlets is: <% ..java code.. %>
Example:
Matthew Xuereb © 2012/2013
Page 6 of 7
CIS 3052 [Part 2]
Java Servlerts & JSP
Declarations
A JSP declaration enables the programmer to define methods or fields outside the service
method of the servlet. The syntax for a JSP declaration is: <%! ..java code.. %>
Example:
Matthew Xuereb © 2012/2013
Page 7 of 7