JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Controlling the Structure of Generated Servlets: The JSP page Directive




. JSP Page Directives

The page directive is used to define attributes that apply to an entire JSP page.
There attributes of page directive are, language, extends, import, session, buffer, autoFlush, isThreadSafe, info, errorPage,

contentType, isErrorPage, pageEncoding, isELIgnored

JSP Page Directives : language

Used for specifying other scripting languages to be used in a JSP page.

%@ page Language="java" %>


JSP Page Directives : extends

Used for specifying some other java classes to be used in a JSP page.

<%@ page  extends="pack.MyServ" %>

example

MyServ.java

package pack;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServ extends HttpServlet {
  private static final long serialVersionUID = 1L;

  public String name = "sohan";

  protected void doGet(HttpServletRequest request,
      HttpServletResponse responsethrows ServletException, IOException {

    response.setContentType("text/html");
    response.getWriter().print("Hello " + name);

  }

}
Download code

test.jsp

<%page  extends="pack.MyServ" %>
Name of the student is <%=name %>
download code

Output:

The name is fetched from the java file. and display in jsp file.



JSP Page Directives : import

Used for importing packages or java classes.

%@ page import="java.util.Date"%>

Example

<%page import="java.util.Date"%>

<% 
out.println(new Date());
%>

output




JSP Page Directives : session

Used for controlling session management.

<%@page session="true" %>

example

By default session object is available as u an see the code complied without error



when we set the session value to false the session is not more available to jsp page.





JSP Page Directives : buffer

Used by the out object to handle output generated by the JSP page.

JSP Page Directives : autoFlush

Used for controlling buffer flow.
<%@page autoFlush="true" %>


JSP Page Directives : isThreadsafe

Used to control the request handling feature.

<%@page isThreadsafe="true" %>


JSP Page Directives : info

Used to set some description for the page..

JSP Page Directives : errorPage

Used to set the error page to be called if any error occurs.

<%@page errorPage="error.jsp" %>

Example
error1.jsp
<%@page errorPage="error.jsp" %>

<%

int ar[]={11,22,3,4,5,6,7};

%>

8th no is <%=ar[7%>


 
download code


error.jsp
<h1>Error page</h1>
<%page isErrorPage="true" %>

 Error is <%=exception %>
download code


Output

when the error error1.jsp run it throw an exception.
when the exception is thrown it transfer the control to error.jsp which is an error page defined in the page attribute of the error1.jsp page.



JSP Page Directives : contentType

Used to specify the MIME type.

JSP Page Directives : isErrorPage

Used to specify if the jsp page is an error page or not

<%@page isErrorPage="true" %>


  • Bydefault isErrorPage is false




JSP Page Directives : pageEncoding

Used to specify the language that the page uses when the page is sent to the browser.

JSP Page Directives : isELIgnored

Used to control the EL expression evaluation in the JSP page
  • Bydefault it is false.
  • if  you want it you have to set it true.

JSP Static and Dynamic Inclusions

Static Inclusions

It can be done using the "include" directive.
<% @include file="included.html" %>

This is done at compile time not at run time.

Example

header.jsp
<h2>
<font color="Green">
IACT
</font>
</h2>

test1.jsp
<%include file="header.jsp" %>
<h3>
Welcome to Computer Study Center
</h3>

Output





Dynamic Inclusions

It can be done using the <jsp: include> action tag

<jsp:include page="included.html" />

This is done at compile time not at compile time.

Example

test3.jsp
<jsp:include page="header.jsp"></jsp:include>
<h3>
Welcome to Computer Study Center
</h3>
Output




JSP Implicit Variables and Objects

  • request: An instance of HttpServletRequest
  • response: An instance of HttpServletResponse
  • out: An instance of JspWriter
  • session: An instance of HttpSession
  • config: An instance of ServletConfig
  • application: An instance of ServletContext
  • page: An instance of Object
  • pageContext: An instance of PageContext
  • exception: An instance of Throwable


JSP Page Scopes

  • Page Scope
  • Request Scope
  • Session Scope
  • Application Scope
JSP Page Scopes: Page

This scope helps to keep the data available while the page is loading. Any object whose scope is defined as page scope will disappear as soon as the response is sent to the browser. The object with a page scope may be modified as often as desired within the particular page but the changes are lost as soon as the user moves away from the page. By default all beans have page scope.

JSP Page Scopes: Request

Any object created in the request scope will be available as long as the request object is valid. For example if the JSP page uses a <jsp:forward> tag, then the bean will be accessed in the forwarded page and if redirect is used then the bean is destroyed.

JSP Page Scopes: Session

In JSP terms, the data associated with the user has session scope. A session does not correspond directly to the user; rather, it corresponds with a particular period of time the user spends at a site. Typically, this period is defined as all the hits a user makes to a website between starting and exiting his browser.

JSP Page Scopes: Application

The bean associated with the application scope will be accessible to all the users and all the pages in the application.