Engineering Full Stack Apps with Java and JavaScript
JSPs allow you to write html, css, javascript etc. just as if you were writing an html file. JSPs can be considered as html pages (except for the .jsp extension and some extra lines) with some java code inside. JSPs may also contain no Java at all. In this example we will create our first JSP page, deploy it on Apache Tomcat and then runt it.
JDK (1.7 or more)
Eclipse IDE (Mars or more)
Apache Tomcat (7 or more)
Creating the project
Creating theJSP file in eclipse
Deploying and running the JSP file on server from eclipse
Deploying the project in tomcat
Verifying the compiler generated files
First, create a dynamic web project in eclipse (with web.xml) with the name JSP-Demo.
Next, create a JSP file called simplejsp.jsp under the WebContent folder. You may create from eclipse as New > JSP File or even create a file called simplejsp.jsp and copy paste the lines above.
We will use the below simple JSP code in our file:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Simple JSP</title>
</head>
<body>
<h1>This is a simple JSP file</h1>
<p>
This is a simple JSP File.
<br> This JSP file does not contain any java code.
</p>
</body>
</html>
You may use only <!DOCTYPE html> if you want to be html5 compliant.
You can test it by right clicking and selecting run on the server.
Do copy and save this URL before going to next step, if you don't want to type it all again.
Now export the project as a war file by right clicking on the project and going to export > war file.
Copy this war into the tomcat installation’s webapps directory.
Before starting tomcat, make sure you stop the server from eclipse, if it was running from eclipse. If it was running from a standalone start from command line using startup.bat, stop it using shutdown.bat from the bin folder.
Now go to the bin folder of tomcat installation from command line and run startup.bat.
Once the server is up and before testing the JSP file, verify below things:
Once the server is up, you can see the JSP-Demo context folder created in the tomcat installation’s webapps directory. Whatever you place within the WebContent folder will be directly placed in this context and can be easily accessed by specifying the name after the context. The url in this case will be http://localhost:8080/JSP-Demo/simplejsp.jsp.
Before running the url, also verify the folder where servlets are generated from jsps, which is <Tomcat-Installation-Directory>/work/Catalina/localhost/<context- directory>/org/apache/jsp. In our case it will be <Tomcat-Installation-Directory>/work/Catalina/localhost/JSP-Demo. Currently this folder will be empty.
Once the server is started, execute your JSP file using the previous url from your browser. You should get a similar response as you got from your eclipse.
Now go back to the folder where servlets are generated from jsps, which is <Tomcat-Installation-Directory>/work/Catalina/localhost/<context- directory>/org/apache/jsp. In our case it will be <Tomcat-Installation-Directory>/work/Catalina/localhost/JSP-Demo and you will find a package structure as org/apache/jsp with files simplejsp_jsp.class and simplejsp_jsp.java.
You can open and check this servlet to learn more.
I have collapsed all block and method contents.
Important things you will notice about this Java file are that:
It is a final class named simplejsp_jsp, that extends org.apache.jasper.runtime.HttpJspBase and implements org.apache.jasper.runtime.JspSourceDependent and org.apache.jasper.runtime.JspSourceImports.
Apart from few getter methods, it has three important methods:
_jspInit()
public void _jspDestroy()
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
Above three methods are part of the JSP life cycle.
Within the –jspService lifecycle method, you can see the print statements generated for your JSP file:
Complete _jspService method is not shown.
We will next see the jsp life cycle in detail.
Eclipse Luna Java EE IDE for Web Developers and Apache Tomcat 8.0.18, using Servlet spec 3.1.