What is JSP

advertisement
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 provided 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
template-based approach to content generation. JSP pages are textual documents
containing HTML, XHTML, or XML markup with embedded Java code and custom JSP
tags.
How does JSP differs from Servlets
From the developers perspective Servlets are pure java programs with class and
method definitions whereas a JSP page 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 compiled 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 precompiled. 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 time 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 response 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 basic 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 your 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 client 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
client.
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, response 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 request header, which provides additional information
about the request.
User-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
type 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, POST
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 develop 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, and 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 parameters along with the request.
Query string consist of name value pairs and appended at the end of request URL with
character ? Query string format
?name1=value1&name2=value&name3=value3 ….
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.
http://www.jsptube.com/login.jsp?userid=sudhir
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 even 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
doesn’t appear in browser address bar and cannot be bookmarked. The data sent to the
server is invisible to user.
POST 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 this 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.
Download