Posts

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 cu...

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 reques...

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...

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 <%! declarat...