A JSP program contains set of JSP elements. The following are
the elements of JSP. They are given below:
1.
Template
Text
2.
Script
let
3.
JSP
Expressions
4.
JSP
Declarations
5.
JSP
Directions
6.
JSP
Action Tags
7.
JSP
Custom Tags
8.
EL
Expressions(Expression Language)
*Template Text:
This Element is used to send output to the client. We can
write the Template TextElement.directly in the JSP without any special symbols.
Ex: First
Line //TemplateText Line1
Second Line //TemplateText Line2
When we use Template Text the content will be placed inside
out.Print() method. When we run the above JSP we got the following servlet.
public class Final one_jsp extends
HttpJSPBase{
public void –JSPService(…….){
out.Println(“First Line”);
out.Println(“Second Line”);
}
}
The following is an example of sending a form to the client
by using JSP’s.
<form>
User
Name:<input type = “text” name = “uname”/><br>
Password:<input
type = “Password” name = “pwd”/><br>
<input
type = “submit” value = “Login”/>
</form>
//One.jsp
Scriplets:
Scripts are
used to write java code in JSP’s. The following is the syntax of Scriplet.
<% //start
scriplet
Java code
%> //ending
script
Ex:
<%
int
a = 10;
System.out.println(a);
%>
//One.JSP
When the JSP complier encounter a Scriplet directly placed
inside –JSPService() method.
int a = 10;
System.out.println(a);
%>
JSP compiler follows set of rules in converting JSP program
into corresponding Servlet program.
Implicit
variables:
In
JSP’s we can use some variables without we declaring it. The variables which
can be used in JSP’s without declaring we call them as implicit variables or
implicit objects. The following are of implicit variables available in the JSP.
1.
request
2.
response
3.
pageContext
4.
session
5.
application
6.
config
7.
out
8.
page
9.
Exception
<%
response.setContextType(“text/xml”);
out.println(“<student>”);
out.println(“<sno>|</sno>”);
out.println(“</student>”);
%>
We can send the error message directly to the client.
<%
O
response.sendError(543,”abc”);
%>
Request:
Request implicit
variable can be used directly in the JSP script without we declaring it.
request -----------------à HttpServletRequest
<%
out.println(request.getMethod()+”<br>”);
out.println(request.getRequestURI()+”<br>”);
out.println(request.getProtocol()+”<br>”);
out.println(request.getHeader(“User
Agent”)+”<br>”);
out.println(request.getHeader(“Accept-language”)+”<br>”);
%>
Config: Config variable is belongs to
servletConfig data type. By using servletConfig we can remove the hard coding.
Config----à servletConfig
<%
String drv =
config.getInitParameter(“drv”);
out.print(drv);
%>
servletConfig is an object used by servlet container to pass
the information during initialization.
Once if we get the ServletConfig object, we can find the name
of the servlet which is configured in web.xml file by using a method
getServletName() method.
Ex:
<%
String
name = config.getServletName();
oput.println(name);
%>
As part of the url pattern if it end with .jsp, jsp compiler
directly processes the jsp program.
1.
Application
implicit variable can be used directly in the jsp.
2.
ServletContext
object contains set of methods which are used to interact with
ServletContainer. By using these methods we can find the information about
ServletContainer.
3.
When
ever sun micro system releases a new version of API. It is the responsibility
of server vender to provide the implementation to the latest API. For example
when servlet 2.5 is released Tomcat guys has provided the implementation and
integrated it in Tomcat 6.0 the latest version of servlet API is 3.0 this version
is integrated in Tomcat 7.0.
Session:
We can use
session implicit variable directly in the jsp. Session implicit variable is
holding Http Session object.
Session ----à HttpSession
We can use
page implicit variable directly in the JSP.
Page -----à Object
Page implicit variable hold the currently executes Servlet
object for the corresponding JSP.
Exception:
The implicit
variable can be used only in Error pages. When we try to use this variable in a
JSP which is not an Error page we get an Error message.
Exception
-----à
Throwable
PageContext:
PageContext ---à PageContext
1.
Whenever
-jspService() method is Executing it creates PageContext object. Whenever
–jspService() method Execution is completed it removes PageContext object.
2.
By
using PageContext implicit variable we can get any other implicit variable
object.
Ex: application = PageContext.getServletContext();
In the jsp program also we can use comments.
i.
We
can follow HTML style of comments in jsp.
Ex: <!-
- -
<%
out.println(“we
are in scriplet”);
%>
---
>
ii.
jsp
style of writing comments.
<% ----
code
----%>
JSP Declarations:
JSP
declarations are used to create instance variables, instance methods and static
variables and static methods.
Syntax:
<%!
// instance
variables
// static
variables
// instance
methods
// static
methods
%>
When we run the JSP program which is having JSP declarations
the JSP compiler directly place the contents inside the class.
Ex:
<%!
int a;
public void method(){
}
%> // One.jsp (JSPC)
public final
class one_jsp Extends HttpJSPBase{
int a;
public void
method(){
}
public void
_JSPService(------){
// implicit
variables }
}
// One.servlet
If we write the JSP declaration with set of methods we need
to call the methods from _JSPService() method.
<%!
public void methodOne(){
System.out.println(“we
are in methodOne()”);
}
%>
<%
methodOne();
%>
The implicit variables of JSP can’t be accusable in JSP
declarations.
Ex:
<%!
Public void methodOne(){
out.println(“we are in
methodOne()”);
}
%>
According to sun micro system the implicit variables can’t be
accusable in JSP declarations. This is because the local variables of one()
method can’t be accusable in another method.
As a java programmer we can follow our own techniques to use
the local variables of one() method in another method.
Ex:
<%!
JSPWriter
out;
public
void methodOne(){
out.println(“we
are in methodOne()”);
}
%>
<%
this.out = out;
methodOne();
%>
The
following are three life cycle methods of JSP.
1. JSPInit()
2. _jspService()
3. jspDestroy()
Ex:
<%
public void JSPInit(){
System.out.println(“we are
in JSPInit()”);
}
public void
jspDestroy(){
System.out.println(“we
are in jspDestroy()”);
}
%>
<%
System.out.println(“we
are in _jspService()”);
%>
When the corresponding servlet object for the jsp is created,
server call JSPInit() method. Every time the client send the request it Execute
_jspService() method. When the project is un-deploy server Executes
jspDestroy() method.
In case of jsp when ever we modify the jsp and send the
request. The Existing servlet object will be removed and create new servlet
object.
When the server create servlet object for a corresponding jsp
program?
Scenario:
1.
When
the client send the request to jsp for the 1st time server create
the 1st servlet object and call the life cycle method.
2.
When
we modify the jsp and when the client send the request to the server if already
servlet object is available it removes the old servlet object and creates new
servlet object [it will call jspDestroy()].
JSP Expressions: jsp Expressions simplifies the use
of java Expressions in jsp.
Getting the value from variable and display to the client is
called as java Expressions.
Ex:
<%
int
a = 10;
out.println(a);
%>
Performing arithmetic, relational, logical operations on a
variable one also called as java Expression.
Ex:
<%
int
a = 10;
int
b = 20;
out.println(a+b);
%>
In a project we use some supporting classes every supporting
class must be placed inside a package other wise server will not recognize the
supporting classes.
Displaying an object to the client is also considered as java
Expressions.
Calling the methods an object and send the output in the client
is also called as java Expressions.
Ex:
<%
Infodreamsoft.Emp
e = new infodreamsoft.Emp();
out.println(e); or (e.toString()); or (e.Eno());
%>
<% = javaExpressions
%>
The following is an Example of jsp Expressions.
<%
int
a = 10;
int b = 20;
%>
<% = a +
b %>
or <% = a%>
The following is the code how jsp compiler convert jsp
Expressions.
JSPC
Out.println(a + b);
<%
info.ashok.Emp e = new info.ashok.Emp();
e.setEno(“1”);
e.setEName(“Raju”);
%>
<% =
e%>
<% =
e.getEno() %><br>
0 comments:
Post a Comment