Posts

Showing posts from February, 2020

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

Include Directive in JSP

Image
JSP Include Directive The include directive tells the Web Container to copy everything in the included file and paste it into current JSP file. Syntax of include directive is: < %@ include file = " filename.jsp " % > Example of include directive welcome.jsp < html > < head > < title > Welcome Page </ title > </ head > < body > < %@ include file = " header.jsp " % > Welcome, User </ body > </ html >     header.jsp < html > < body > <img src="header.jpg" alt="This is Header image" / > </ body > </ html > The example above is showcasing a very standard practice. Whenever we are building a web application, with webpages, all of which have the top navbar and bottom footer same. We make them as separate jsp files and include them using the include directive in

Implicit Objects in JSP

Image
Implicit Objects in JSP JSP provide access to some implicit object which represent some commonly used objects for servlets that JSP page developers might need to use. For example you can retrieve HTML form parameter data by using request variable, which represent the HttpServletRequest object. Following are the JSP implicit object   Object Description  request The HttpServletRequest object associated with the request response The HttpServletRequest object associated with the response that is sent back to the browser. out The JspWriter object associated with the output stream of the response. session The HttpSession object associated with the session for the given user of request. application The ServletContext object for the web application. config  The ServletConfig object associated with the servlet for current  JSP page. pageContext The PageContext object that encapsulates the enviroment of a single request for this current JSP page pag

Expression Tag in JSP

JSP Expression Tag Expression Tag is used to print out java language expression that is put between the tags. An expression tag can hold any java language expression that can be used as an argument to the out.print() method. Syntax of Expression Tag <%= Java Expression %>   When the Container sees this <%= (2*5) %>   It turns it into this: out . print ( ( 2 * 5 ) ) ;   Note: Never end an expression with semicolon inside Expression Tag. Like this: <%= (2*5); %> Example of Expression Tag < html > < head > < title > My First JSP Page </ title > </ head > <% int count = 0; %> < body > Page Count is <%= ++count %> </ body > </ html >

JSP Directive Tag

JSP Directive Tag Directive Tag gives special instruction to Web Container at the time of page translation. Directive tags are of three types: page , include and taglib . Directive Description <%@ page ... %> defines page dependent properties such as language, session, errorPage etc. <%@ include ... %> defines file to be included. <%@ taglib ... %> declares tag library used in the page We'll discuss about include and taglib directive later. You can place page directive anywhere in the JSP file, but it is good practice to make it as the first statement of the JSP page. The Page directive defines a number of page dependent properties which communicates with the Web Container at the time of translation. Basic syntax of using the page directive is <%@ page attribute="value" %> where attributes can be one of the following : import attribute language attribute extends attribute session attribute isThreadSafe attribute i

JSP Declaration Tag

JSP Declaration Tag We know that at the end a JSP page is translated into Servlet class. So when we declare a variable or method in JSP inside Declaration Tag , it means the declaration is made inside the Servlet class but outside the service(or any other) method. You can declare static member, instance variable and methods inside Declaration Tag . Syntax of Declaration Tag : < %! declaration % > Example of Declaration Tag < html > < head > < title > My First JSP Page </ title > </ head > <%! int count = 0; %> < body > Page Count is: < % out.println(++count); % > </ body > </ html >   In the above code, we have used the declaration tag to declare variable count . The above JSP page becomes this Servlet : public class hello_jsp extends HttpServlet { int count = 0 ; public void _jspService ( HttpServletRequest request ,

JSP Scripting Tag

JSP Scriptlet Tag Scriptlet Tag allows you to write java code inside JSP page. Scriptlet tag implements the _jspService method functionality by writing script/java code. Syntax of Scriptlet Tag is as follows : < % JAVA CODE % > JSP: Example of Scriptlet In this example, we will show number of page visit. < html > < head > < title > My First JSP Page </ title > </ head > <% int count = 0; %> < body > Page Count is < % out.println(++count); % > </ body > </ html >     We have been using the above example since last few lessons and in this scriptlet tags are used. Everything written inside the scriptlet tag is compiled as java code. Like in the above example, we initialize count variable of type int with a value of 0. And then we print it while using ++ operator to perform addition on it. JSP makes it so easy to perform calculations, databas

Scripting Elements of JSP

JSP Scripting Element JSP Scripting element are written inside <% %> tags. These code inside <% %> tags are processed by the JSP engine during translation of the JSP page. Any other text in the JSP page is considered as HTML code or plain text. Example: < html > < head > < title > My First JSP Page </ title > </ head > <% int count = 0; %> < body > Page Count is < % out.println(++count); % > </ body > </ html >     Just to experiment, try removing the <% %> scriplet tag from the above code and run it as JSP. You will see that everything is printed as it is on the browser, because without the scriplet tag, everything is considered plain HTML code. There are five different types of scripting elements Scripting Element Example Comment <%-- comment --%> Directive <%@ directive %> Declaration <%! declarations %>

Lifecycle of JSP

Image
Lifecycle of JSP A JSP page is converted into Servlet in order to service requests. The translation of a JSP page to a Servlet is called Lifecycle of JSP. JSP Lifecycle is exactly same as the Servlet Lifecycle, with one additional first step, which is, translation of JSP code to Servlet code. Following are the JSP Lifecycle steps: Translation of JSP to Servlet code. Compilation of Servlet to bytecode. Loading Servlet class. Creating servlet instance. Initialization by calling jspInit() method Request Processing by calling _jspService() method Destroying by calling jspDestroy() method Web Container translates JSP code into a servlet class source(.java) file , then compiles that into a java servlet class. In the third step, the servlet class bytecode is loaded using classloader. The Container then creates an instance of that servlet class.

Introduction of JSP

Image
Introduction to JSP JSP technology is used to create dynamic web applications. JSP pages are easier to maintain then a Servlet . JSP pages are opposite of Servlets as a servlet adds HTML code inside Java code, while JSP adds Java code inside HTML using JSP tags. Everything a Servlet can do, a JSP page can also do it. JSP enables us to write HTML pages containing tags, inside which we can include powerful Java programs. Using JSP, one can easily separate Presentation and Business logic as a web designer can design and update JSP pages creating the presentation layer and java developer can write server side complex computational code without concerning the web design. And both the layers can easily interact over HTTP requests. In the end a JSP becomes a Servlet JSP pages are converted into Servlet by the Web Container. The Container translates a JSP page into servlet class source(.java) file and then compiles into a Java Servlet class. Why JSP is preffered ov