JSP Intro
JSP Intro
• 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.
•
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
• <%!
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