Introduction
This article explains JSP in Java. The NetBeans IDE is used for the sample.
What JSP is
JSP is a web-page that contains static HTML contents and dynamic contents generation logic using a special JSP tag that is processed by the server.
It is just like Servlet technology, it can be thought of as an extension to the servlet because it provides more functionality than a servlet, such as expression logic, JSTL, and so on.
Why we use JSP since there is already a technology for developing dynamic web-pages
Servlet Technology has the following limitations:
- In a servlet the presentation logic is invalid with dynamic contents generation logic, that results in a maintenance problem.
- Development of the servlet is outside of production, since the application must write all the HTML to the output stream.
The following diagram describes the sequence of the processing of a JSP page.
The javax.servlet.jsp package contains classes and interfaces of the JSP API. The main interface of the JSP API is javax.servlet.jsp.JSPPage. This interface extends the servlet interface and provides the following methods.
- public void jspInit();
- public void jspDestroy();
This method is used to define the initialization and clean up operations for a JSP.
1. public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
"_" as the prefix of the method name denotes this method is auto-generated. It is neither defined nor overridden by the application programmer.
2. javax.servlet.jsp.jspBase
It is an abstract class, it implements the HttpJspBase interface and defines all the methods except the _jspService method.
It is used as super-class of the auto-generated servlet.
Types of JSP Tag
- Scriptlet
- Declaration
- Expression
- Action
- Directive
1. Scriptlet
Used as the main tag of JSP. It is used to define the processing logic, that is executed with _jspService(); method.
Syntax
<%
Request Processing Logic
%>
JSPPage may contains any number of scriptlet tags. At the time of translation all the scripts are grouped in the order of there occurrence in the JSP and their contents are copied to the _jspService method of the autoGeneratedServlet.
Code After compilation (Auto-generated Servlet)
1.0: Contents are copied to the _jspService() method of the auto-generatedServlet at the time of translation.
Exception*-> It is made available to the error handler page only.
Within a scriptlet tag the following objects are made available to the JSP program.
Implicit Object Type
1. out JspWriter(sub-class of PrintWriter)
2. request HttpServletRequest
3. response HttpServletResponse
4. config ServletConfig
5. session HttpSession
6. application ServletContext
7. page JspBase
8. pageContext PageContext
9. exception* Throwable
2. Declaration Tag
This tag defines the methods and data members in the autoGeneratedServlets.
Syntax
<%!
Data Member &
Method Definition
%>
Note
Overriding performed in JSP using this tag.
3. Expression Tag
This tag has the following two uses:
i. To write a string on a string returning expression to the output stream.
Syntax
<%= expression%>
ii. To assign the value of a variable or expression to an attribute of HTML elements.
Syntax
attribute="<%=variable%>"
4. Action Tag
It generates code in the _jspService method to perform a specific task.
i. <jsp:include>
Used to include the contents of a page to the response of the current JSP page.
Syntax
<jsp:include page="URL"/>
ii. <jsp:forward>
Used to forward a request to another page.
Syntax
<jsp:forward page="URL"/>
5. Directive
It represents an introduction to the translator. Directives are used to provide information to the translator, that can be used when the Servlet is auto-generated.
JSP supports the following 3 directives:
- include
- page
- taglib
i. include directive
Used to ask the translator to include the contents of the specified resource to the response of the current JSP.
<%@ include file="URL" %>
The difference between an include of an action and a directive
The following is the difference between an include of an action and a directive:
1. In the case of "include directives" the contents are included only once at a time of the translation whereas in the case of "include action" the contents are included each time the request is submitted.
2. "include directives" provide better performance but "include action" provides more flexibility.
3. "include directives" is used where the static contents are included and the "include action" is to be used when the dynamic contents are included.
How to create a simple JSP
For creating JSP you need a simple editor. Type there any HTML code and just save it with a .jsp extension. But to run a JSP page you need a web server like Tomcat, web-logic, Glassfish, etcetera.
Since I work with Tomcat7, if you are working on the same then you might configure this server first.
For the configuration and starting of the server use the following link:
http://www.c-sharpcorner.com/UploadFile/fd0172/how-to-configure-and-install-apache-tomcat-server-in-windows/
Now start creating a simple JSP page
The following are the two ways to create a JSP page:
- creating manually on a text-editor like Notepad or Notepad++.
- using an IDE like Netbeans, Eclipse, etcetera.
1. creating on a simple Notepad++
index.jsp
<html>
<body>
<h1>Hello</h1>
</body>
</html>
Note
You need to save the file in your Tomcat Server folder. for example my location is:
E:\myserver\tomcat7.0.40\webapps\examples\jsp\ab
Start deploying on server
First go to the command prompt and type the following command to start the server.
First go to that path where your server bin store is as shown below.
Now type:
Now press Enter; your server will be started. Minimize both of the command prompt windows.
Now open your web browser and enter the following command to see that it is working or not. If you have the same window then that means it's working.
http://localhost:9999/examples
For deploying the JSP page open the file directly using a web browser.
2. Create using the NetBeans IDE
In the NetBeans IDE you need to follow several steps.
Step 1
Open the NetBeans IDE.
Step 2
Choose "Java web" -> "web browser" as in the following:
Step 3
Enter your project name; I used "JSPDemo" as in the following:
Step 3
Click on "Next" and choose the Java version and your server wizard, then click on "Finish" as in the following:.
Step 4
Now you have seen a default index.jsp file has been created automatically. Now we change the code as in the following.
index.jsp
<!DOCTYPE HTML>
<html>
<body>
<% out.println("Hello User"); %>
</body>
</html>
Step 5
Now run the project.
Right-click on the Project menu then select "Run" as in the following:
Step 6
The following output is generated on the web browser.
Summary
This article is designed to provide a brief introduction to JSP in Java. This example is just intended to show a demo of how to create and run a JSP page.