Getting Started with JSP

JSP(Java Server Pages) is a view technology which allows to write template text in client side languages like HTML, CSS, JavaScript and others, instead of Java itself(Which is true for Servlet). To achieve this JSP supports Taglibs(Backed by Java code), Expression Languages and Scriptlet to control theoutput of page.

In the end a JSP becomes Servlet, Servlet which is created by Container and not Developer. So you would require a JSP capable Webserver or Application Server to run a JSP in you application. Stand alone J2SDK is not enough to run JSP.

Different Application Servers

  • Oracle Weblogic
  • IBM WebSphere
  • JBoss
  • Apache Tomcat

Note*: Tomcat is not Application server but a servlet container which can be used for JSP’s.

 

JSP is Servlet(In the end)

When we write a JSP it becomes a Servlet running in web app server. This servlet is same like other Servlet except it is created for you by Container.

Steps Followed by Container

Container follows the given steps in Converting JSP to Servlet.

  1. JSP written by Developer
  2. Container translate this JSP in Servlet Class source file(.Java)
  3. Compiler above Java source file into Java servlet Class.
  4. Loaded and initialized

After above step it is just a Servlet(No JSP) and it runs in the same way as it would have been written as Servlet by developer.

Question: Where different part of JSP will be added in generated Servlet?

Example of Generated Servlet

Actual JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<% out.println("Hello"); %>
</body>
</html>

 Generated Servlet

package org.apache.jsp;

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

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

  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().getServletContext()).getExpressionFactory();
    _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
  }

  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 {
      response.setContentType("text/html; charset=ISO-8859-1");
      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("rn");
      out.write("<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">rn");
      out.write("<html>rn");
      out.write("<head>rn");
      out.write("<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">rn");
      out.write("<title>Insert title here</title>rn");
      out.write("</head>rn");
      out.write("<body>rn");
      out.write("t");
 out.println("Hello"); 
      out.write("rn");
      out.write("</body>rn");
      out.write("</html>");
    } catch (Throwable t) {
      if (!(t instanceof SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}

 

Methods in Generated Servlet

Generated Servlet will have 3 method like Servlets created by developer.

jspInit()

This method is called from init() method. This method can be overriden.

jspDestroy()

This method is called from destroy() method of Servlet. This method can be override.

_jspService()

This method is called from service method of Servlet. This method run in individual thread whenever invoked by request.Container passed the request and response object to this method. This method can not be overriden. But we can control which code will go in this method.

3 Comments Getting Started with JSP

  1. Atmiya Kolsawala

    I want to learn java with all guideline from the software installation to till how to work with it

    Reply
  2. Java Code

    Just desire to say your article is as astounding.
    The clearness on your submit is simply excellent and i could assume
    you’re a professional in this subject. Fine along with your permission let me to clutch your RSS
    feed to keep up to date with drawing close post. Thank you one million and please
    carry on the gratifying work.

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.