All Jsp Elements
What is JSP ?
------------------
Jsp is nothing but a servlet......,Container translates it into a
servlet class source(.java),then compiles that generated java file into
Java Servlet class.
If you want to generate html from Servlet it will be very difficult...
Servlet Code to generate form tag:
Jsp code to generate form tag:
You can see.. in servlet whenever you find double quote you have to prepend
double quote with backslash which will be very tedious processs.
What ever you write in jsp will go to respective places in the translated servlet...
source code(.java file)
Ex:
----
Following line in jsp will be converted to response.getWriter().println("<form name=\"helloform\" method=\"post\" "); in translated servlet source code.
<form name="helloform" method="post">
At the end jsp is nothing but a servlet.... all the jsp content will be mapped to
servlet source.When you request the jsp for the first time this translation
and compilation will happen.. from then onward there is no difference between
jsp and servlet..
Let us start with various jsp elements:
--------------------------------------------
1)Directive element.Directive element starts with <%@.There are different
types of page directives.
a)page directive(page directives will have many attributes over 10 attributes,
one of them import I have covered below Eg:isThreadSafe,ContentType,isErrorPage,errorPage,pageEncodeing etc.......)
A class will be normally imported in java file like following
import java.util.ArrayList.
If you want to import class in jsp
<%@ page import="java.util.ArrayList" %>
If you want to import multiple classes in jsp,seperate classes with comma
<%@ page import="java.util.ArrayList,java.sql.Connection" %>
b)taglib directive
Eg:<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
We will cover this during JSTL.
c)include directive
If there is some common content that needs to be include in several jsp
pages.. Add the common content like company logo in one jsp page
and include it in all the pages where you want.. So that when there is
a change in company logo image instead of modifying it over 100 pages
just you will change at one place.
<%@ include file="header.jsp" %>
----------------------------------------------------------------------------------------------
2)Scriptlet:Scriptlet looks like below..All the normal java code can be placed
in scriptlet..Writing scriptlets is very easy to perform java related logic
but scriptlets should not be used much because it will become difficult to
maintain jsp pages with lot of scriptlets....... mixed with html code..You have
to use jstl as much as possible..
<%
out.println("Hello world scriptlet");
%>
3)Expresssion tag.
<%= "Hello world scriptlet" %>
Expression tags are very easy to output strings.. no need to write out.println(
as in scriptlet.. What ever is there between <%= %>("Hello world scriptlet")
will become argument to out.println method. Container during translation phase
does this conversion for us..
4)Action elements(Standard Actions)
<jsp:include page="wickedFooter.jsp" />
<%@ include file and <jsp:include seems to be same but there is
differnce..
1)<%@ include directive happens at translation time
where as <jsp:include happens at runtime.
2)When to use include directive and when to use <jsp:include.
when you think that your code in the header.jsp wont' change,
use include directive.Because at translation time the content in
the header.jsp gets into included page and then after changes
in header.jsp won't get reflected.
In jsp:include, changes in header.jsp gets always reflected.
b)jsp:useBean
c)jsp:setProperty
d)jsp:getProperty
d)jsp:include
e)jsp:forward
I will try to cover examples on all these........ in coming posts.........
No comments:
Post a Comment