Java Certification Training Course
- 45k Enrolled Learners
- Live Class
Thinking what is JSP and its usage? Well, you have landed at the right place! Java Server Pages, commonly termed as JSP technology is one of the Java web technologies. It is a server-side technology basically used for creating web applications. Let me discuss the concept of JSP in depth with you all.
In this article, I will be covering the following pointers:
JSP technology is basically a language widely used to develop JSP pages. This technology creates web content which consists of both dynamic as well as static components.
Now, let me explain what exactly is a JSP page!
A JSP page is a text document. It contains two types of text: static content and dynamic content. Static content can be expressed in any text-based format, say, HTML. Whereas, the dynamic content comprises of the Java code. JSP technology here combines the static content with the Java code, hence making it a dynamic web page. The file extension for the source file of a JSP page is supposed to be .jsp. The extension for the source file of a fragment of a JSP page is .jspf.
Now that you are familiar with the concept of JSP pages and JSP technology, let’s proceed and understand the features of JSP!
1. Easy coding
JSP allows tag-based programming. Hence, there is no need for expertise in the Java language. HTML tags are easy to use, hence the code is easily readable.
2. Interactive web pages
It allows us an easy connection to the database as it mainly connects with the server.
After studying about the features, let’s move further and look out for the lifecycle of a JSP page.
Let me explain the steps involved in the above-shown diagram.
1. JSP page translation:
A Java servlet file is created from the JSP source file. In the translation phase, the container validates the correctness of the JSP pages and tag files.
2. JSP page compilation:
The created java servlet file is compiled into a Java servlet class.
3. Class loading:
The java servlet class that was compiled from the JSP source is now loaded into the container.
4. Execution phase:
In the execution phase, the container creates one or more instances of this class in response to the requests. The interface JsP Page contains jspInit() and jspDestroy(). JSP provides special interface HttpJspPage for JSP pages specifically for the HTTP requests and this interface contains _jspService().
jspInit() method is called immediately after the instance is created.
6. jspDestroy() execution:
There are certain life cycle methods provided in JSP, these are: jspInit(), _jspService() and jspDestroy(), explained above.
Learning about the lifecycle is important. It gives you an insight into the actual functioning. Now, let us see and understand the syntax used in creating a JSP page.
Syntax of JSP
The syntax for the following in JSP:
<%= expression %>
&<%marks1 = marks1 + marks2 %>
<%! Dec var %>
<%! int var = 50; %>
<% java code %>
Here, you can insert the respective Java code.
<% -- JSP Comments %>
Java servlets were the first attempt to get access to the full power of Java in Web applications. They are written in Java. To make you more familiar with servlets let me show you the code. For more information, kindly go through the ‘Introduction to Java Servlets’ blog!
Now, let me show you a code which will teach you to create a JSP page.
A Simple JSP page
<HTML> <HEAD> <TITLE>A Web Page</TITLE> </HEAD> <BODY> <% out.println ("JSP in JAVA") ; %> </BODY> </HTMl>
As you can see in the above code, how easily a JSP page is created. This easier approach has helped JSP to take off so well. Simple HTML tags have been used. An additional element <% out.println (“JSP in JAVA”) ; %> can be seen. This element is called a scriptlet! It includes a java code used in an HTML-JSP code.
Moving further, let us dive in and learn how to run a JSP page.
Execution of JSP involves several steps. These are mentioned below:
Firstly, create an HTML file, say, ana.html, from here a request will be sent to the server.
Secondly, create a .jsp file, say, ana1.jsp, this would tackle the request of the user.
Thirdly, create a project folder structure.
Now, you need to create an XML file and then a WAR file.
After that, start Tomcat
Finally, you are ready to run the application.
On executing the above-written code in JSP file, the output looks like as shown below:
With this, we have reached towards the end of this article. I hope the content you read was informative and helpful. We will keep exploring the Java world with more topics. Stay tuned!
Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “JSP in Java” blog and we will get back to you as soon as possible.