Wednesday, February 6, 2013

Head First Servlets and JSP: Chapter 8 Scriptless JSP

in this chapter we will talk how you can right standard action with no scripting inside JSP pages.

for example to read an attribute in JSP page you may write:

as you can see we cand use

the id="person" is basically the request.setAttribute("person", p). and also it is the bean id

<jsp:getProperty>: we use it to get a bean property value,

important: if there was no attribute for "person", jsp:useBean will create an empty bean.

and also you can set a proerty
<jsp:setProperty name="person" property="name" value="Fred" />

you can do this:

as you can see, jsp:setProperty is inside jsp:useBean, which means if the bean wasnt set (the attribute "person" wasnt there) do a setProperty

jsp:useBean and Polymorphism 
lets say you have
abstract class person {}
public class Employee extends Person{}.

if you write
<jsp:useBean id="person" class="foo.Person" scope="request"/>
you will get an exception, because Person is an abstract class.

in order to do a polymophic useBean

<jsp:useBean id ="person" type="foo.Preson" class="foo.Employee" scope="request"/>
as you can see we use type and class

Send Requests Directly to JSP page:

the request now will go directly to the jsp page not to the servlet.
in the JSP page you can read the inputText property:

as you can see we are using script to read the "userName"

in order to write the code above scriptless:

as you can see we are using param attribute

Standard Action and Objects
as you can see in the previous examples, you can read string and integer attributes, however how can we read Objects attribute.

lets say we have a servlet and the servlet adds this attribute:

we added a person attribute, inside the person attribute we have a dog

now, if you want to get the dog name for example you can write this in JSP

however if you want to do it without scripting:

the code above will return the dog object not its name, and you cannot write ""

standard actions with no scripting is good for string and integer attribute. however in such a case you should use Expression Language

as you can see ${} which means person.getDog.getName().

EL dot (.) operator
the EL we saw with . operator works for objects and
for example when you write ${}

if person is a bean, this will be translated to person.getDog().
if person is a, this will return person.getValue("dog") which means returns the value for the key "dog"

EL [] operator 
you can use [] with EL, for example ${person["dog"]}. however this could be used for multiple purposes

1- if person is a bean, it will be translated to person.getDog()
2- if person is a map, it will return the value for the key "dog"
3- if person is an array, and you write ${person[0]} it will return the first element
4- if person is a List, and you write ${person[0]} it will return also the first element

EL implicit objects
there are some implicit objects that you can use in your EL code
these objects are

for example you can get a request parameter like this

you can read header information

Use your function with EL
you can write your function and use it in EL
1- define a class with static method
2- define a Tag Library Description
3- import the TLD
4- use the function

Master Pages and Include

1- so as we mentioned before you can define a master page and include it in other pages,
2- the master page should be without <html><body> tags

you can include the page by

<%@ include file="Header.jsp" %>: this will behave like copying what you have in Header.jsp to the current page. (if the container is not new, it may not noticed the changes in Header.jsp and you have to restart the app, however we dont have this problem with Tomcat)

<jsp:include page="Header.jsp"/>: this actually will add dynamic code which will create a request to header.jsp each time ( alot of overhead ).

you can pass parameter to the included page like this

and use the attribute in the included page:

<strong> ${param.subTitle} </strong>

you can use <jsp:forward> to forward your request, for example if the user is not logged in do a forward

very important to know that when <jsp:forward> executes it clears the buffer, which means all the rendered stuff before <jsp:forward> will be cleared.

No comments:

Post a Comment