Showing posts with label JSP. Show all posts
Showing posts with label JSP. Show all posts

Implicit Variables

Implicit Variables:

In JSP we can use some variables without we even declaring, such variables are called as Implicit Variables. 
or 
The variables which can be used in JSP's without declaration, we call such variables as Implicit Variables.

There are 9 Implicit Variables available in JSP. They are:
  1. request
  2. response
  3. pageContext
  4. session
  5. application
  6. config
  7. out
  8. page
  9. exception
Let us see each one in detail.

request:

This implicit variables can be used directly in the JSP scriptlet without even declaring. This variable represents the HttpServletRequest of servlets. The following is the example for request variable.
request implicit variable
fig: request implicit variable example

response: 

response implicit variable represents the HttpServletResponse object of servlets. The following is the example of response implicit variable.
response implicit variables
fig: response implicit variables example

out: 

This out implicit variable is used to display the output to the user. out variable is of type JspWriter. JspWriter is a class which is available in javax.servlet.jsp package. JspWriter is just similar to the PrintWriter in Servlets
There are two methods which can be used with out variable. They are: 1) println 2)write

The following example shows the usage of out variable.
out Implicit variable
fig: out Implicit variable example

config: 

This config variable belongs to ServletConfig datatype. By using ServletConfig we can remove hardcoding. ServletConfig is an object used by servlet container to pass the information during initialization. The following figure shows you the usage example of config variable.

application:

This application implicit variable can be used directly in the jsp. application variable represents ServletContext. ServletContext object contains set of methods which are used to interact with ServletContainer. By using these methods we can find the information about ServletContainer. 

session:

This implicit object represents HttpSession. This variable is of type HttpSession.

page:

page implicit variable holds the currently executing Servlet object for the corresponding JSP. page variable is of type Object.

exception:

This implicit variable can be used only in error pages. When we try to use this implicit variable in JSP which is not an error page. We will get an error message. This variable of type Throwable.

pageContext: 

By using this pageContext implicit variable we can get any other implicit variable/object.                                                   

Scriptlets - JSP Element

Scriptlet is the JSP Element used to write the Java code in JSP program. The following is the syntax of the scriptlet in JSP. 
 
Scriptlet Syntax
Scriptlet Syntax

When we are using scriptlets in JSP, we must follow all the rules of Java. The following example shows the java code in the scirptlet.

scriptlet example

If we doesn't follow all the rules of Java, the JSP compiler will fail in converting .java program into .class file, instead it shows error in browser. We use Implicit variable as part of scriptlet.                                 

Template Text - JSP Element

Template Text:


Template Text is a JSP Element used to send the output to the client. We can write the Template Text directly in JSP without any special symbols. The following figure shows the example. 

TemplateText - JSP Element
Template Text Example
We know that every JSP program is converted into concerned servlet program. When we use Template Text the content will be placed inside out.println() in the generated servlet program.                               

JSP Elements

As of now we have seen, what is JSP? , How a JSP program executes? Now let us know one of the important part in JSP, i.e, JSP Elements. These JSP Elements play a crucial role in JSP program. Entire JSP program structure is based on these Elements. They are listed below:
  1. Template Text
  2. Scriptlet
  3. JSP Expressions
  4. JSP Declarations
  5. JSP Directives
  6. JSP Action Tags
  7. JSP Custom Tags
  8. El Expressions 
Click on each to know in detail.....

Calling JSP Compiler Manually

As of now we have seen the introduction and work flow of JSP programs. Now let us know about one of the most important part, i.e, JSPC (Java Server Pages Compiler). 

It is the responsibility of every server vendor to develop the JSP Compiler. They must develop JSPC based on the JSP specification. JSP compiler is a Java program which follows the rules of JSP specification. 

When the client sends the request to JSP, it is the responsibility of the server to call the JSP compiler. Even we can call JSPC manually, by calling manually we can understand the internal code of generated servlet.
 page under construction.....
               

JSP Flow of Execution

JSP Flow of Execution

Now in this post let us see the Flow of execution of a JSP program. When we deploy the JSP Programs, it is the responsibility of the of the JSP Container to convert JSP programs into corresponding Servlet code. Every JSP container is having a JSP Compiler. It is the responsibility of the JSP compiler to convert/to run the JSP program. The following figure shows the steps that will be carried out when we deploy the JSP on top of server.

JSP program execution flow/execution steps
JSP Program Flow of execution
  • The client sends the request to server.
  • JSP compiler generates(converts JSP into servlet) the servlet code of  one.jsp and places it in a file named one.jsp.java.
  • Now the Java Compiler comes into picture. JavaC converts the Java code into Byte code, i.e, it generates the .class file.
  • Now server creates the servlet object and process the clients request.

The server will carry out all the above steps, when it got the request for the first time. When the client sends the same request for the second time, the server just executes the servlet object of the corresponding JSP program.

Types of Servers

As far as Servers are considered, there are two different types of servers. They are:
  1. Web Server
  2. Application Server
  • Most of the web servers run Servlets and JSP's
  • Applications Servers run Servlets and JSP's, as well as they can run EJB (Enterprise Java Bean) applications also.
  • An application server provides complete implementation to JEE API. Where as a webserver provide the partial implementation of JEE API. 

JSP Introduction

JSP INTRODUCTION:

JSP stands for Java Server Pages. These are used to develop web based applications. JSP's are also used in client - server environment. Every program ends with '.jsp' extension. we no need to configure JSP programs as part of web.xml(Deployment Descriptor). We can send the request directly from the browser as follows. 
  • Eg: http://localhost:8080/webapp/one.jsp 

JSP Introduction, JSP LOGO

There are so many advantages of JSP when compared with servlets. The following are some of them. 

ADVANTAGES OF JSP ON SERVLETS:

  1. Improve the productivity of the developers. One can develop JSP project quickly when compared with servlets. 
  2. We can develop JSP's without the knowledge on Java programming language
  3. By using JSP's we can separate business logic and presentation logic.  
  4. Complexity in the code can be reduced using JSP's.
When we deploy the JSP project, the server converts the JSP programs into corresponding Servlets.