JSP Components Declaratives Scriptlets Expressions

JSP Components -
    JSP has mainly six type of components.

1.JSP Declaratives
2.JSP Scriptlets
3.JSP Expressions

4.Directives 
5.Implicit Objects
6.Standard Actions 


JSP Declaratives - 
      JSP Declaratives is used for embed Java code in class label. Any code which is written in JSP Declaratives will goes to scope of a class of servlet. JSP Declaratives is open with <%! and closed with %> syntax.
If any variable is made in JSP Declaratives means this variable will be available for any where with in JSP.
Example -
    <HTML>
         <HEAD>
         <BODY>
        <%!
            public int getSum(int a, int b){
                return a+b;
            }
        %> 
         </BODY>
    </HTML>

Servlet of this JSP will be -

package org.apache.jsp;

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

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

 public int getSum(int a, int b){
         return a+b;
     }
  private static java.util.List _jspx_dependants;

  public Object getDependants() {
    return _jspx_dependants;
  }

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

    JspFactory _jspxFactory = null;
    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 {
      _jspxFactory = JspFactory.getDefaultFactory();
      response.setContentType("text/html");
      pageContext = _jspxFactory.getPageContext(this, request, response,
                  null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\r\n");
      out.write("<HTML>\r\n");
      out.write(" <HEAD>\r\n");
      out.write(" <BODY>\r\n");
      out.write(" \t");
      out.write("\r\n");
      out.write(" </BODY>\r\n");
      out.write("</HTML>\r\n");
    } catch (Throwable t) {
      if (!(t instanceof SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          out.clearBuffer();
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}

JSP Scriptlets - 
       JSP Scriptlets is also used for embed code with in a _jspService() methods in servelt. Any code which is written in JSP Declaratives will goes to _jspService() method of servlet. JSP Scriptlets syntax is
<%
    //embed java code
%>

Example -
    <html>
        <body>
        <%
            System.out.println(“This is my first JSP Page”);
        %>
        </body>
    </html>

JSP Expressions - 
      JSP Expressions is used for print any variable on JSP page. It works like out.println(); function.

Syntax of JSP Expressions -
    <%= // variable will comes here %>
Example -
    <HTML>
         <HEAD>
         <BODY>
            <%="My First JSP Page"%> 
         </BODY>
    </HTML>

Comments

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

Shallow Copy Deep Copy Java example

Database Connection using NODE JS example