0% found this document useful (0 votes)
31 views

JSP Intro

Uploaded by

shubhangi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

JSP Intro

Uploaded by

shubhangi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

JSP

• JSP termed as Java Server Pages is a


technology introduced by Sun Microsystems Inc.
to develop the web application in more efficient
way than Servlets. It has got many advanced
features than servlets, one of them itself define
the JSP i.e. JSP separates the presentation
logic from the business logic and provide the
designer and developer of the web application to
work independently without any hassle.
• The user goes to a web site made using JSP. The user goes to a
JSP page (ending with .jsp). The web browser makes the request
via the Internet.

• The JSP request gets sent to the Web server.

• The Web server recognises that the file required is special


(.jsp),therefore passes the JSP file to the JSP Servlet Engine.

• If the JSP file has been called the first time,the JSP file is
parsed,otherwise go to step 7.

• The next step is to generate a special Servlet from the JSP file. All
the HTML required is converted to println statements.

• The Servlet source code is compiled into a class.

• The Servlet is instantiated,calling the init and service methods.

• HTML from the Servlet output is sent via the Internet.

• HTML results are displayed on the user's web browser.


How JSP and JSP Container
function
• A JSP page is executed in a JSP container or a JSP
engine, which is installed in a web server or in a
application server. When a client asks for a JSP page
the engine wraps up the request and delivers it to the
JSP page along with a response object. The JSP page
processes the request and modifies the response object
to incorporate the communication with the client. The
container or the engine, on getting the response, wraps
up the responses from the JSP page and delivers it to
the client. The underlying layer for a JSP is actually a
servlet implementation. The abstractions of the request
and response are the same as the ServletRequest and
ServletResponse respectively. If the protocol used is
HTTP, then the corresponding objects are
HttpServletRequest and HttpServletResponse.
• The first time the engine intercepts a
request for a JSP, it compiles this
translation unit (the JSP page and other
dependent files) into a class file that
implements the servlet protocol
JSP compared to ASP


JSP and ASP are fairly similar in the functionality
that they provide. JSP may have slightly higher
learning curve. Both allow embedded code in an
HTML page,session variables and database
access and manipulation. Whereas ASP is
mostly found on Microsoft platforms i.e. NT,JSP
can operate on any platform that conforms to the
J2EE specification. JSP allow component reuse
by using Javabeans and EJBs. ASP provides
the use of COM / ActiveX controls.
JSP compared to ASP.NET

• ASP.NET is based on the Microsoft .NET


framework. The .NET framework allows
applications to be developed using different
programming languages such as Visual
Basic,C# and JavaScript. JSP and Java still has
the advantage that it is supported on many
different platforms and the Java community has
many years of experience in designing and
developing Enterprise quality scalable
applications. This is not to say that ASP.NET is
bad,actually it is quite an improvement over the
old ASP code.
JSP compared to Servlets
• A Servlet is a Java class that provides
special server side service. It is hard work
to write HTML code in Servlets. In Servlets
you need to have lots of println statements
to generate HTML. JSP pages are
converted to Servlets so actually can do
the same thing as old Java Servlets. 
Benefits of JSP
• simplify application design by separating dynamic
content from static template display data
• it allows to more cleanly separate the roles of web
application/HTML designer from a software developer
• The JSP technology is platform independent, in its
dynamic web pages, its web servers, and its underlying
server components. That is, JSP pages perform perfectly
without any hassle on any platform, run on any web
server, and web-enabled application server. The JSP
pages can be accessed from any web server.
• The JSP technology emphasizes the use of reusable
components. These components can be combined or
manipulated towards developing more purposeful
components and page design. This definitely reduces
development time
• JSP page is built using components
such as :
• Directives
• Declarations
• Scriplets
• Expressions
• Standard Actions
• Custom Tags

JSP DECLARATIVES
Syntax of JSP Declaratives are:  
<%!
/java codes
   %>
