Você está na página 1de 9

You can write java code in your JSP but should you?

And if you dont write java code ,then what do you write?
How does it translate into Java code?
Six different kinds of JSP elements each with its own purpose and unique syntax.
How ,why and what to write in your JSP?
What not to write in your JSP.
JSP is just a servlet : MyJSP.jsp
MyJSP_jsp servlet

MyJSP_jsp.java MyJSP_jsp.class

Where does each part of your jsp file end up in the servlet source code?
Do you have access to the servletness of your JSP page?
For example does your JSP have the concept of ServletConfig or ServletContext?
What are the types of elements you can put in a JSP?
Whats the syntax of the different elements in a JSP?
Whats the lifecycle of a JSP and can you step into the middle of it?
How do the different elements in JSP interact in the final servlet?
What are scriptlets?
Using scriptlets you can put regular, old java code in a jsp
Scriptlet : <% out.println(Counter.getCount()); %>
What are directives?
Use fully qualified class name or use page directive to import packages.
<%@ page import =foo.* ,java.util.* %.>

What are directives?


A directive is a way for you to give special instruction to the container at page translation
time.
3 types of directives in JSP spec : page ,include,taglib
Directives have attributes.Eg import attribute of page directive.

What are expressions?


Expressions help avoid out.println.
<%= Counter.getCount() %>
Why is there no semicolon at the end of Counter.getCount()?
Expressions become an argument to an out.println();
When the container sees this :
<%= Counter.getCount() %>
It turns it into this:
<% out.println(Counter.getCount());%>
If you are supposed to use expressions instead of out.println();,then why is there an
implicit out object?
You might pass out to some other object thats part of your app that does not have direct
access to the output stream for the response.
In an expression,what happens if the method does not return anything?
You cannot use a method with a void return type as an expression.

After scriptlets,expressions,directives,the other important element in JSP is declarations.


What are declarations for?
JSP declarations are for declaring members of the generated servlet class.That means
both methods and variables.That means anything inside<%! %> is added to the class
outside the service method.That means you can declare both static variables and methods.
Everything you write in your scriptlets and expressions goes inside service method.

What the container does with your JSP?


Looks at directives for information it might need during translation.

Creates a HttpServlet subclass

Implicit Objects
With implicit objects you can write a JSP knowing that your code is going to be part of
servlet
All of the implicit objects map to something in the Servlet/JSP API.
E.g request object is a reference to the HttpServletRequest object passed to service
method by the container
Implicit objects
API

Implicit Objects

JspWriter
HttpServletRequest
HttpServletresponse
HttpSession
ServletContext
ServletConfig
Throwable
PageContext
Object

out
request
response
session
application
config
exception
pagecontext
page

Diff between JspWriter and PrintWriter


JspWriter is not in the class hierarchy of PrintWriter,so you cant use it in place of
PrintWriter.
It has most of the same print methods except it adds buffering capabilities.

Comments
//this is a comment
Unless this is within a scriptlet or declaration tag ,youll end up displaying this to the
client as part of response.
The container those 2 tags are template text.
2 different types of comments ins JSP :
i.HTML comment : <!-- HTML comment -->
the container passes this straight on to the client where the browser interprets it as a
comment
these are comments that stay as part of response
ii.JSP Comment : <%-- JSP Comment --%>
these are for page developers like java comments in java source file
theryre stripped out of translated page
these are comments about wat u r doing.

API for the generated servlet :


The container generates a class from ur JSP that implements the HttpJspPage interface

jspInit()

called from init() of generated servlet


jspDestroy()
called from destroy() of gen servlet
_jspService()
_ signifies that it Cant be overridden
JSPs life cycle
1.the container reads the dd but does not do anything with the jsp until the first time it is
requested
2.the translation of jsp and compilation of generated java file happens only once on first
request.
3.but even this can be done before first request,but that is container vendor dependent
How do you configure init parameters for JSP and how do you retrieve it in a JSP?
Configure :
<servlet>
<servlet-name>MyTestInit</servlet-name>
<jsp-file>/TestInit.jsp</jsp-file>
<init-param>
<param-name>email</param-name>
<param-value>alok.chandna@gmail.com</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyTestInit</servlet-name>
<url-pattern>/TestInit.jsp</url-pattern>
</servlet-mapping>
Use in JSP :
Override jspInit() in JSP using scriptlet,this is called by container at the beginning of this
pages life as a servlet.

It is called from init() of generated servlet,so by the time this method runs there is a
ServletConfig and ServletContext available to the servlet.
So u can call getServletConfig() and getServletContext() from within the jspInit()
method.

What are the 4 implicit objects used in a JSP to get and set attributes?
application,request,session,pagecontext
all 4 have on emethod for setting attribute
application.setAttribute(foo,barObj);
But what is the other way to get and set attributes in a JSP at any scope?
Using the pageContext implicit object

The methods that work with other scopes take an int argument to indicate the scope.
Although the attribute access methods come from JspContext,youll find the constants for
the scopes in the PageContext class.

The 3 directives
1. Page directive : defines page specific properties
<%@ page import=foo.* session=false %>
a. character encoding for page
b. content type for this pages response.
c. Whether this page should have an implicit session object

d. page directive can have 13 attributes oly 4 are there exam


2. Taglib directive : tag libraries available to this jsp
<%@ taglib tagdir=/WEB-INF/tags/cool,prefix=cool %>
3. Include directive : defines text and code thats gets added to the
current page at translation time.
<%@ include file=wickedHeader.html %>
a. Lets u build reusable chunks (page heading or avigation bar)
that can be added to each page.

Use EL instead of scriptlets, expressions or


declarations
1. Page designers vs coders : dreamweaver and adobe photoshop
2. EL : ${something}
This EL expression :
Please contact : ${applicationScope.mail}
Is the same as :
Please contact : <%= application.getAttribute(mail) %>

Disabling scripting elements


<web-app>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
<jsp-config>

Ignoring EL
Why ignore : same reason as assert keyword was added with java 5
So previously if there was ${} then it wud give problem
1. Using DD
<web-app>

<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</ el-ignored >
</jsp-property-group>
<jsp-config>
2. Using page directive
<%@ page isElIgnored=true %>

Another jsp element


Actions
1. So far 5 different JSP elements :
scriptlets,expressions,directives,declarations,EL
2. 2 types of actions : standard and not
Standard Action:
<jsp:include page=wickedFooter.jsp />
Other Action
<c:set var =rate value=32 />

Você também pode gostar