Saturday, July 24, 2010

JSP Learnings

Hi,

JSP :

JSP is a way of developing the UI where we can embed Java Code in HTML and make use of beans

To write Java Code, We need to embed the code inside <% and %> which we call them as scriptlets

<%@ -- page directive
used to import files, include pages
e.g.,<%@page language="java" import="java.sql.*,mypackage.myclass" %> ,
<%@ include file="/header.jsp" %> ,
<%@ taglib uri="tlds/taglib.tld" prefix="mytag" %>

<%! -- delcaration
used to declare some Java Methods at the start of jsp

To write HTML Code, We need to output using out.println("test") or we can use <%=<variablename< which internally converts that into out.println and prints output

If you want to use a Java Bean inside Jsp use jsp:useBean

<jsp:useBean id="user" class="user.UserData" scope="session"/>

setProperty sets all the properties value taken from the form directly into the bean id provided. Here it is "user" bean
The only condition for this to happen automatically is, the getters and setters defined in java bean file must be same as fieldName
For example: if there is field firstName then the bean should have setFirstName and getFirstName

<jsp:setProperty name="user" property="*"/>

summary of jsp tags :

jsp:include
The jsp:include action work as a subroutine, the Java servlet temporarily passes the request and response to the specified JSP/Servlet. Control is then returned back to the current JSP page.

jsp:param
The jsp:param action is used to add the specific parameter to current request. The jsp:param tag can be used inside a jsp:include, jsp:forward or jsp:params block.

jsp:forward
The jsp:forward tag is used to hand off the request and response to another JSP or servlet. In this case the request never return to the calling JSP page.

jsp:plugin
In older versions of Netscape Navigator and Internet Explorer; different tags is used to embed applet. The jsp:plugin tag actually generates the appropriate HTML code the embed the Applets correctly.

jsp:fallback
The jsp:fallback tag is used to specify the message to be shown on the browser if applets is not supported by browser.
Example:
<jsp:fallback>
<p>Unable to load applet</p>
</jsp:fallback>

jsp:getProperty
The jsp:getProperty is used to get specified property from the JavaBean object.

jsp:setProperty
The jsp:setProperty tag is used to set a property in the JavaBean object.

jsp:useBean
The jsp:useBean tag is used to instantiate an object of Java Bean or it can re-use existing java bean object.


A Ram Prasad

No comments:

Post a Comment