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

Web Service


  • Declarations is of the form <%! code %> 
  • This  is  inserted into the body of the servlet class, outside of any existing methods
  • Its syntax is <%! declarations %>
    The declarations are inserted into the servlet class, not into a method
  • Use <%! ... %> for declarations to be added to your servlet class, not to any particular method
    Caution: Servlets are multithreaded, so nonlocal variables must be handled with extreme care
    If declared with <% ... %>, variables are local and OK
    Data can also safely be put in the request or session objects
  • Example:
    <%! private int accessCount = 0; %>
          Accesses to page since server reboot:
    <%= ++accessCount %>
    You can use <%! ... %> to declare methods as easily as to declare variables
XML Declration Syntax

  • Declaration ued above can also be  used as  xml syntax
  • Xml syntax is easy compared to normal.
  • All scripting elements i.e declaration, scriptlet and expression have
    xml declaration sysntax.


<%!  int counter=1; %>                                              
the XML equivalent of it is
int counter=1;


  • It is of the form  <% code %>.
  • It is inserted into the servlet’s _jspService method.
  • It contains the java code.
  • It does not produce  value that is inserted directly into the HTML
    as like in expression (<%= ... %> )
  • Scriptlets are Java code that may write into the HTML
  • Scriptlet code is  inserted into the servlet directly without any change
  • It gets compiled when the whole servlet is compiled

XML Scriplet Syntax
  • Scriptlet  used above can also be  used as  xml syntax
  • This syntax is easier compared to normal one.

java   code

the XML equivalent of
is equivalent to

out.print(counter);   </jsp:scriptlet>                                            

    java.util.Date date = new java.util.Date();
    out.println("Date is " + date);



Expression tag ( <%= %>)
This tag allows the developer to embed any Java expression and is short for
A semicolon ( ; ) does not appear at the end of the code inside the tag.

For example, to show the current date and time.

Date : <%= new java.util.Date() %>

<%!String name = "sohan";
  int math = 55;
  int eng = 66;
  int science = 77;
  int tot;
  int avg;
  String grade;%>

  tot = math + science + eng;
  avg = tot / 3;

  out.println("name is " + name);
  out.println("<br>marks of math  is " + math);
  out.println("<br>marks of english  is " + eng);
  out.println("<br>marks of science  is " + science);
  out.println("<br>total marks   is " + tot);
  out.println("<br>percentage  is " + avg);

  if (avg >= 60 && avg<=100)
    grade = "first";

  else if (avg >= 50)
    grade = "second";

  else if (avg >= 35)
    grade = "third";

    grade = "fail";
<br>grade is <%=grade %> 
Download Code

  • This is generated by the servlet compiler.
  • All Jsp file are converted into thye servelt on the server.
  • so this is the servelt for the above grade_jsp

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

public final class grade_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