• JSP Declaratives begins with <%! and ends %> with .We can embed any
amount of java code in the JSP Declaratives. Variables and functions
defined in the declaratives are class level and can be used anywhere in
the JSP page.
• Code placed in this tag must end in a semicolon ( ; ).

• <%!
          private int counter = 0 ;
          private String get Account ( int accountNo) ;
%>
 
DIRECTIVES
• Syntax of JSP directives is:
• <%@directive attribute="value" %>
• Where directive may be:
• page: page is used to provide the information about it.
Example: <%@page language="java" %> 
   
• include: include is used to include a file in the JSP page.
Example: <%@ include file="/header.jsp" %> 
  
• taglib: taglib is used to use the custom tags in the JSP pages
(custom tags allows us to defined our own tags).
Example: <%@ taglib uri="tlds/taglib.tld" prefix="mytag" %> 
attributes of the page directive
• language
• extends
• import
• session
• buffer
• autoFlush
• isThreadSafe
• info
• errorPage
• contentType
• isErrorPage
• pageEncoding
• isELIgnored
attribute
• language="java"
This tells the server that the page is using the java language. Current JSP
specification supports only java language.
Example: <%@page language="java" %> 
   
• extends="mypackage.myclass"
This attribute is used when we want to extend any class. We can use comma(,) to
import more than one packages.
Example: <%@page language="java" import="java.sql.*,mypackage.myclass" %> 
   
• session="true"
When this value is true session data is available to the JSP page otherwise not. By
default this value is true.
Example: <%@page language="java" session="true" %> 
  
• errorPage="error.jsp"
errorPage is used to handle the un-handled exceptions in the page.
Example: <%@page language="java" session="true" errorPage="error.jsp"  %> 
   
• contentType="text/html;charset=ISO-8859-1"
Use this attribute to set the mime type and character set of the JSP.
Example: <%@page language="java" session="true"
contentType="text/html;charset=ISO-8859-1"  %> 
    
• extends: This is the attributes of the page directive of
the JSP which is used for specifying some other java
classes to be used in your JSP page like
packagename.classname. The fully qualified name of
the superclass of the Java class will be accepted.
• import: This attribute imports the java packages and it's
classes more and more. You can import more than one
java packages and classes by separating with comma
(,). You can set the name of the class with the package
name directly like packagename.classname or import
all classes of the package by using packagename.*.
• buffer: This attribute sets the buffer size in
kilobytes i.e. used by the out object to handle
output generated by the JSP page on the client
web browser. If you specify the buffer size then
the output will be buffered with at least 8kb
because the default and minimum value of the
buffer attribute is 8kb.
• autoFlush: This attribute of the page directive
supports for flushing buffer automatically when
the buffer is full. The value of the autoFlush
attribute is either true or false. If you will specify
the true value then the buffer will be flushed
otherwise it will generate a raised exception if
you set the false value. You cannot set the false
value if the buffer size is none.
• isThreadSafe: This attribute support the facility of
maintaining thread for sending multiple and concurrent
requests from the JSP container to the JSP page if you
specify the true value of the attribute otherwise if you
specify the false value of the attribute then the JSP
container can send only one request at one time. The
default value of the attribute is true.
• info: This attribute simply sets the information of the JSP
page which is retrieved later by using
Servlet.getServletInfo() method. The value of the
attribute will be a text string.
• errorPage: This attribute sets a url (relative path starting
from the "/" which refers from the root directory of your
JSP application). If any exception is generated the the
attribute refers to the file which is mentioned in the given
url. If you do not specify the url then the attribute refers
to the current page of your JSP application if any
exception generated.
• isErrorPage: This attribute sets the boolean value either
true or false. You can use the exception object in the
JSP page if you set the true value of the attribute
otherwise you cannot use the exception object because
the default value of the attribute is false.
• contentType: This attribute specifies the MIME type and
the character encoding i.e. used for the JSP response.
The default MIME type is "text/html" and the default
character set is "ISO-8859-1". You can also specify
other.
• pageEncoding: This attribute specifies the language
that the page uses when the page is sent to the browser.
This attribute works like the meta tag of the HTML
markup language.
• isELIgnored: This is a boolean attribute that specifies
either true or false value. If you set the attribute value is
true then any type of the EL expressions will be ignored
in the JSP page.
JSP SCRIPTLETS
• Syntax of JSP Scriptles are:   <%
  //java codes
   %>
