Getting Started With Java Server Pages (JSPs) Lab

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.

 

Prerequisites

  • JDK (1.7 or more)

  • Eclipse IDE (Mars or more)

  • Apache Tomcat (7 or more)

 

Summary of the Steps

  1. Creating the project

  2. Creating theJSP file in eclipse

  3. Deploying and running the JSP file on server from eclipse

  4. Deploying the project in tomcat

  5. Verifying the compiler generated files

 

Creating the project and the JSP file in eclipse​

First, create a dynamic web project in eclipse (with web.xml) with the name JSP-Demo.

 

Creating theJSP file in eclipse

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.

 

 

Deploying and running the JSP file on Server from eclipse

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.

 

Deploying the project in tomcat

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.

 

Verifying the compiler generated files

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.

 

Development and Execution Environment

Eclipse Luna Java EE IDE for Web Developers and Apache Tomcat 8.0.18, using Servlet spec 3.1.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (52) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (4) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (7) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)