String name = "sohan";
  int math = 55;
  int eng = 66;
  int science = 77;
  int tot;
  int avg;
  String grade;
  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

  private static java.util.List _jspx_dependants;

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.AnnotationProcessor _jsp_annotationprocessor;

  public Object getDependants() {
    return _jspx_dependants;

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().
    _jsp_annotationprocessor = (org.apache.AnnotationProcessor)

  public void _jspDestroy() {

  public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {

    PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;
    PageContext _jspx_page_context = null;

    try {
      pageContext = _jspxFactory.getPageContext(this, request, response,
            null, true, 8192true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;


  tot = math + science + eng;
  avg = tot / 3;

  out.println("name is " + name);
  out.println("<br>marks of math  is " + math);
  out.println("<br>marks of english  is " + eng);
  out.println("<br>marks of science  is " + science);
  out.println("<br>total marks   is " + tot);
  out.println("<br>percentage  is " + avg);

  if (avg >= 60 && avg<=100)
    grade = "first";

  else if (avg >= 50)
    grade = "second";

  else if (avg >= 35)
    grade = "third";

    grade = "fail";

      out.write("<br>grade is ");
      out.print(grade );
      out.write(' ');
    catch (Throwable t) {
      if (!(instanceof SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try out.clearBuffer()catch (java.io.IOException e) {}
        if (_jspx_page_context != null)
    finally {


Directive tag

Directive tag ( <%@ directive … %>)

  • A JSP directive gives special information about the page to the JSP Engine.

There are three main types of directives:

1) page – processing information for this page.

2) Include – files to be included.

3) Tag library – tag library to be used in a particular page..

  •     Directives do not produce any visible output when the page is requested
    but change the way the JSP Engine processes the page.
  •  For example, you can make session data unavailable to a page
    by setting a page directive (session) to false.

XML Directive Syntax
  • Directive used above can also be  used as  xml syntax
  • This syntax is easier compared to normal one.

java   code

 the XML equivalent of
<%@ page import="java.util.*" %>

 is <jsp:directive.page import="java.util.*" />
this example contains all the scripting elements as normal syntax and xml.syntax
String city="delhi";

int x=1;

out.print("city is "+city);
out.print("user has visited my page "+(++x)+" times");

<br>date is 
<%=new java.util.Date() %>

<br>sin90 is 

download code

1. Page directive

This directive has 11 optional attributes that provide the JSP Engine with special
processing information. The following table lists the 11 different attributes with a
brief description:Action tag
There are three main roles of action tags :
1) enable the use of server side Javabeans
2) transfer control between pages
3) browser independent support for applets.

2. Include directive

Allows a JSP developer to include contents of a file inside another. Typically include files are used for navigation, tables, headers and footers that are common to multiple pages

Two examples of using include files:

This includes the html from privacy.html found in the include directory into the
current jsp page.

<%@ include file = “include/privacy.html %>

or to include a naviagation menu (jsp file) found in the current directory.

<%@ include file = “navigation.jsp %>

3. Tag Lib directive

A tag lib is a collection of custom tags that can be used by the page.
<%@ taglib uri = “tag library URI” prefix = “tag Prefix” %>
Custom tags were introduced in JSP 1.1 and allow JSP developers to hide complex
server side code from web designers.

4. Action tag

There are three main roles of action tags :
1) enable the use of server side Javabeans
2) transfer control between pages
3) browser independent support for applets.


A Javabean is a special type of class that has a number of methods. The JSP page
can call these methods so can leave most of the code in these Javabeans. For
example, if you wanted to make a feedback form that automatically sent out an
email. By having a JSP page with a form, when the visitor presses the submit
button this sends the details to a Javabean that sends out the email. This way
there would be no code in the JSP page dealing with sending emails (JavaMail
API) and your Javabean could be used in another page (promoting reuse).

To use a Javabean in a JSP page use the following syntax:

<jsp : usebean id = “ …. “ scope = “application” class = “com…” />

The following is a list of Javabean scopes:

page – valid until page completes.
request – bean instance lasts for the client request
session – bean lasts for the client session.
application – bean instance created and lasts until application ends

Implicit Objects

So far we know that the developer can create Javabeans and interact with Java
objects. There are several objects that are automatically available in JSP called
implicit objects.

The implicit objects are

Variable Of type

Request                   Javax.servlet.http.httpservletrequest
Response                Javax.servlet.http. httpservletresponse
Out                          Javax.servlet.jsp.JspWriter
Session                    Javax.servlet.http.httpsession
PageContent           Javax.servlet.jsp.pagecontext
Application             Javax.servlet.http.ServletContext
Config                     Javax.servlet.http.ServletConfig
Page                       Java.lang.Object

Page object

Represents the JSP page and is used to call any methods defined by the servlet

Config object

Stores the Servlet configuration data.

Request object

Access to information associated with a request. This object is normally used in
looking up parameter values and cookies.

<% String devStr = request.getParameter(“dev”); %>
Development language = <%= devStr %>

This code snippet is storing the parameter “dev” in the string devStr. The result is
displayed underneath.