Let us quickly go through what we learnt in the previous few chapters about the JSP Technology.
Introduction to JSPs
• JSPs are an integral part of any J2EE Application and are used extensively
• CGI, Perl and Active Server Pages (ASP) are all predecessors of the JSP Technology
• A JSP page gets converted/translated into a Servlet before it is executed
• You can have the following in a JSP Page
o HTML Code
o Java Code
o XML Code
o JSP Scriptlets
JSP Tags:
• JSP Tags begin with a “<” symbol and end with a “>” symbol
• There are many directives that can be used in a JSP Page
• The include directive is a placeholder that lets you include the text in another file
• The page directive gives directions to the servlet engine about the general setup of the page
• The scriptlet is a way to include Java code directly in a JSP page
• JSP Tags can be used in two ways. One is the regular JSP Tag construct and the other is the XML Equivalent.
The Page Directive:
• The Page Directive is one of the important components of any JSP Page. It can help us define page specific properties like Buffer size or location of an error page etc
• In JSP, you do not have to use the page directive for the default import list of java.lang.*, javax.servlet.*, javax.servlet.jsp.*, and javax.servlet.http.*. These are automatically available to you
• The page directive can be used to define an error page to which the control will be re-directed in case there is an issue with the current page
• The page directive also defines if the current page is an error page that would be invoked by any other JSP page
JSP Lifecycle:
• Just like Servlets, JSPs too have a life cycle
• The following are the life cycle stages of a JSP Page
o Page translation
o Page compilation
o Load class
o Create instance
o Call jspinit
o Call _jspService
o Call jspDestroy
JSP Implicit Objects:
In any JSP Page, there are a bunch of implicit objects that are available for the programmer to use. It contains a variety of information that can be used to display stuff on the page. The following Implicit Objects are available for the programmer inside the JSP Page
• request
• response
• out
• session
• config
• application
• page
• pageContext
• exception
Key Terms Learnt in the previous Chapters:
• JSP
• servlet
• directive
• expression
• scriptlet
• implicit object
• JSP container
• JSP page lifecycle
• tag library
• Web application
Previous Chapter: Chapter 37 - JSP Scriptlets
Next Chapter: Self Test - Chapters 31 to 37
Topics Covered in the Blog - Synopsis
Showing posts with label servlet. Show all posts
Showing posts with label servlet. Show all posts
Tuesday, April 12, 2011
Tuesday, March 1, 2011
Chapter 5: JSP to Servlet Conversion
In the previous chapter, we took a look at how a JSP file looks like and the contents that can be present inside a typical JSP file.
As you might already know (If you have J2EE programming experience) a JSP file gets converted into a Servlet at runtime and then gets executed. Well, if you did not know this, don't worry. That is what this chapter is for. To tell you the fact that JSPs get converted into Servlets for execution and also to tell you how that happens.
So, lets get started!!!
JSP to Servlet Conversion
JSPs are converted to servlets before the container runs them. This is actually cool because you don't need hardcore java programming skills to create a JSP page whereas you’ll need them to write a servlet. Moreover, all you’ll need to write a JSP is some expertise in creating HTML files and in using JavaScript. You can create front-end JSP pages without having much expertise in Java at all. Although JSP reduces the required skill level, JSP becomes a servlet, with the nice performance and portability benefits.
Below is how the conversion happens.
First lets look at a sample JSP page that we will consider for this conversion process. It's the same sample JSP we saw in the previous chapter. Lets name this guy my_first_jsp.jsp
Sample JSP File Code:
< html >
< body >
I Like Cars, Especially Ferrari .
< / body >
< / html >
This JSP file has to be placed in the …\jakarta-tomcat-4.0.1\webapps\examples\jsp folder in our system. To access this JSP through the tomcat server we can use the below URL:
http://localhost:8080/examples/jsp/my_first_jsp.jsp.
When you hit enter after typing the contents above in the browsers address bar, tomcat covnerts this JSP into a servlet, compiles it and then invokes it.
The servlet that gets created will be placed in …\jakarta-tomcat-4.0.1\work\localhost\examples\jsp as my_0005fservlet$jsp.java.
The contents of this converted Servlet would be as below:
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;
public class my_0005fservlet$jsp extends HttpJspBase {
static {
}
public my_0005fservlet$jsp( ) {
}
private static boolean _jspx_inited = false;
public final void _jspx_init()
throws org.apache.jasper.runtime.JspException {
}
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;
String _value = null;
try {
if (_jspx_inited == false) {
synchronized (this) {
if (_jspx_inited == false) {
_jspx_init();
_jspx_inited = true;
}
}
}
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=" +
"ISO-8859-1");
pageContext = _jspxFactory.getPageContext(this,
request, response, "",
true, 8192, true);
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
// HTML // begin [file="/jsp/my_first_jsp.jsp"]
out.write(">
\r\n< html >\r\n< body >"+
"\r\nI Like Cars, Especially Ferrari ."+
"\r\n
As you might already know (If you have J2EE programming experience) a JSP file gets converted into a Servlet at runtime and then gets executed. Well, if you did not know this, don't worry. That is what this chapter is for. To tell you the fact that JSPs get converted into Servlets for execution and also to tell you how that happens.
So, lets get started!!!
JSP to Servlet Conversion
JSPs are converted to servlets before the container runs them. This is actually cool because you don't need hardcore java programming skills to create a JSP page whereas you’ll need them to write a servlet. Moreover, all you’ll need to write a JSP is some expertise in creating HTML files and in using JavaScript. You can create front-end JSP pages without having much expertise in Java at all. Although JSP reduces the required skill level, JSP becomes a servlet, with the nice performance and portability benefits.
Below is how the conversion happens.
First lets look at a sample JSP page that we will consider for this conversion process. It's the same sample JSP we saw in the previous chapter. Lets name this guy my_first_jsp.jsp
Sample JSP File Code:
< html >
< body >
I Like Cars, Especially Ferrari .
< / body >
< / html >
This JSP file has to be placed in the …\jakarta-tomcat-4.0.1\webapps\examples\jsp folder in our system. To access this JSP through the tomcat server we can use the below URL:
http://localhost:8080/examples/jsp/my_first_jsp.jsp.
When you hit enter after typing the contents above in the browsers address bar, tomcat covnerts this JSP into a servlet, compiles it and then invokes it.
The servlet that gets created will be placed in …\jakarta-tomcat-4.0.1\work\localhost\examples\jsp as my_0005fservlet$jsp.java.
The contents of this converted Servlet would be as below:
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;
public class my_0005fservlet$jsp extends HttpJspBase {
static {
}
public my_0005fservlet$jsp( ) {
}
private static boolean _jspx_inited = false;
public final void _jspx_init()
throws org.apache.jasper.runtime.JspException {
}
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;
String _value = null;
try {
if (_jspx_inited == false) {
synchronized (this) {
if (_jspx_inited == false) {
_jspx_init();
_jspx_inited = true;
}
}
}
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=" +
"ISO-8859-1");
pageContext = _jspxFactory.getPageContext(this,
request, response, "",
true, 8192, true);
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
// HTML // begin [file="/jsp/my_first_jsp.jsp"]
out.write(">
\r\n< html >\r\n< body >"+
"\r\nI Like Cars, Especially Ferrari ."+
"\r\n
