![]() |
JSP Advance (Part-II)
In the previous article I explained about the different JSP tags except action tags. In this article I have explained about the JSP implicit objects and JSP action tags in details.
JSP Implicit ObjectsJSP provides a set of implicit objects that can be used to access many server-side objects that represent the incoming request, response, and session objects. These objects are actually defined in the _JSP pageservice() method of the page implementation class and initialized with appropriate references. JSP defines four scopes for the objects that can be used by the JSP authors: Scope --------> Description page ---------> Objects can be accessed only within the JSP page in which they are referenced. request ------> Objects can be accessed within all the pages that serve the current request. These include pages that are forwarded to, and included in, the original JSP page to which the request was routed. session ------> Objects can only be accessed within the JSP pages accessed within the session for which the objects are defined. application --> Application scope objects can be accessed by all JSP pages in a given context. The following relation lists the implicit objects that can be used in scriptlets:- request Object is Protocol dependent sub type of javax.servlet.ServletRequest with request scope - A reference to the current request.response Object is Protocol dependent sub type of javax.servlet.ServletResponse with page scope - The response to the request.pageContext Object is javax.servlet.jsp.PageContext type with page scope - Provides a common point to access the request, response, session, and application, associated with the page being served.session Object is javax.servlet.http.HttpSession type with session scope - The session associated with the current request.application Object is javax.servlet.ServletContext type with application scope - The servlet context to which the page belongs.out Object is javax.servlet.jsp.JspWriter type with page scope - The object that writes to the response output stream.config Object is javax.servlet.ServletConfig type with page scope - The servlet configuration for the current page.Page Object is java.lang.Object type with page scope - An instance of the page implementation class that is serving the request (synonymous with the this keyword if Java is used as the scripting language).exception Object is java.lang.Throwable type with page scope - Available with JSP pages that act as error pages for other JSP pages.The following JSP page illustrates the use of implicit objects in JSP pages. Save this as webapps/jsp/Implicit.jsp:- Code:
<html>http://www.go4expert.com/images/articles/jsp/p2A.jpg JSP ActionsJSP actions are processed during the request processing phase (as opposed to directives, which are processed during the translation phase). The JSP specification defines a few standard actions that must be supported by all compliant web containers. JSP also provides a powerful framework for developing custom actions, which are included in a JSP page using the taglib directive. 1) The jsp:include Action:- The JSP specification defines the include action for including static and dynamic resources in a JSP page. With the include directive the contents of the included resource is substituted into the JSP page at translation phase but with the include action the response of the included resource is added to the current response output stream during the request processing phase. The following code shows how we can use the include action: Code:
<jsp:include page="includedPage.jsp" flush=”true” />Page= The resource to include. Flush= this is optional with the default value of false. If the value is true the buffer in the output Stream is flushed before the inclusion is performed. This action will include the output of processing includedPage.jsp within the output of the JSP page during the request processing phase. There are some important points to note regarding the include action and directive: Changing the content of the included resource used in the include action is automatically reflected the next time the including JSP is accessed. The include directive is normally used for including both dynamic and static resources, the include action is used for including only dynamic resources. Consider the Following example: Webapps\jsp\includeAction.jsp Code:
<html>Code:
<html>Code:
<html>1) The jsp:forward Action:- The JSP specification defines the forward action to be used for forwarding the response to other web application resources. The forward action is the same as forwarding to resources using the RequestDispatcher interface. The following code shows how to use the forward action: Code:
<jsp:forward page="Forwarded.html"/>We can only forward to a resource if content is not committed to the response output stream from the current JSP page. If content is already committed, an IllegalStateException will be thrown. To avoid this we can set a high buffer size for the forwarding JSP page. Forward.html Code:
<html>Code:
html>Code:
<html>Then login with username=Admin and password=Admin http://www.go4expert.com/images/articles/jsp/p2C.jpg http://www.go4expert.com/images/articles/jsp/p2D.jpg 3) The jsp: param Action:- The JSP param action can be used in conjunction with the include and forward actions to pass additional request parameters to the included or forwarded resource. The param tag needs to be embedded in the body of the include or forward tag. The following code shows how we can use the param action: Code:
<jsp:forward page="Param2.jsp">Using JavaBeans with JSP PagesThe JSP specification defines a powerful standard action that helps to separate presentation and content by allowing page authors to interact with JavaBean components that are stored as page, request, session and application attributes. The useBean tag along with the getProperty and setProperty tags allows JSP pages to interact with JavaBean components. 4) The jsp:useBean Action:- The useBean action creates or finds a Java object in a specified scope. The object is also made available in the current JSP page as a scripting variable. The syntax for the useBean action is: Code:
<jsp:useBean id="name" scope="page | request | session | application"First, the container tries to find an object by the specified name in the specified scope. If an object is found, a scripting variable is introduced with the name specified by the id attribute and type specified by the type attribute. If the type attribute is not specified the value of the class attribute is used. The variables value is initialized to the reference to the object that is found. The body of the tag is then ignored. If the object is not found and both class and beanName are not present, an InstantiationException is thrown. If the class defines a non-abstract class with public no-argument constructor an object of that class is instantiated and stored in the specified scope using the specified ID. A scripting variable is introduced with the name specified by the id attribute and type specified by the class attribute. If the value of the class attribute specifies an interface, a non-public class or a class without a public no-argument constructor, an InstantiationException is thrown. The body of useBean of the tag is then executed. If the beanName attribute is used then the java.beans.Bean.instantiate() method is called, passing the value of the attribute beanName. A scripting variable is introduced with the name specified by the id attribute and type specified by the type attribute. The body useBean of the tag is then executed. For example: Code:
<jsp:useBean id="myName" class="java.lang.String" scope="request">5) The jsp:getProperty Action:- The getProperty action can be used in conjunction with the useBean action to get the value of the properties of the bean defined by the useBean action. For example: Code:
<jsp:getProperty name="myBean" property="firstName"/>6) The jsp:setProperty Action:- The setProperty action can be used in conjunction with the useBean action to set the properties of a bean. We can even get the container to populate the bean properties from the request parameters without specifying the property names. In such cases the container will match the bean property names with the request parameter names. The syntax for the setProperty action is shown below: Code:
<jsp:setProperty name="beanName"Code:
<jsp:setProperty name="myBean" property="name" value="harsh"/>Code:
<jsp:setProperty name="myBean" property="name1" param="name2"/>Code:
<jsp:setProperty name="myBean" property="name1"/>Code:
<jsp:setProperty name="myBean" property="*"/>Webapps\jsp\beans.html Code:
<html>Code:
<html>webapps\jsp\com\LanguageBean.class Code:
package com;http://www.go4expert.com/images/articles/jsp/p2F.jpg 6) The jsp: plugin Action:- The plugin action enables the JSP container to render appropriate HTML to initiate the download of the Java plugin and the execution of the specified applet or bean, depending on the browser type. The plugin standard action allows us to embed applets and beans in a browser-neutral manner as the container takes care of the user agent specific issues. For example: Code:
<jsp: plugin type="applet" code="MyApplet.class" codebase="/"> |
Re: JSP Advance (Part-II)
Nominate this article for Article of the month - Apr 2010
|
Re: JSP Advance (Part-II)
Vote for this article for Article of the month - Apr 2010
|
Re: JSP Advance (Part-II)
Interesting article and I think it has covered most of the important intrinsic objects which are definitely required every time. I was quite confused while my time of learning about the "include" tag. Now I got that its actually useful if we want to emulate like master pages in ASP.NET. I am actually an ASP.NET fan and I learned JSP just to know other technology. Small suggestion is that please capture good screen-shots at proper resolution to avoid the blurs and stretching.
|
Re: JSP Advance (Part-II)
Thank you very much for this very informative post. I am a newbie and appreciate all of the great information on this forum. I think this forum is an ideal resource for someone like me who is learning the ropes. This particular thread has answered some questions that I had and I am very grateful. I have lots more to learn, and expect to be digging through the goldmine of information here.
|
Re: JSP Advance (Part-II)
thanks for information in jsp advance
|
Re: JSP Advance (Part-II)
Before reading the thread the topic of JSP seemed something horrable :crazy: for me, but now i see that it's not so complicated as it seemed !! Thanks
|
| All times are GMT +5.5. The time now is 03:28. |