Updated April 11, 2023
Introduction to JSP File
JSP File is used to create dynamic and fast loading web pages over the network. The file extension used for JSP Files is “.jsp”. For example test.jsp. JSP files contain a global tag “HTML” and under that tag JAVA coding is done to get the responsive feature added to the web platform. If there would not have been JAVA code added then the web page would have been static. JSP files are often considered similar to JAVA servlets if we consider the usage but the added advantage to JSP files is the separation of page design and business logic.
How to Create a File in JSP?
One can create a JSP file just like an HTML file but the difference is that a JSP file should be saved with an extension of “.jsp”. It also should have “<html>” tags in the start or end of file. Once these tags are declared then inside html tags JAVA code can be inline. Also, a JSP file can be created separately as a standalone file and then a JSP file can be included in another file like JAVA servlets. Any IDE (Integrated Development Environment) like Eclipse or netbeans can be used to create a new “ dynamic web project”. Providing relevant information like name of main class, requirement of xml descriptors and other details a web project can be created. Once a project is created, a new JSP page can be created in the “WEB-INF” folder under the project.
File operations in JSP, JSP pages are compiled once and well connected with servlets or JSP as per the project requirements.
There are three tags used to do file operations in JSP and these are:
- Scriptlet Tag: This tag is used to script JAVA code in JSP pages. This tag looks like <% out.print( ” you are using scriptlet tag in JSP page to inline JAVA code ” ); %>. Here the “out.print()” function in AVA code is used to print the desired statement on the output screen. Variable declarations or control statements can also be included under this tag.
- Expression Tag: This tag is used to stream the outputs on the web page. It is to replace the function called “print()”. The tags look like: <%= ” You are using expression tag in JSP.. ” %>. This tag is used in the below provided examples as well.
- Declaration Tag: This tag is used to declare fields and methods in JSP pages. This tag does not use memory allocation since it is kept out of service() method of auto generated servlet. The JSP declaration tag looks like: <%! ..This is Declaration tag in JSP to declare methods or fields.. %>
Examples of JSP File
Given below are the examples mentioned :
Example #1
test1JSP project
Code:
JSP File: test1JSPfile.jsp ( This should be placed in webContent folder-> web-inf )
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example of JSP file</title>
</head>
<body>
<%= "Hello All, This output screen is designed using JSP page..." %>
</body>
</html>
Output:
Explanation:
- In this example, HTML tags are declared. In the HTML tags CSS can be applied by creating a separate global CSS sheet and be included in the JSP file.
- CSS can be included inline locally as well if usage of CSS is limited. In this example a web page titled “ Example of JSP file” is created.
- The character set is used using an attribute of the “meta” tag named “charset” and assigned UTF-8 as value to it. The output screen contains a statement showing up from JSP code.
- Inline JSP code should be written under tage “<%= ….. JSP Code ….. %>”.
Example #2
test1JSP project
Code:
<html>
<head><title>Example 2 in JSP</title></head>
<body>
<h1> Lets try your luck here! </h1>
<%
double num = Math.random();
if (num > 0.95) {
%>
<h2>You'll have a lucky day!</h2><p>The number you got in your stars is: <%= num %> </p>
<%
} else {
%>
<h2>Well, life goes on ... </h2><p>The number you got in your stars is: <%= num %> </p>
<%
}
%>
<a href="<%= request.getRequestURI() %>"><b>Click here to try Again</b></a>
</body>
</html>
Output:
Explanation:
- In this example, the JSP file with an extension “.jsp” is created under the “WEB-INF” folder. Like explained in the previous example, html tags should be used and under that JSP code can be inlined. The title of this page is titled as “Example 2 in JSP”. A variable is declared under the JSP tag of data type double and named “num”.
- “Num” is assigned with the value of “Math.random()”. This function is stored in JAVA library to generate and return a random number. The example then has an “if-else” control statement to check if the random value is more than “0.95” or not. If the condition is satisfied then data is loaded from the if section.
- If the condition is not met then the data from the else section is printed on the output screen. Finally a self referencing link is provided under “<a> </a>” tag. The function used for self referencing is “request.getRequestURI()”. If this link is clicked again then the content is loaded again. The new content loaded generates new random numbers and uses “if/else” logic to print the output screen.
Conclusion
JSP pages are a lightweight and quick method to create dynamic web content. The content can be loaded on the fly over the network without much overheads while data exchange. The best advantage offered by JSP pages is the division of “design” and “business logic”. The business logic shows up under JAVA code while design comes from HTML (Hypertext markup language) and CSS (Cascading style sheet). This division simplifies developer’s work and promotes easier project maintenance. JSP is one of the most elegant ways of generating projects on web dynamic development.
Recommended Articles
This is a guide to JSP File. Here we discuss how to create a file in JSP along with programming examples respectively. You may also have a look at the following articles to learn more –