Updated March 28, 2023
Introduction to JSP in Java
JSP is a short form of Java Server Pages. JSP does the processing of all its stuff on the server-side. This is an extension of the core Java Language. In Java, we can write a program but making things dynamic JSP is the one that can help us in doing that. This is basically a combination of HTML and Java. If we have an HTML file, the processes can be done using JavaScript. JavaScript works on the client machine but JSP works on the server-side. JSP is the best suited for server-side dynamic web pages. A JSP page can be the HTML or the JSP or the combination of both. The moment we save the HTML page as JSP, it will give us the ability to use the Java code on that page.
How does JSP Work in Java?
For running the JSP page, we need to have an environment. Since it runs on the server-side we need to have a server in addition to JDK. JDK can run the Java program only. As we know, the JSP code only runs on the server. We can use the Tomcat server for running our JSP file.
For writing JSP code we need to write our code inside <% and %>.
<%
// JSP code goes here…
%>
Suppose we want to print Hello Word, we can write the code as mentioned below:
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<%
out.println("<h1>Hello World!</h1>");
%>
</body>
</html>
In the above code, we can see, it a combination of the HTML and the Java both.
The file extension of the JSP will be .jsp. If we want to save the above code as hello, then we need to save this with “hello.jsp”.
This program can be run using any of the web browsers. Function out.print() can be used to print anything on the output window. Everything we cannot achieve inside JSP but we can extend a fully functional Java class into our JSP file class. In JSP, we can get and set the session variable that we cannot in normal Java.
Syntax:
Since this is an extension of Java, code will be written in Java itself. There will be more features to play with addition to code Java features. There will be a block in which every code of the Java will be written. Outside this block HTML and the CSS, JavaScript, JQuery can be written. Now, time to see the scripting elements. These elements help us to write the java code into the JSP page.
Scriptlet Tag:
<% Our java source code goes here %>
Expression Tag:
<%= our expression goes here %>
Declaration Tag:
<%! Our field or the method declaration goes here %>
Implicit objects of JSP in Java
There are 9 implicit objects in JSP to fulfill the various need.
- out: This can be used to display the output on the screen.
PrintWriter out=response.getWriter();
- request: By using this, we can get the request submitted by form elements.
String name=request.getParameter("user_name");
- response: There are various things we can use this object. Like, sending a request to a page()
- config: This can be used for initializing the parameter for a page or whole application.
- Application
- Session
- pageContext
- page
- exception
This is some of the useful objects. JSP is more secure than another dynamic support language like PHP, ASP, etc. Due to Java Popularity, this JSP is still in demand.
Examples to Implement JSP in Java
Below are the examples of JSP in Java:
Example #1
Code: hello.jsp
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<%
out.println("<h1>Hello World.</h1>");
out.println("<h1>This is my first JSP Program.</h1>");
%>
</body>
</html>
Output:
Example #2
Code: comment.jsp
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<%
// This is single line comment
/*
This is multi-line comment line -1
This is multi-line comment line -2
*/
out.println("<h1>This is my first JSP Program.</h1>");
%>
</body>
</html>
Output:
Example #3
Code:
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<%@ page session = "true" %>
<%
int i = 10;
int j = 20;
int sum = i + j;
out.println("Sum of i and j will be: "+sum);
%>
</body>
</html>
Output:
Example #4
Code:
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<%@ page session = "true" %>
<%
int i = 10;
int j = 20;
int sum = i + j;
out.println("Sum of i and j will be: "+sum);
session.setAttribute("SUMSESSION",sum); // setting the sum output to a session, SUM is the name of the session variable
session.getAttribute("SUM"); // getting the session value
out.println("<br>Reading session value: "+ session.getAttribute("SUMSESSION"));
%>
</body>
</html>
Output:
Example #5
Code:
In this example, we will see, how we can print a message on the browser screen without using the out object.
<html>
<head>
<title>JSP Test Expression Page</title>
</head>
<body>
<%= "welcome to this jsp " %>
</body>expression tag
</html>
Output:
Example #6
Code:
<html>
<head>
<title>JSP Test Initialization Page</title>
</head>
<body>
<%! int i=50; int j=50; int sum=0; %>
<%= "Sum of i and j variables will be:"+(sum = i+j) %>
</body>
</html>
Output:
Conclusion
It is better to use any IDE as it provides us a complete environment to run the JSP Program. Eclipse is the one we can move ahead with. We can choose any Eclipse out of its various available versions. There is another similar term Servlet, which is again very similar in nature and functionality to the JSP. For the beginner, we can also find some online editors if we are not able to set an environment for running the JSP on your machine.
Recommended Article
This is a guide to JSP in Java. Here we discuss the Introduction to JSP in Java and top Implicit objects along with its examples with Code Implementation. You can also go through our other suggested articles to learn more –