Pages

JSP

JSTL
JSP

JavaServer Pages (JSP) is a Technology for developing Webpages that supports dynamic content. This helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %> JSP Technology is used to create web application just like Servlet technology. JSP can be thought of as an extension to Servlet because it provides more functionality than servlet like Expression Language (EL), JSTL.

Advantages of JSP
  1. Easy to maintain
  2. Fast Development
  3. Extension to Servlet
  4. Less code than Servlet
Lifecycle of a JSP
  1. Translation
  2. Compilation
  3. Classloading
  4. Instantiation
  5. Initialization
  6. Request Processing
  7. Destroy
JSP Elements (Declaration, Scriptlet, Expression)

JSP Declaration : A declaration tag is a piece of Java code for declaring variables, methods and classes. If we declare a variable or method inside declaration tag it means that the declaration is made inside the servlet class but outside the service method. We can declare a static member, an instance variable and methods inside the declaration tag.
Syntax : <%! Here Declare the method or a variable inside the JSP Declaration Tag %>

JSP Scriptlet : Scriptlet tag allows to write Java code into JSP file. JSP Container moves statements in _jspservice() method while generating servlet from JSP. For each request of the client, service method of the JSP gets invoked hence the code inside the Scriptlet executes for every request. A Scriptlet contains Java Code that is executed every time JSP is invoked.
Syntax : <% Here Declare the Java Code inside the JSP Scriptlet Tag %>

JSP Expression : Expression tag evaluates the expression placed in it. It accesses the data stored in stored application. It allows create expressions like arithmetic and logical. It produces scriptless JSP page.
Syntax : <%= Here Declare the arithmetic or logical expression inside the JSP Expression Tag %>

JSP Comments : Comments are the one when JSP container wants to ignore certain texts and statements. When we want to hide certain content, then we can add that to the comments section.
Syntax : <%-- Here Declare the comments which is ignored by browser inside the JSP Comments Tag %>

JSP Directives (Page Directive. Include Directive, Taglib Directive)

JSP Directives : JSP directives are the messages to JSP container. They provide global information about an entire JSP page. JSP directives are used to give special instruction to a container for translation of JSP to servlet code. In JSP life cycle phase, JSP has to be converted to a servlet which is the translation phase. They give instructions to the container on how to handle certain aspects of JSP processing Directives can have many attributes by comma separated as key and value pairs.
Syntax : <%@ directive attribute="value" %>

JSP Page Directives : JSP Page Directives provides attributes that get applied to entire JSP page. JSP Page Directives defines page dependent attributes, such as scripting language, error page, and buffering requirements and used to provide instructions to a container that pertains to current JSP page.
Syntax : <%@ page attribute="value" %>

JSP Include Directives : JSP Include Directives is used to include one file to the another file. JSP Include Directives includes file during translation phase. JSP Include Directives is also useful in creating templates with the user views and break the pages into header and footer and sidebar actions. This included file like HTML, JSP, Text files, Servlets etc.
Syntax : <%@ include file="resourceName" %>

JSP Taglib Directives : JSP Taglib Directives is used to define the tag library with taglib as the prefix, which we can use in JSP. JSP Taglib Directives is used in the JSP pages using the JSP Standard Tag Libraries and uses a set of custom tags, identifies the location of the library and provides means of identifying custom tags in JSP page.
Syntax : <%@ taglib uri="uri" prefix="value" %>

List of Attributes in JSP Page Directive

01. language : It defines the programming language being used in the page.
Syntax : <%@ page language="value" %>

02. extends : This attribute is used to extend (inherit) the class like JAVA does
Syntax : <%@ page extends="value" %>

03. import : This attribute is most used attribute in page directive attributes.It is used to tell the container to import other java classes, interfaces, enums, etc. while generating servlet code.It is similar to import statements in java classes, interfaces.
Syntax : <%@ page import="value" %>

04. contentType : It defines the character encoding scheme i.e. it is used to set the content type and the character set of the response and default type of contentType is "text/html; charset=ISO-8859-1".
Syntax : <%@ page contentType="value" %>

05. info : It defines a string which can be accessed by getServletInfo() method. This attribute is used to set the servlet description.
Syntax : <%@ page info="value" %>

06. session : SP page creates session by default. We don't need a session to be created in JSP, and hence, we can set this attribute to false in that case.The default value of the session attribute is true, and the session is created. When it is set to false, then we can indicate the compiler to not create the session by default.
Syntax : <%@ page session="true/false" %>

07. isThreadSafe : It defines the threading model for the generated servlet. It indicates the level of thread safety implemented in the page. Its default value is true so simultaneous. We can use this attribute to implement SingleThreadModel interface in generated servlet. If we set it to false, then it will implement SingleThreadModel and can access any shared objects and can yield inconsistency.
Syntax : <%@ page isThreadSafe="true/false" %>

08. autoflush : This attribute specifies that the buffered output should be flushed automatically or not and default value of that attribute is true. If the value is set to false the buffer will not be flushed automatically and if its full, we will get an exception. When the buffer is none then the false is illegitimate, and there is no buffering, so it will be flushed automatically.
Syntax : <%@ page autoFlush="true/false" %>

09. buffer : Using this attribute the output response object may be buffered. We can define the size of buffering to be done using this attribute and default size is 8KB. It directs the servlet to write the buffer before writing to the response object.
Syntax : <%@ page buffer="value" %>

