What Is JSP In Java? Know All About Java Web Applications
What Is JSP In Java?
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!
What 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!
Features of JSP technology
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
The building of dynamic web pages that are able to interact with the users in a real-time environment.
3. Easy connection to a database
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.
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().
5. Initialization:
jspInit() method is called immediately after the instance is created.
6. jspDestroy() execution:
This method is called when JSP is destroyed. With this call, the servlet completes its purpose and goes into the garbage collection This ends the JSP life cycle.
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.
Programming & Frameworks Training
Syntax of JSP
The syntax for the following in JSP:
1. JSP Expression
1 | <%= expression %> |
Example:
1 | &<%marks1 = marks1 + marks2 %> |
2. Declaration Tag
1 | <%! Dec var %> |
Example:
1 | <%! int var = 50 ; %> |
3. JSP scriptlet
1 | <% java code %> |
Here, you can insert the respective Java code.
4. JSP comments
1 | <% -- JSP Comments %> |
As we are all acquainted with the syntax of JSP, now let me brief you about the term ‘Java servlet’.
What is a servlet?
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
1 2 3 4 5 6 7 8 | < 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.
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!
Build real-world apps from scratch and showcase your skills with our hands-on Flutter APP Development Course.
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 make a comment and you will get reply....
Comments
Post a Comment