• JSP Scriptlets begins with <% and ends
%> .We can embed any amount of java
code in the JSP Scriptlets. JSP Engine
places these code in the _jspService()
method.
• Variables available to the JSP Scriptlets are:
• request:
request represents the clients request and is a subclass of
HttpServletRequest. Use this variable to retrieve the data submitted
along the request.
Example:
  <%
//java codes
   String userName=null;
   userName=request.getParameter("userName");
   %>

• response:
response is subclass of HttpServletResponse.
 
• session:
session represents the HTTP session object associated with the
request.
 
• out:
out is an object of output stream and is used to send any output to
the client.
• Other variable available to the scriptlets are pageContext,
application,config and exception.
JSP EXPRESSIONS
• Syntax of JSP Expressions are:
•   <%="Any thing"   %>
• JSP Expressions start with 
• Syntax of JSP Scriptles are with <%= and ends with  %>.
Between these this you can put anything and that will
converted to the String and that will be displayed. A
semicolon ( ; ) does not appear at the end of the code
inside the tag.
• Example:
  <%="Hello World!" %>
Above code will display 'Hello World!'.
Request Object In JSP

• All the things like headers, cookies or


parameters are sent from the client browser to
the server during an HTTP request object which
most common use of the request object is to
obtain parameter or query string values.
• Methods of the request object
– request.getParameter(String name):
This is the method, used for getting the value of the
HTML form attribute. This method returns the string
type value
• request.getParameterNames():
This is the method of the request object used
for getting the enumeration of the attributes of
the html form.
• request.getCookies():
This is the method of the request object used for
getting all the cookies existed in the HTTP
request object.
• request.getQueryString():
This is the method of the request object used for
getting the query string which is the values with
the attribute of the html from where the jsp page
is referenced.
• request.getRequestURI():
This is the method of the request object used for
getting the URI of the current page.
• request.getHeaderNames():
This method returns the enumerator of all HTTP
header names means this method retrieves
name of all the headers in enumeration form that
is enumerated and get all the name one by one
by using the Enumeration.nextElement() up to
the last element of the enumeration.
• request.getHeader(String hdr):
This is the method return the value of the HTTP
header like query string or the URL
• request.getAttribute(String):
This method is used for getting the value of the attribute
which is set through the setAttribute(String
attributeName) method of the request object.
• request.getAttributeNames():
Above method is used for retrieving all the attributes
name in the current session of the page. This method
returns the enumerated data which is to be retrieved
later by enumerating.
• request.setAttribute(String, object):
Above method sets the value of the attribute for the
request which is retrieved later either in the current JSP
page or the another JSP page by passing the request
object through the dispatcher method.
Cookie Class
• Cookies are short pieces of data sent by web
servers to the client browser. The cookies are
saved to clients hard disk in the form of small
text file. Cookies helps the web servers to
identify web users, by this way server tracks the
user. Cookies pay very important role in the
session tracking
• In JSP cookie are the object of the class
javax.servlet.http.Cookie.
• This class is used to creates a cookie, a small
amount of information sent by a servlet to a Web
browser, saved by the browser, and later sent
back to the server. A cookie's value can uniquely
identify a client, so cookies are commonly used
for session management. A cookie has a name,
a single value, and optional attributes such as a
comment, path and domain qualifiers, a
maximum age, and a version number.
• The getCookies() method of the request object
returns an array of Cookie objects. Cookies can
be constructed using the following code:
• Cookie(java.lang.String name, java.lang.String
value)

You might also like