10. isErrorPage : It indicates that JSP Page that has an errorPage will be checked in another JSP page. Any JSP file declared with "isErrorPage" attribute is then capable to receive exceptions from other JSP pages which have error pages. Exceptions are available to these pages only and default value is false.
Syntax : <%@ page isErrorPage="true/false" %>

11. pageEncoding : This attribute defines the character encoding for JSP page. The default is specified as "ISO-8859-1" if any other is not specified.
Syntax : <%@ page pageEncoding="vaue" %>

12. errorPage : This attribute is used to set the error page for the JSP page if JSP throws an exception and then it redirects to the exception page.
Syntax : <%@ errorPage="value" %>

13. isELIgonored : This is a flag attribute where we have to decide whether to ignore EL tags or not. Its datatype is java enum, and the default value is false hence EL is enabled by default.
Syntax : <%@ page isELIgnored="true/false" %>

JSP Implicit Objects

JSP Implicit Objects : JSP Implicit Objects are created during the translation phase of JSP to the servlet. These objects can be directly used in scriplets that goes in the service method. Automatically, These Implicit Objects are provided by the JSP Container and they can be accessed using these objects.

01. out : Out is one of the implicit objects to write the data to the buffer and send output to the client in response. Out object allows us to access the servlet's output stream. Out is object of javax.servlet.jsp.jspWriter class. While working with servlet, we need printwriter object

02. request : The request object is an instance of java.servlet.http.HttpServletRequest and it is one of the argument of service method. It will be created by container for every request. It uses getParameter() to access the request parameter. It will be used to request the information like parameter, header information , server name, etc.

03. response : Response is an instance of class which implements HttpServletResponse interface. Container generates this object and passes to _jspservice() method as parameter. Response Object will be created by the container for each request. It represents the response that can be given to the client. The response implicit object is used to content type, add cookie and redirect to response page

04. config : Config is of the type java.servlet.servletConfig. It is created by the container for each jsp page. It is used to get the initialization parameter in web.xml

05. application : Application object (code line 10) is an instance of javax.servlet.ServletContext and it is used to get the context information and attributes in JSP. Application object is created by container one per application, when the application gets deployed. Servletcontext object contains a set of methods which are used to interact with the servlet container.We can find information about the servlet container

06. session : The session is holding httpsession object. Session object is used to get, set and remove attributes to session scope and also used to get session information

07. pageContext : This object is of the type of pagecontext. It is used to get, set and remove the attributes from a particular scope. Four Type of Scopes in pageContext : Page, Request, Session, Application

08. page : Page implicit variable holds the currently executed servlet object for the corresponding JSP. Acts as this object for current jsp page.

09. exception : Exception is the implicit object of the throwable class. It is used for exception handling in JSP. The exception object can be only used in error pages.

JSP Standard Action Tags

JSP Action : JSP Standard Actions Tags use the construct in XML syntax to control the behavior of the Servlet Engine. We can dynamically insert a file, reuse the beans components, forward user to another page etc. through JSP Actions like include and forward. The action tags are used to control the flow between pages and to use Java Bean.
Syntax : <jsp:action_name attribute="value" />

01. jsp:useBean : This action name is used when we want to use beans in the JSP page With this tag, we can easily invoke a bean.
Syntax : <jsp:useBean id="" class="" />

02. jsp:include : This action name is used to insert a jsp file into another file, just like include directive. It is added during request processing phase
Syntax : <jsp:include page="page URL" flush="true/false" />

03. jsp:setProperty : This property is used to set the property of the bean. We need to define a bean before setting the property
Syntax : <jsp:setproperty name="" property=""/>

04. jsp:getProperty : This property is used to get the property of the bean. It converts into a string and finally inserts into the output.
Syntax : <jsp:getAttribute name="" property=""/>

05. jsp:forward : It is used to forward the request to another jsp or any static page. Here the request can be forwarded with no parameters or with parameters.
Syntax : <jsp:forward page="value"/>

06. jsp:plugin : It is used to introduce Java components into jsp, i.e., the java components can be either an applet or bean. It detects the browser and adds <object> or <embed> tags into the file
Syntax : <jsp:plugin type="applet/bean" code="objectcode" codebase="objectcodebase"/>

07. jsp:param : This is child object of the plugin object described above It must contain one or more actions to provide additional parameters.
Syntax : <jsp:params> <jsp:param name="val" value="val"/> </jsp:params>

08. jsp:body : This tag is used to define the XML dynamically i.e., the elements can generate during request time than compilation time. It actually defines the XML, which is generated dynamically element body.
Syntax : <jsp:body> </jsp:body>

09. jsp:attribute : This tag is used to define the XML dynamically i.e. the elements can be generated during request time than compilation time. It actually defines the attribute of XML which will be generated dynamically.
Syntax : <jsp:attribute> </jsp:attribute>

10. jsp:text : It is used to template text in JSP pages. Its body does not contain any other elements, and it contains only text and EL expressions.
Syntax : <jsp:text> template text </jsp:text>

11. jsp:output : It specifies the XML declaration or the DOCTYPE declaration of JSP. The XML declaration and DOCTYPE are declared by the output.
Syntax : <jsp:output doctype-root-element="" doctype-system="" />

JSP Expression Language (EL)

JSP Expression Language (EL) : Expression Language (EL) is mechanism that simplifies the accessibility of the data stored in Java bean component and other object like request, session and application, etc. There are many operators in JSP that are used in EL like arithmetic and logical operators to perform an expression. It was introduced in JSP 2.0
Syntax : ${ expression }