WEB TECHNOLOGIES UNIT-6 JSP 6.1 introductions to JSP: Java

advertisement
WEB TECHNOLOGIES
UNIT-6
JSP
6.1 introductions to JSP:
Java server pages—JSP, is a java-based technology that simplifies the process of
developing interactive (dynamic) web sites. Jsp with its java and HTML tags can
easily make static web pages into dynamic.
Java server pages are simple text files with the extension.jsp. JSP files contains
HTML tags and java code and with this embedded java code, the designer can
access data from java code running on the server. When a JSP page (on HTTP
server) is requested by the user, the java code from HTML tags is separated,
compiled, executed and the result is supplied back to the page. The result is
communicated (as response) to the user. This result makes Jsp dynamic.
JSP creates dynamic content in the web. Now we will see the evolution of dynamic
content technologies earlier to JSP.
Activer Server Pages:
Microsoft’s active server pages (ASP) support multiple scripting languages like
perlscript (developed based on PERL), jscript (based on javascript) and VBS script
(default one). VBScript supports active components which are compiled code
objects that can access the database and can also the do file manipulation. Microsoft
provides tools and documentation which makes easy to write the code. The major
limitation ASP is it supports microsoft’s internet information server (IIS) running
under widows NT operating system.
Differences between ASP and jSP:
ASP
Windows dependent.
ASP software is server dependent.
JSP
Platform independent.
Jsp Software is server
Independent.
(works only on IIS and
Personal server).
jspengine.jar is responsible to
Interpret JSP code.
extension is .jsp.
asp.dll is responsible to interpret ASP
code.
Extension is .asp.
Only Microsoft components Can
be model
compatible in terms of
component embedded
Scriplet is in ASP and can be
VBScript
scriplet is only in java
language.
or jscript or javascript
Java Servlets:
Sum grabbed the opportunity to develop a software in the domain (area ) that can
create dynamic web content while overcoming the drawbacks of earlier technologies
discussed above. In 1996 sun introduced servlets to work on server (sometimes
referred as web servers) in a similar fashion it developed applets to run on clients
(also referred as client browsers or web browsers).
CGI programs spawns a new process to handle each request where as servlets run
inside a single process. This process runs a JVM (java virtual machine). JVM
creates a java thread to handle each servlet request. You know already in
1
multithreading chapter, java threads have less overhead than processes. At the same
time, because servlets are written in java, they enjoy all the benefits of the core java
platform : an object-oriented programming, model, automatic memory
management, cross-platform portability and access to the rich collection of java
APIs now available for accessing databases, directory servers, network resources
and so forth.
Java Server Pages:
JSP is originally intended to be written in any scripting languages, but now only
java language is permitted. Like cold fusion, JSP supports as set of HTML- like tags
that interact with the java objects on the server. JSP 1.1 specification provides
facility for the programmer to create custom tags and tag libraries that can be
loaded into a JSP page.
Similar to servlets and JSP of sun, many products available, in the market that can
run on web servers, of which two are popular. They are JRUN form live software
and servlet exec of new Atlanta. As dynamic content generation is gaining
momentum among feature-rich web sites and web applications, sun would like to
position the JSP on the top to play a major role in the ongoing evolution of web
technology.
The problems with servlets:
1. use println statements to generate HTML pages, for developing a servlet
based application, knowledge of java as well as HTML code is necessary.
2. Changing the look and feel of the application or adding support for a new
type of client requires the serlet code to be updated and recompiled.
3. It’s hard to take advantage of web page development tools when designing
the application interface. If such tools are used to develop the web page
layout, the generated HTML must then be manually embedded into the
servlet code, a process which is time consuming, error prone, and extremely
boring.
4. With JSP these problems are solved by separating the request processing
and business logic code from the presentation. All the HTML code should be
placed in a separate JSP page, just as in a regular web page, and add a few
JSP elements to generate the dynamic parts of the page. The request
processing should be in the servlet class, and the business login can be
handled by javabeans and EJB components.
How JSP works ?
We know in servlets chapter, that when a server ( to put correctly, web server)
receives a request for a servlet program from a client (to put correctly, web
browser), it forwards (sends ) the request to a special process (program )
dedicated to handling servlet execution. This special process is referred as servlet
container (earlier name, sevlet engine). JSP is typically implemented via servlets.
The same servlet container in the context of JSP, is called as JSP container or
JSP engine.
Servlet containe (or JSP container ) is a separate process in HTTP server that
needs a JVM (because servlets are written in java ) because HTTP server may
be written in other languages. ( for those servers that are written in java, servlets
need not be separate process), multiple requests for servlets and JSP are handled
2
through java threads. You know it already that threads ( light- weight takes less
overhead than proncesses (heavy – weight).
Web Server
JSP Page
Request
Web Browser
Generated
Servlet
Response
1.
2.
3.
4.
Compiled
Cleint requests a JSP page.
Jsp container compiles the JSP into a servlet.
Generated servlet is compiled.
Response to the browser.
Advantages of JSP:
1. JSP is very much useful for server side programming
2. It provides a very powerful and flexible mechanism to produce dynamic web
pages.
3. Enen though JSP is based on servlet technology, the amount of time required
to develop JSP will be lesser than the time required developing the servlet.
4. It does not require any special set up at the client side.
5. It is complied for efficient server processing.
6. JSP is a specification and not a product. Hence developers can develop
variety of applications and add up to performance and qualify of software
products.
7. It provides built in support for HTTP session management.
8. JSP is an essential component of J2EE and easy to combine static templates.
3
The Anatomy of JSP page:
the JSP page contains the JSP elements and template text. The template text can be
scripting code such as HTML.XML or a simple plain test. Various JSP elements can
be action tags, custom tags, JSTL library elements.
JSP Element
Template Text
JSP Element
s
JSP Code Structure
Template Text
JSP Element
When JSP request gets processed template text and JSP elements are merged
together and sent to the browser as a response.
JSP PROCESSING:
JSP pages can be processed using JSP container only.
 Client makes a request for required JSP page to the server. The server must
have JSP container so that JSP request can be processed.
 One receiving this request the JSP container searches and then reads the
desired JSP page. Then this jsp page is straight away converted to
corresponding servlet. Basically any JSP page is a combination of template
text and JSP element. Every template text is translated into corresponding
println statement.
 Every JSP element is converted into corresponding java code. This phase is
called translation phase. The output of translation phase is a servelt.
 This servlet is then compiled to generated the servlet class file. Using this class
the response can be generated. This phase is called request processing phase.
 The JSP container thus executes the servlet class file
 A requested page is then returned to the client as a response.
4
JSP Container
Example.jsp
Request and reads JSP
Generate Servlet
Client
Example.java
Generate class
Example.class
Response is
sent to client
Executes
JSP application design with MVC:
The MVC stands for model-view- controller. Any server application is classified in
three parts such as business logic, presentation and request processing. According to
the MVC design model, the model corresponds to business logic, view corresponds
to presentation and controller corresponds to request processing. The business logic
means the coding logic, the presentation means to the code written for look and feel
of the webpage. The request process means a combination of business logic and
presentation to generate the response.
Model
View
Controller
Setting up the JSP environment:
for executing any JSP we require java development kit (JDK)
 any web server such as
o apache tomcat web server
o the JBoss open source application server
o BEA system weblogic application server
o IBM’s websphare application server
o Oracle’s application server
5
Installing the java software development Kit:
Before installing the tomcat server we must install JDK in our computer. We
have already discussed the method of installing JDK in section 5.3
Tomacat server installation:
We have already discussed the method of installing apache tomcat web server in
section 5.4
Example 1:
Write a simple JSP age for displaying the message’’ web technologies’’
<HTML>
<HEAD>
<TITLE> my first JSP program</TITLE>
</HEAD>
<BODY BGCOLOR="lightcyan" TEXT="green">
<H2 ALIGN=center> WEB TECHNOLOGIES </H2>
</BODY>
</HTML>
Just save the above file and JSP does not require compilation. Copy the file to
working directory of tomcat and run from the browser with the command:
http://loaclhost:8080/vanesh/ex.jsp.
6
Download