JSP
CHAPTER 1: BASIC INTRODUCTIONWhat is JSP?
JSP files are HTML files with special tags that contain Java source code that provides dynamic content.
Example: Google Search engine, the search page is dynamic will display the search results based on the user’s search request.
Page is referred to as dynamic as the same page displays different data based on different user input.
Sample JSP page
Java code embedded inside HTML tags using <%%> tags. This is the basic structure of JSP.
Servlet Vs JSP
In Servlets HTML is written inside java code using print statements. In JSP java code is embedded inside HTML
JSP pages are converted to servlets by the web container, so it can do the same thing as Java Servlets.
JSP is easier for creating HTML content but servlets are easier for writing the java code.
A combination of JSP and Servlet can be used to separate the presentation (HTML) and logic (java code) and taking the advantage of both.
It describes phases like conversion, translation, and execution of a Java server page with interactive diagrams. A complete flow diagram (at last) represents various stages i.e., requests from the web client to the response from the webserver.
In Model 1 architecture of JSP,1. User requests for a JSP page.2. JSP performs computations related to the database, calculations, etc.3. JSP gives output and processes it in HTML format.
We need to first understand the Model-View-Controller framework. MVC individualizes data, business logic, and presentation logic, and still keeps them together as if in a container.
Model
• It deals with the business logic of the application and often referred to as an “inner” representation.
• The model contains the data of the application and, in turn, represents the state of the application. It contains classes that are connected to the database.
• The model stores the data given by the user as it is connected to it.
• For example, a chess game may have a model in which the array represents the board, numbers represent pieces and some encoding of the rules.
View
- It is a user interface. It is a presentation layer for the qualified model with little or no programming logic.
- Its work is to read and display the information fetched by the controller. It is HTML, CSS, JSP etc. The user views the UI through it.
Controller
- It acts as an interface between model and view, which recognizes incoming requests. It receives the request from the view layer and passes it to the model layer along with its own computations on the request.
- Again, the controller takes the generated response from the model and passes it to the view layer so that view can show it as output.
Now, let’s talk about the JSP Architecture (how the JSP works). Before we proceed, let us be informed that we use Apache Tomcat for JSP as the Tomcat server has a JSP engine that is needed to process the JSP pages. Below we are discussing the flow of JSP request and response (architectural points) in an ordered manner.
- For a JSP file, the request is usually initiated by the client browser.
- Web Server (here, JSP Engine) loads the JSP file and translates the same to generate a Java Code which will be considered as Servlet.
- When the Servlet(Java Code) is generated, the JSP Engine compiles the Servlet, and compilation errors are detected(if any) in this phase.
- After compilation, the Container loads the servlet class and executes it.
- After the execution, the JSP Engine sends the response back to the client.
Below is the pictorial representation of a JSP page on how it’s served/processed through the server:
Basically, JSP (Java Server Pages) is part of a 3-tier architecture where a Server (usually referred to as an Application Server or Web Server) supports the Java Server Pages (.jsp pages). This server acts as a mediator between the client system browser and a database as shown in the pictorial diagram.
JSP Life Cycle Phases
The following methods will be generated by the web container when translating the JSP to the Servlet Java file.
jspInit() - The web container calls the jspInit() to initialize the servlet instance generated. It is invoked before servicing the client request and invoke only once for a servlet instance.
_jspservice() - The container calls the jspservice() for each user request, passing it the request and the response objects.
jspDestroy() - The container calls this when it decides to take the instance out of service. It is the last method called in the servlet instance.
Lend a Hand: JSP Jump Start
Here we will create a web application with a simple JSP page that prints a message “Welcome to JSP”.
This Demo is meant for understanding the following things
How to deploy a JSP application?
What happens to the JSP when it is deployed?
How to call the JSP page from the browser?
Note: We will not be covering the details of the JSP components which will be covered in the coming slides. Demo just meant to see a basic view of a JSP page.
Lend a Hand: Lets Start Development
Steps for Development
Step 1: Open SDE and Create a dynamic Web project
Step 2: Create an index.jsp
- Open SDE and Create a Dynamic web project named “JSPDemo”
- Right-click web content -> Click new -> Click other -> Click web Select JSP.
- Enter the file name as an index.jsp
- Enter finish and finish the process
- Code of index.jsp
Right-click the application and run it using the option
Run As-> Run on Server
Call index. jsp from the browser
http://localhost:5000/JSPDemo/index.jsp
The JSP which you had created would have been converted to a servlet file and compiled as a class for servicing users request.
You can find this file in the webserver folder where the applications are deployed.
The web server creates a temporary folder for extracting these files.
NOTE: The folder path varies between web servers.
Let's see how our generated java file of the index.jsp looks like.
The service, init and destroy methods generated by the web container.
The service, init and destroy methods generated by the web container.
CHAPTER 2: BASIC INTRODUCTION
What is a java bean?What is a JSP action tag?
Type of JSP action tags.
Comments
Post a Comment