JSP
FUNDAMENTALS

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.
Lets start with the basic of the JSPs, JSP is flavour of Cold
fusion and ASP and hence provide the flexibility to embed the business
logic efficiently within the HTML content (presentation logic).
JSP page is built using
components such as :
- Directives
- Declarations
- Scriplets
- Expressions
- Standard
Actions
- Custom
Tags
Directives
:
Listing some of them to start off :
1)
Page
Syntax : <
%@ page
Language=”Java” extends=”<Class name>”
import=”<class> or <package>” %>
Attributes:
a.
Language = “Java”
b.
Import = “Class”
c.
Buffersize = “”
d.
Scope = “REQUEST/PAGE/SESSION/APPLICATION”
e.
And etc….
Page
directive is aimed to define certain attribute of a JSP page for e.g.
Language of the page in which the page content should be written , which
class to be imported so that it can be used within the JSP page.
2)
Include
Syntax: <%@ include
file=”<filename>” %>
Attributes:
a.
file = “<filename>”
This
directive is to include the a HTML, JSP or Sevlet file into a JSP file.
This is a static inclusion of file i.e. it will be included into the JSP
file at the time of compilation and once the JSP file is compiled any
changes in the included the file will not the reflected.
Declarations:
Syntax: <%!
Declare
all the variables here %>
Scriplets:
Syntax: <%
All your scripts will come here %>
Expressions:
Syntax: <%=
expression evaluation and display the variable %>
Standard Action:
Syntax:
Include <jsp:include
page =”<filename>” />
This inclusion of
file is dynamic and the specified file is included in the JSP file at
run-time i.e. out put of the included file is inserted into the JSP
file.
Forward <jsp:forward
page=”<filename>” />
This will redirect to the different page without notifying
browser.
And many more.
Custom Tags:
taglib
Syntax:
<%@
taglib uri=”<tag library uri>” prefix=”<tagprefix>”
%>
Attributes:
a.
uri = “<relative path of the tag library uri>”
b. prefix = “<tagprefix>”
prefix
is alias name for the tag library name.
JSP
provides
certain Implicit Objects listed below.
| Object |
Of
Kind |
| Out |
JSP
writer |
| Request |
HttpServletRequest |
| Response |
HttpServletRespose |
| Session |
HttpSession |
| Application |
ServletContext |
| Config |
Sevlet
Config |
| Page |
Object |
| PageContext |
Page
Context => is responsible for generating all other implicit
objects. |
Out:
This
object is instantiated implicitly from JSP Writer class and can be used
for displaying anything within delimiters.
For
e.g. out.println(“Hi Buddy”);
Request:
It
is also an implicit object of class HttpServletRequest class and using
this object request parameters can be accessed.
For
e.g. in case of retrieval of parameters from last form is as follows:
request.getParameters(“Name”);
Where
“Name” is the form element.
Response:
It
is also an implicit object of class HttpServletResponse class and using
this object response(sent back to browser) parameters can be modified or
set.
For
e.g. in case of modifying the HTTP headers you can use this object.
Response.setBufferSize(“50”);
Session:
Session
object is of class HttpSession and used to maintain the session
information. It stores the information in Name-Value pair.
For
e.g.
session.setValue(“Name”,”Jakes”);
session.setValue(“Age”,”22”);
Application:
This
object belongs to class SevletContext and used to maintain certain
information throughout the scope of Application.
For
e.g.
Application.setValue(“servername”,”www.myserver.com”);
PageContext:
This
object is of class pageContext and is utilized to access the other
implicit objects.
Connection Pooling
|
<%@
page language="Java"
import="javax.naming.*,javax.sql.*,java.sql.*,java.util.*"%>
<jsp:useBean
id="empEli"
scope = "page"
class ="Elitraining.Employee"
/>
<jsp:setProperty name ="empEli"
property = "*"
/>
<%
try {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL,"t3://ramses:7001");
InitialContext ic = new InitialContext(p);
DataSource ds = (DataSource)ic.lookup("demoPool");
Connection con = ds.getConnection();
Statement stmt = con.createStatement();
int i = stmt.executeUpdate(
"insert
into employee values('" + empEli.getFirstName() +
"','" + empEli.getLastName() + "')" );
}
catch(Exception ex) {
ex.printStackTrace();
}
%>
<html>
<body>
Employee
Firstname is : <jsp:getProperty name="empEli"
property ="firstName" />
<BR>
Employee
Lastname is : <jsp:getProperty name="empEli"
property ="lastName" />
</body>
</html>
|
Here
we are using the property feature to store all the attributes
required to use the connection pool like INITIAL_CONTEXT_FACTORY,
PROVIDER_URL.
DataSource
ds = (DataSource)ic.lookup("demoPool");
This
statement looks up for the connection pool already configured in the
particular Application Server. Here the name of the connection pool is
named as demoPool.
Connection
con = ds.getConnection();
This
statement gets hold of an connection to the database from the pool of
database connections already existing with the application server.
The
rest of the statements are standard JDBC statements used in a normal
connection to the database.

|
Current Comments
19 comments so far (post your own) View All Comments Latest 10 Comments:Hi,
I am Manoj undertraining in software job my platform is JAVA plz send more information on JSp
Posted by manoj on Monday, 04.28.08 @ 15:58pm | #58057
Can we learn whole of Java(Core & Advance) by a self study
Posted by prashanthi on Tuesday, 03.4.08 @ 17:49pm | #51306
Hello frends i am fresher and beginer. can guide me how can i got job in java platform. i have learn core java, jsp, servlet only. is it enough for entry. please help me.
Posted by Hiren Patel on Monday, 02.18.08 @ 15:03pm | #48876
The example for connection pooling
jsp:getProperty and jsp:setProperty
how to go whit connection pooling is not mentioned..............some more examples are needed to practice.....retriving, updating,etc....dnamically from database...thankyou if anyone helps
Posted by kiron kumar on Monday, 02.11.08 @ 18:56pm | #47914
hai
i see this website on 1st time its vry useful 2 me. Then u should include in update/new technologies are used in java,jsp,j2ee,j2se,j2me plz sent my id this vry useful 4 my feature carrier.
Posted by shivani on Thursday, 01.31.08 @ 15:03pm | #46715
Dear Sir,
Examples are nice but please mention some more examples of database connectivity specially how to retrieve the values from table by using checkbox and radiobuttons.
Thanks
Kumaril Mishra
Posted by Kumaril Mishra on Wednesday, 01.30.08 @ 14:48pm | #46588
What is computer?
Posted by Nirmal Singh on Thursday, 01.17.08 @ 11:15am | #45408
its good but more description has to be included
Posted by trish on Thursday, 12.6.07 @ 11:20am | #41417
All the definition are superb. But we need Some more jsp programs
Posted by Senthamil Bharathi on Tuesday, 09.25.07 @ 18:36pm | #29327
hi , Need some more Detailed information abt JSP
Posted by priya on Thursday, 09.20.07 @ 12:41pm | #28181