JavaServer Pages | JSP

Learn to create dynamic web applications using JavaServer Pages

A new way to the servlet is called JSP Or JavaServer Pages. JSP’s are mostly similar to ASP( Active Server Pages). Mainly their functionality and syntaxes are similar. JSP is used in many servers and server-side. JSP does not use < servlet> tag in HTML 5 but uses actual snippets  of servlet code. also, The purpose is to separate content from presentation. Mainly in use for the pages where huge dynamic contents are present with static contents too.

thus, Like servlets need to include server chaining, JSP does not need any changes to the servlet API. It needs special support from the web server. JavaServer Pages permits directly inserting servlet code into it otherwise it has to be included in the static HTML file. thus, Every block of servlet code is called as scriptlets. These scriptlets are surrounded by <% tag and a closing   %> tag. 

For programming convenience, JSP has got four predefined variables as follows:
  • request: This is a servlet request and is an object of an HttpServletRequest class.
  • response: This is also a servlet response and is an object of HttpServletResponse class.
  • out: it is an output writer and is an object of printWriter class. 
  • in: It is an input reader and an object of BufferedReader class.

so, The example below shows a simple java server page. It gives the “Hello” message. afterwards, For running this on the web server you need to store this file in a server’s document root directory with an extension  .jsp. The name could be hello.jsp.

however, It could be accessible at the URL http://server: port/ hello.jsp. The server automatically:

  1. Creates
  2. Compiles
  3. Loads and 
  4. Runs 

A special servlet that generates the contents of the page. 

Program : Hello message with JSP-hello.jsp

The background program that supports the static HTML file as follows. out.println( ) gives call to the static contents. The program below is the workhorse program for hello.jsp

Expression and Directives | JavaServer Pages 

Like scriptlets, JSP permits the use of the expression and directives too. A JSP expression starts with <%= and ends with %>. The expression written between two tags will be evaluated and the result will be converted to the string and the text will be directly included on the page.

This technique removes the complexity because of an out.println() call. For example, <%= Val %>. This includes the value of variable ‘ val’. A JSP directive starts with <%@ and it ends with %>. The use of directive is to allow a JavaServer Pages to control specific aspects or features of its workhorse servlet.

Directives are used to set the content type of workhorse servlet or program. Also, it is used for importing a package and extending some superclass, which may implement an interface, including handling of GET OR POST requests. A directive may allow the use of non-java scripting language also.

Key variables can be assigned values between the directive tags as follows:

<%@ varname =”value”%>.

There is a total of six variables that can be set: 

  1. content_type
  2. Import
  3. extends
  4. implements
  5. method
  6. language

1. content_type: 

The content type is used to specify the type of contents of the page that was generated. For example: <%@ content_type = “text/plain” %>. so, The default content type of the page is ” text/html”.

2. import: 

Import is used to specify the list of classes that should be imported by the servlet. Many classes can be given in the list with the help of a comma as a separator. thus, Or even multiple import directives can be used. For example: <%@ import = ” java.io.*, java.util.*, java.lang.*”%>.

3. extends: 

Extends is used to mention the superclass that the servlet needs to extend. For example: <%@  extends =”SuperClass1″ %>. HttpServlet is a default superclass.

4. implements: 

Implements are used to specify the list of interfaces that should be implemented by the servlet. though Multiple interfaces could be separated by a comma or can be specified with the help of multiple import directives. <%@ implements =”Serializable”%>. By default, it does not implement anything.

5. method: 

Method directive is used to specify the method present in the servlet that needs to have generated code. therefore, they should handle the request given by the client “service” is a default method that handles all requests. so, For example, <%@ method=”doPost”%>.

6. language:

  • Language directive is used to specify the back-end scripting language. “java” is the default language. There could be other languages also. For example <%@ language=”java”%>.
  • Following example shows a program for hello page that include the use of directives: 
  • The background workhorse servlet for the JSP page above should be almost the same as the last program. The only difference is that servlet will implement doPost( ) instead of service( ) method.

Declarations: 

  • In JSP it may be required to define the methods and local variables in its workhorse servlet programs.
  • For this purpose, there is a constructor that is known as a JSP declaration. The declaration starts with a tag <SCRIPT RUNAT = ” server”> and it ends with a </SCRIPT> tag.
  • In between these two tags listed above, the user may also write servlet code which has to be placed outside the servlet’s service method.
  • however, The example below is a demonstration of the same. It is a JSP page that makes use of the declaration for defining the getName( ) method.
  • Program: the workhorse program for the above JSP program.

Life Cycle of a JavaServer Pages: 

  • A JSP life cycle is an entire process that includes the creation of the JSP till it’s destruction. so, This Life Cycle is similar to that of the servlet.
  • JSP needs an additional activity or step of compiling JSP into the servlet.
  • Following is the list of steps or path taken for JSP :
  1. Compilation
  2. Initialization
  3. Execution
  4. Cleanup

1. JSP Compilation: 

This phase compiles the page if it is never compiled or certain modifications have been done to it or if compilation is required. thus, The compilation process is made up of three phases :

  • Parsing the JSP.
  • Converting JSP into a servlet.
  • Compilation of the servlet.

2. JSP Initialization: 

When JSP is loaded by the container, the jspInit() method gets invoked before any request is serviced. Generally, initialization is done only once. so, It is used to initialize the database connections, create a lookup table and open files.

3. JSP Execution:  

  • In this phase of JSP, all its interaction with requests takes place. This phase gets over and continues till destroyed.
  • When a browser requests a JSP, the page gets loaded and is initialized. The JSP engine calls a method called _jspService( ) method.
  • The _jspService( ) method takes two parameters HttpServletRequest and HttpServletResponse.
  • The _jspService( ) method is called or invoked as per the request. It generates the response to the request and also gives a response to HTTP methods like DELETE,  POST, GET, etc.

4. JSP Cleanup:

This is a destructive phase of JSP life cycle. This phase represents a stage when JSP is removed by the container. The method jspDestroy( ) is used to destroy JSP.

Java Beans:

JSP can be used with JavaBeans components. It is an interesting combination of many programs and programmers. JavaBeans reuses Java classes having certain methods and conventions to give them additional abilities.

however, they can be directly embedded in a JSP page with the help of <BEAN> tag. so, JavaBeans components can do a well-define task as follows:

  1. Executing database queries.
  2. Getting connected to a mail server.
  3. Maintaining client-related information.

JavaBeans gives it’s resulting information to a java server page with the help of simple accessor( ) methods.

thus, The JavaBeans get special treatment by the web servers. For example, the server can set the bean’s instance variable or properties using parameter values from the request sent by the client.

In case if a client request includes a name parameter and if a server automatically detects it at runtime, the server comes to know that bean has to name property and a method called setName(String name). thus, A server automatically calls a setName method. A server can even automatically manage the scope of the bean. hence, A bean could be automatically assigned to a particular request.

Conclusion:

however, JavaServer Pages is a nicely-known and universal technology for developing Java web application views. Combined with servlets. thus, JSP pages are very powerful and provide access to the entire range of Java capabilities.

written by: Vickky Bopche

reviewed by: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

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