Introduction : |
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….
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 =”” />
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=”” />
This will redirect to the different page without notifying browser.
And many more.
And many more.
Custom Tags:
taglib
Syntax: < %@ taglib uri=”” prefix=”” %>
Attributes:
a. uri = “< relative path of the tag library uri >”
b. prefix = “< tagprefix>”
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”);
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" /> < /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.
No Response to "JSP FUNDAMENTALS"
Post a Comment