Introduction to Java Server Pages

infestationwatchΛογισμικό & κατασκευή λογ/κού

28 Οκτ 2013 (πριν από 3 χρόνια και 8 μήνες)

79 εμφανίσεις

Introduction to Java Server Pages

What is JSP

Java server pages is the J2EE technology for generating dynamic web content. JSP
specification is provided buy sun micro systems for vendors to implement. JSP
specification is based on the functionality prov
ided by Servlet specification. JSP
specification provides more convenient web page authoring framework than Servlet and
simplifies the creation and management of dynamic web content. JSP provides
based approach to content generation. JSP pages are

textual documents
containing HTML, XHTML, or XML markup with embedded Java code and custom JSP

How does JSP differs from Servlets

From the developers perspective Servlets are pure java programs with class and
method definitions whereas a JSP pa
ge is much like a text document or web page. With
servlets developer must write java code to output the entire markup, on the other hand
a JSP page can be designed like a static web page. JSP separates static content from
dynamic content and Java beans and
/or custom JSP tags are used to generate dynamic
portion of the web page. Servlets are well suited for handling client request and
executing application logic whereas JSP pages are well suited as views.

Every JSP is a Servlet

Every JSP page is compile
d into a servlet by JSP engine. The first time the JSP
engine receives the request for a JSP, jsp engine converts the jsp files in to java
servlets and than compiles this servlet, it is called the translation phase. If needed JSP
pages can also be precompi
led. Once the JSP page is compiled into a servlet, all the
subsequent requests will be handled by the compiled servlet class. If you modify the
source code of the JSP file, container automatically detects the changes and recompile
the JSP page the next tim
e that JSP page is requested.

Following figure explains the JSP translation process.

@TODO Insert image

Note: If the JSP pages are translated on demand when the container receives the
request for an untranslated JSP page than there will be a slow respons
e for the first
time any JSP page is requested. On other hand container may choose to translate the
JSP pages at deployment time in that case the first request for any JSP page will take
the normal time.

Overview of HTTP protocol

This tutorial gives the

basic overview of HTTP protocol, if you are a web developer and
already know the basics of the protocol, you can safely skip this tutorial. But if you are
new to server side programming, you should read this tutorial carefully, as you will
need to have ba
sic understanding of HTTP protocol when you start learning Servlets
and Java server pages.

The HTTP Protocol

Do you surf over the internet? If your answer is yes than you are already using
HTTP protocol unknowingly. When you access any website using y
our favorite web
browser, you are using HTTP protocol. Web browser uses the HTTP protocol to
communicate with the web server.

HTTP stands for Hyper Text Transfer Protocol which is an application level protocol
for transferring textual data between clie
nt and server. However HTTP can be used for
many other tasks beyond its use for hypertext. HTTP is stateless protocol, that mean,
client such as web browser sends the request to server, server responds and sends the
requested data (such as a HTML document)

or returns error code and closes the
connection. Once the response is returned, server doesn

t remember anything about the

Http request methods and Headers

Whenever the client or browser sends the request to server for any resource such
as a
HTML document, it specifies the HTTP method, request URI, protocol version and
optional Header information. After the client sends the request, server processes the
request and sends the response. Response contains information such as status
information, r
esponse headers and response data.

Following is an example of HTTP request which uses GET request method to ask for
a HTML document named tutorial.html using HTTP protocol version 1.1

GET /tutorial.html HTTP/1.1

Following is the example of reque
st header, which provides additional information
about the request.

Agent: Mozilla/4.0 (compatible; MSIE 4.0; Windows 95)

Accept: image/gif, image/jpeg, text/*, */*

Above header specifies the information about the client software and what MIME
ype the client accepts in response, using User
Agent and Accept headers.

Http request methods

The request method indicates the server what action to perform on the resource
identified by the request
URI. HTTP/1.0 specifies three request methods: GET,
and HEAD. HTTP/1.1 specifies five additional request methods: OPTIONS, PUT,
TRACE, DELETE, and CONNECT. Servlets can process any of the above requests,
however GET and POST methods meets most of the common requirements, so most of
the time you will de
velop the Servlets which process theses two methods.

The GET request method

The GET request method is very simple and most frequently used method. The GET
request is used for getting static information from the server such as HTML document,
images, an
d CSS files. Although GET request is designed to retrieve static information,
it can be used to retrieve dynamic information also by appending query string at the end
of request URI.

Query String

Query string is used to pass additional request paramet
ers along with the request.
Query string consist of name value pairs and appended at the end of request URL with
character ? Query string format



Here the name is the name of the parameter and value is the value
of the parameter
that needs to be sent to the server for processing. name value pairs (or query
parameters) are separated by ampersand sign (&). Following example show an HTTP
URL which sends the request parameter userid in query string.


The POST request method

The POST request method is commonly used to access dynamic resources. POST
request is used to send the large amount of data to the server. POST request can be
used to upload files to servers or ev
en to send serializable Java objects or raw bytes.
Unlike GET, POST request sends all the data as part of the HTTP request body, so it

t appear in browser address bar and cannot be bookmarked. The data sent to the
server is invisible to user.

T request is generally used to send HTML form parameters to server and to
upload files.

Difference between GET and POST request methods

There are some differences between GET and POST requests. GET request sends
the request parameters as query string
appended at the end of the request URL,
whereas POST request sends the request parameters as part of the HTTP request body.
Since GET request sends the parameters as query string, parameters are displayed in
browser address bar. User can bookmark the URL.
Depending on how sensitive request
parameters are, it may be a security issue. One another issue with GET request is,
some servers limits the length of the URL to 240 characters, so if the too many
parameters are appended in query string and URL exceeds th
is length, some web
servers might not accept the request. These restrictions don

t apply to POST method as
it sends the parameters as request body. POST requests cannot be bookmarked.

NOTE: HTTP/1.1 does not impose any restrictions on the length of URL.