Posts

jsp:setProperty Tag in JSP

Image
JSP jsp:getProperty Tag The getProperty tag is used to retrieve a property from a JavaBeans instance. The syntax of the getProperty tag is as follows: < jsp: getProperty name = " beanName " property = " propertyName " /> The name attribute represents the name of the JavaBean instance. The property attribute represents the property of the JavaBean whose value we want to get. Example of getProperty with Java Bean Following is our Java class. PersonBean.java import java . io . Serializable ; public class PersonBean implements Serializable { private String name ; public PersonBean ( ) { this . name = "" ; } public void setName ( String name ) { this . name = name ; } public String getName ( ) { return name ; } }   hello.jsp < html > < head > < title > Welcome Page </ title > </ head > < jsp: useBean id

jsp:getProperty Tag in JSP

Image
JSP jsp:getProperty Tag The getProperty tag is used to retrieve a property from a JavaBeans instance. The syntax of the getProperty tag is as follows: < jsp: getProperty name = " beanName " property = " propertyName " />   The name attribute represents the name of the JavaBean instance. The property attribute represents the property of the JavaBean whose value we want to get. Example of getProperty with Java Bean Following is our Java class. PersonBean.java import java . io . Serializable ; public class PersonBean implements Serializable { private String name ; public PersonBean ( ) { this . name = "" ; } public void setName ( String name ) { this . name = name ; } public String getName ( ) { return name ; } }   hello.jsp < html > < head > < title > Welcome Page </ title > </ head > < jsp: useBean

jsp:useBean tag in JSP

Image
JSP jsp:useBean Tag If you want to interact with a JavaBeans component using the Action tag in a JSP page, you must first declare a bean. The <jsp:useBean> is a way of declaring and initializing the actual bean object. By bean we mean JavaBean component object. Syntax of <jsp:useBean> tag <jsp:useBean id = "beanName" class = "className" scope = "page | request | session | application">     Here the id attribute specifies the name of the bean. Scope attribute specify where the bean is stored. The class attribute specify the fully qualified classname. Given a useBean declaration of following : < jsp: useBean id = " myBean " class = " PersonBean " scope = " request " /> is equivalent to the following java code, PersonBean myBean = ( PersonBean ) request . getAttribute ( "myBean" ) ; if ( myBean == null ) { myBean = new PersonBean ( ) ;

Java Bean Components

JSP JavaBean Components A JavaBeans component is a Java class with the following features: A no-argument constructor. Properties defined with accessors and mutators(getter and setter method). Class must not define any public instance variables. The class must implement the java.io.Serializable interface. Example of a JavaBean Let's take a simple Java code example to understand what do we mean when we say JavaBean, import java . io . Serializable ; public class StudentBean implements Serializable { private String name ; private int roll ; // constructor public StudentBean ( ) { this . name = "" ; this . roll = "" ; } // getters and setters public void setName ( String name ) { this . name = name ; } public String getName ( ) { return name ; } public int getRoll ( ) { return roll ; } public void setRoll ( int roll ) { this . roll = roll

JSP Action Element

SP Standard Tag(Action Element) JSP specification provides Standard (Action) tags for use within your JSP pages. These tags are used to remove or eliminate scriptlet code from your JSP page because scriplet code are technically not recommended nowadays. It's considered to be bad practice to put java code directly inside your JSP page. Standard tags begin with the jsp: prefix. There are many JSP Standard Action tag which are used to perform some specific task. The following are some JSP Standard Action Tags available: Action Tag Description jsp:forward forward the request to a new page Usage : <jsp:forward page="Relative URL" /> jsp:useBean instantiates a JavaBean Usage : <jsp:useBean id="beanId" /> jsp:getProperty retrieves a property from a JavaBean instance. Usage : < jsp : useBean id = "beanId" . . . / > . . . < jsp : getProperty name = "beanId" property = "someProperty"

Exception Handling in JSP

Image
JSP Exception Handling Exception Handling is a process of handling exceptional condition that might occur in your application. Exception Handling in JSP is much easier than Java Technology exception handling. Although JSP Technology also uses the same exception class objects. It is quite obvious that you dont want to show error stack trace to any random user surfing your website. You can't prevent all errors in your application but you can atleast give a user friendly error response page. Ways to perform Exception Handling in JSP JSP provide 3 different ways to perform exception handling: Using isErrorPage and errorPage attribute of page directive. Using <error-page> tag in Deployment Descriptor . Using simple try...catch block. Example of isErrorPage and errorPage attribute isErrorPage attribute in page directive officially appoints a JSP page as an error page. error.jsp errorPage attribute in a page directive informs the Web Container

JSP Taglib Directive

Image
JSP Taglib Directive The taglib directive is used to define tag library that the current JSP page uses. A JSP page might include several tag library. JavaServer Pages Standard Tag Library (JSTL), is a collection of useful JSP tags, which provides mahy commonly used core functionalities. It has support for many general, structural tasks such as iteration and conditionals, readymade tags for manipulating XML documents, internationalization tags, and for performing SQL operations. Syntax of taglib directive is: < %@ taglib prefix = " prefixOfTag " uri = " uriOfTagLibrary " % >   The prefix is used to distinguish the custom tag from other libary custom tag. Prefix is prepended to the custom tag name. Every custom tag must have a prefix. The URI is the unique name for Tag Library. You can name the prefix anything, but it should be unique. JSP: Using Taglib Directive To use the JSTL in your application you must have the jstl.jar in