JSP Tutorials for Beginners and Professionals
In this JSP Tutorials for Beginners and Professionals article series, we are going to discussed all the basic, intermediate, and advanced concepts of JSP with simple as well as real-time examples.
This JSP tutorial is for whom?
This JSP tutorial is designed for beginners, students, and professional java developers who want to learn and enhance their JSP skills step by step from scratch. These tutorials provide a hands-on approach to the subject with step-by-step program examples that will assist you to learn and put the acquired knowledge of JSP into practice.
Java Server Pages (JSP) technology is that the Java platform technology for delivering dynamic content to web clients in a portable, secure, and well-defined way. The Java Server Pages specification extends the Java Servlet API to supply web application developers with a strong framework for creating a dynamic web page on the server using HTML, and XML templates, and Java code, which is secure, fast, and independent of server platforms. JSP has been built on top of the Servlet API and utilizes Servlet semantics. JSP has become the well-liked request handler and response mechanism. Although JSP technology goes to be a strong successor to basic Servlets, they need an evolutionary relationship and may be utilized in a cooperative and complementary manner.
In the initial days of servlets, sun Microsystems failed to attract ASP programmers towards servlet. The reasons are:
- ASP supports tag-based programming, servlet doesn’t support it.
- To work with servlet string knowledge is required. It is very difficult for an ASP programmer to learn Java.
To overcome the above problems Sun Microsystems has given a tag-based technology called JSP having all the features of Servlet. So, any programmer can use JSP without having strong knowledge of Java/Servlet.
Advantages of JSP over Servlet
JSP technology was designed to simplify the process of creating pages. It simplifies the process by separating web presentations from web content. JSP is a collection of HTML tags and JSP tags and Java code. Every JSP page is saved with extension .jsp. This JSP can be placed in the root folder or WEB-INF. The JSP placed in the root folder is a public resource and this resource can access by the client directly. The JSP placed inside WEB-INF is a private resource, which can’t access directly by the client. Client access this resource using public URL defined inside web.xml.
Servlets are powerful and sometimes they’re a touch cumbersome when it involves generating complex HTML. Most servlets contain a touch code that handles application logic and tons of more code that handles output formatting. This can make it difficult to separate and reuse portions of the code when a special output format is required. For these reasons, web application developers turn towards JSP as their preferred servlet environment.
Difference Between JSP and ASP:
- JSP stands for Java Server Pages is a technology introduced by Sun microsystems whereas ASP stands for Active Server Pages is a technology introduced by Microsoft.
- ASP pages can connect to MYSQL and MS Access databases and also can connect to other databases with the help of ADO whereas JSP can connect to any databases loading the appropriate driver when needed.
- JSP is a technology that helps the developers to implement and develop dynamic web pages, whereas ASP is Microsoft’s server-side script engine for dynamically generated web pages.
- With respect to performance perspective, JSP code runs faster than ASP.
- JSP is mainly used by Apache Tomcat Web Server and mainly implemented on Linux based web server and also runs on JBoss and IBM application server, whereas ASP is mainly based on Microsoft IIS server and is implemented on windows-based systems.
- JSP is free of cost whereas ASP is not free and requires a proper license for usage.
- JSP mainly works with Java Security Model whereas ASP is mainly based on the Windows NT Security Architecture Model.
Difference Between JSP and Servlets:
- If we perform any modifications on the existed Servlets then we have to perform recompiling and reloading on to the server explicitly whereas if we perform any modifications on the existed JSP then it is not required to perform recompilation and reloading because JSP pages are auto compiled and autoloaded.
- In JSP business logic is isolated from presentation logic by using JavaBeans, whereas in Servlet we have to execute everything like business logic and presentation logic in one servlet file.
- Session Management is automatically enabled in JSP, whereas in Servlet, by default session management is disabled, the user needs to enable it explicitly.
- From a performance perspective, JSP is slower than Servlet because the initial step is the translation of JSP to java code in the JSP lifecycle and then compilation.
- In JSP, packages can be imported anywhere top, middle and bottom, whereas, in Servlet, every package needs to be imported on top of the Servlet.
- JSP is used for displaying output data whereas Servlet plays a controller role in the MVC pattern.
Difference Between JSP and SSI:
- JSP is a Java standard technology that enables you to write dynamic, data-driven pages for your Java web applications whereas SSI is a simple server-side scripting language exclusively for the World Wide Web.
- JSP uses servlet instead of a separate program to generate dynamic part as well as JSP allows for richer processing of form data. SSI invokes an existing servlet and its output is inserted whole into a static page.
- JSP is executed by the container’s JSP servlet whereas SSI is executed by the webserver engine.
- JSP is executed only once during JSP compile time whereas SSI is executed on every request.
- JSP works in JSP files only whereas SSI works in non-JSP files as well.
Difference Between JSP and HTML:
- JSP is a technology that helps software developers to create dynamically generated web pages based on HTML whereas HTML is the standard markup language for creating web pages and web applications.
- JSP can generate dynamic content whereas HTML can generate static content only.
- JSP can access server-side resources whereas HTML can access client-side resources.
- HTML does not allow to place java code inside HTML pages whereas JSP allows placing Java code inside JSP pages.
- HTML also does not allow to place a custom tag or third-party tag whereas JSP allows placing a custom tag or third-party tag.
- HTML provides emphasis on appearance, semantics, and layout in the browser whereas JSP can invoke built-in functionality from the server.
- HTML runs in a web browser whereas JSP runs directly on the Web Server and local JVM.
- HTML provides a means to describe the structure of text-based information whereas JSP provides a dynamic interface for continuously changing data and dynamically invokes server actions.
JSP Application design with MVC
MVC was first described by Xerox in the late 1980s. The purpose of using MVC is to separate components into three distinct units: The Model, the View, and the Controller. This is a server-side implementation of a popular MVC design pattern.
The Model is the separation of the way application data is modeled; this is the data layer which consists of the business logic of the system. It consists of all the data of the application and represents the state of the application.
The View is from the way it is presented. It is a presentation Layer that normally presents the UI of the application. It is used to display the data fetched from the controller. It shows the data on the UI of the application.
The Controller is a separate component to handle the processing in between. It acts as an interface between the View Layer and the Model Layer. It receives the request from View Layer and processes it and also does the necessary validation for the request. It intercepts all the requests from the view layer.
In a server application, we commonly classify the parts of the appliance as business logic, presentation, and request processing. Business logic is that the term used for the manipulation of an application’s data, i.e., customer, product, and order information. Presentation refers to how the appliance is shown to the user, i.e., the position, font, and size. And finally, request processing is what ties the business logic and presentation parts together. In MVC terms, the Model corresponds to business logic and data, the View to the presentation logic, and therefore the Controller to the request processing.
What you will learn from these JSP Tutorials?
- What is JavaServer Pages?
- Problem with Servlet
- Why JSP pages are more advantageous than Servlet?
- Why Use JSP?
- Advantages and Disadvantages of JSP
- Features of JSP
- Creating a simple JSP Page
- Process of Execution
- How to run a simple JSP Page?
- Do I need to follow the directory structure to run a simple JSP?
- The Directory structure of JSP
- The architecture of a JSP Application
- What is a JSP container?
- Setting up the Java Development Kit
- Setting up Web Server: Tomcat
- Setting up CLASSPATH
WORKING WITH IDE
- javax.servlet.jsp package
- The JspPage interface
- Methods of JspPage interface
- The HttpJspPage interface
- Method of HttpJspPage interface
- Classes of JSP API
- The Erro Data Class
- Methods of Erro Data Class
- The JspWriter Class
- Methods of JspWriter Class
- The PageContext Class
- Methods of PageContext Class
- How JSP Work
- Model-View-Controller Framework
- Explain about Model, View, and Controller
- Advantages of MVC
- Example of MVC in JSP
- JSP Architecture or JSP Processing and its flow in detail
- What is JSP Life Cycle?
- Different Phases of JSP Life Cycle.
- JSP Life Cycle Flow of Execution
- Different Methods of JSP Life Cycle
- Difference between a Web Server, a Web Container, and an Application Server
- What are JSP Elements?
- JSP Directives, Actions, and Scripting Elements
- JSP Declarations with Examples
- JSP Scriptlets with Examples
- JSP Expressions with Examples
- What are Directives in JSP?
- The page Directive
- Attributes of JSP page directive
- Example of each JSP page directive Attributes
- The include Directive
- Advantage of Include directive
- Example of the include directive
- The JSP Taglib Directive
- Example of JSP Taglib directive
- XML Based Comments
- JSP Based Comments
- Java-Based Comments
- Scope of JSP Objects
- Type of Scopes in JSP
- JSP Page Scope with Example
- Advantages and Disadvantages of JSP Page Scope
- JSP Request Scope with Example
- Advantages and Disadvantages of JSP Request Scope
- JSP Session Scope with Example
- Advantages and Disadvantages of JSP Session Scope
- JSP Application Scope with Example
- Advantages and Disadvantages of JSP Application Scope
- What are JSP Implicit Objects?
- Types of Implicit Objects in JSP?
- Request Object, its Methods, and example of Request Object
- Response Object, its Methods, and example of Response Object
- Out Object, its Methods, and example of Out Object
- Session Object, its Methods, and example of Session Object
- Application Object, its Methods, and example of Application Object
- Config Object, its Methods, and example of Config Object
- PageContext Object, its Methods, and example of PageContext Object
- Page Object, its Methods, and example of Page Object
- Exception Object, its Methods, and example of Exception Object
JSP Actions: Tags, Syntax, and Examples
- What are JSP actions?
- Why do we need JSP Actions?
- How many standard Action Tags are available in JSP?
- Built-in JSP actions
The <jsp:useBean> Action
- What are <jsp:useBean> Action
- When and how to use <jsp:useBean> Action
- Advantages of UseBean actions
The <jsp:include> Action
- What are <jsp:include> Action
- When and how to use <jsp:include> Action
- Advantages of JSP Include actions
The <jsp:setProperty> Action
- What are jsp:setProperty> Action
- When and how to use jsp:setProperty> Action
- Advantages of SetProperty actions
The <jsp:getProperty> Action
- What are <jsp:getProperty> Action
- When and how to use <jsp:getProperty> Action
- Advantages of getProperty actions
The <jsp:forward> Action
- What are <jsp:forward> Action
- When and how to use <jsp:forward> Action
- Advantages of forward actions
The <jsp:plugin> Action
- What are <jsp:plugin> Action
- When and how to use <jsp:plugin> Action
- Advantages of plugin actions
The <jsp:attribute> Action
- What are <jsp:attribute> Action
- When and how to use <jsp:attribute> Action
- Advantages of attribute actions
The <jsp:text> Action
- What are <jsp:text> Action
- When and how to use <jsp:text> Action
- Advantages of text actions
The <jsp:param> Action
- What are <jsp:param> Action
- When and how to use <jsp:param> Action
- Advantages of param actions
The <jsp:fallback> Action
- What are <jsp:fallback> Action
- When and how to use <jsp:fallback> Action
- Advantages of fallback actions
The <jsp:body> Action
- What are <jsp:body> Action
- When and how to use <jsp:body> Action
- Advantages of body actions
The <jsp:output> Action
- What are <jsp:output> Action
- When and how to use <jsp:output> Action
- Advantages of output actions
JSP – Client Request
- What is JSP Client Request?
- Understanding Request Headers (Accept, Accept-Charset, Accept-Encoding, Accept-Language, Authorization, Connection, Content-Length, Cookie, Host, If-Modified-Since, If-Unmodified-Since, Referer, User-Agent)
- The HttpServletRequest Class and its Method
- HTTP Header Request Example
JSP – Server Response
- What is JSP Server Response?
- Understanding Response Headers (Allow, Cache-Control, Connection, Content-Disposition, Content-Encoding, Content-Language, Content-Length, Content-Type, Expires, Last-Modified, Location, Refresh, Retry-After, Set-Cookie)
- The HttpServletResponse class and its Method
- HTTP Header Response Example
JSP – Http Status Codes
- What are JSP HTTP Status Codes?
- Understanding the HTTP Status Codes
- Methods to Set HTTP Status Code
- HTTP Status Code Example
JSP – Form Processing
- What is Form Processing in JSP?
- Methods in Form Processing
- Understanding the GET and POST methods
- Reading Form Data using JSP
- GET Method Example Using URL
- GET Method Example Using Form
- POST-Method Example Using Form
- Passing Form Data to JSP Program
- Reading All Form Parameters
JSP – Filters
- What is JSP Filter?
- Where Do We Use Filters?
- How does JSP Filter work?
- What is the life cycle of a JSP filter?
- The need for JSP Filters
- Advantages of Filter
- JSP Filter Interface / What are the interfaces/methods?
- JSP Filter Life cycle
- JSP Filter Configuration
- Using Multiple Filters
- JSP Filter Ordering
- JSP Filter Examples
- How filter can intercept the request
- Write a filter example to read all the init parameters and display them on screen
- Filter Chaining
Type of Filtering
- Filter of Request
- Filter of Response
Types of Filters in JSP / What are the different filters?
- Authentication filters with Examples
- Data compression filters with Examples
- Encryption filters with Examples
- MIME chain filters with Examples
- Logging Filters with Examples
- Tokenizing filters with Examples
JSP – Session Tracking
- Overview of JSP Session tracking
- What is a Session?
- Session Management in Java
- What is Session Tracking in JSP?
- Need Of Session Tracking
- Session Tracking Techniques
- Hidden Form Fields
- URL Rewriting
- The Session Object
- How to get a Session object
- Methods of the session object
- JSP Session Tracking Examples
- ‘Printing session info
- JSP session invalidate
- Deleting a JSP Session
- Browser Session vs. Server Session
- Store Attributes in Session object wisely
JSP – Cookies Handling
- Overview of JSP Cookies Handling
- What are Cookies?
- How Cookie works
- Types of Cookie
- Non-persistent cookie
- Persistent cookie
- Advantage and Disadvantage of Cookies
- How to Handle Cookie in JSP?
- JSP Cookies API
- Servlet Cookies Methods
- Restricted characters in cookies
- How to Set or Create Cookies with JSP / Sending Cookies to Client
- Set Cookies Example with JSP
- How to Read Cookies with JSP
- How to Delete Cookies with JSP
- Delete Cookies Example with JSP
- Username and Password Functionality using Cookies example
JSP – File Uploading and Downloading
- Overview of JSP File Uploading & Downloading
- Uploading a file to the server using JSP
- Understanding MultipartRequest class and its methods
- JSP File Upload
- Example: Using Action
- Example: Using JSP operations
- Downloading File
- Example of File Downloading
JSP – Handling Date
- Methods & Description
- Getting Current Date and Time
- Date Comparison
- Date Formatting using SimpleDateFormat
- Simple DateFormat Format Codes
JSP – Standard Tag Library (JSTL)
- What is the Standard Tag Library (JSTL) in JSP?
- Features of JSTL
- Why we need the Standard Tag Library (JSTL)?
- Advantage of JSTL
- How to Install the JSTL Library?
- Classification of The JSTL Tags
- Core Tags
- Formatting Tags
- SQL Tags
- XML tags
- JSTL Functions
- JSTL Core Tags Examples:
- JSP Custom Tags
- Advantages of custom tags in JSP
- JSP Tag Interface
- Method of Tag Interface
JSP – Database Access
- Overview of JSP Database Access
- What is JDBC?
- Why we need JDBC?
- Steps to use JDBC.
- MySQL installation
- Database Creation
- Create Table
- Create Data Records
- SELECT Operation
- INSERT Operation
- UPDATE Operation
- DELETE Operation
- CRUD Operations using Stored Procedure
JSP – XML Data
- Overview of XML
- What is XML?
- Why we need XML?
- XML versus HTML
- Advantages of using XML.
- How to Create XML?
- Presenting XML Documents
- Sending XML from a JSP with Example
- Processing XML in JSP with Example
- Formatting XML with JSP with Example
- Generating XML from JSP with Example
- Generating XML from JSP and JavaBeans
- Converting XML to Server-Side Objects
- Simple API for XML (SAX)
- Document Object Model (DOM)
- Transforming XML
JSP – JavaBeans
- What is JavaBean?
- Why do we need to use JavaBean?
- How to access the JavaBean class?
- How JavaBean works with diagrams?
- How To Use JavaBeans in JSP?
- Advantages and Disadvantages of JavaBean
- JavaBeans Properties
- JavaBeans Example
- Accessing JavaBeans
- Accessing JavaBeans Properties
- JavaBeans Components
- JavaBeans Component Design Conventions
- Creating and Using a JavaBeans Component
- Setting JavaBeans Component Properties
- Retrieving JavaBeans Component Properties
- Taglib Directives
- TLD File
- Tag Handler Class
- TagLibrary types
Different Types of Tags and its Life Cycle
- Body Tag
- Simple Tag
- Nested Tag
- Iteration Tag
- Custom tags
JSP – Custom Tags
- What are Custom Tags in JSP?
- What Can You Do with Custom Tags?
- Advantages of Custom Tags
- Syntax to use custom tag
- Creating a Custom Tag
- Accessing the Tag Body
- Custom Tag Attributes
- JSP Custom Tag API
- JspTag interface
- Tag interface
- Fields of Tag interface
- Methods of Tag interface
- IterationTag interface
- Field of IterationTag interface
- Method of Tag interface
- TagSupport class
- Example of JSP Custom Tag
JSP – Expression Language (EL)
- What is Expression Language (EL) in JSP?
- Why we need Expression Language (EL) in JSP?
- JSP Syntax of Expression Language (EL)
- Implicit Objects in Expression Language (EL)
- EL param example
- EL sessionScope example
- EL cookie example
- Operators in EL
- Precedence of Operators in EL
- Reserve words in EL
- Functions in JSP EL
- JSP EL Implicit Objects
- The pageContext Object
- The Scope Objects
- The param and param Values Objects
- header and header Values Objects
- Flow Control Statements:
- Decision-Making statements
- Loop Statements
JSP – Exception Handling
- What is JSP Exception?
- Exception Handling in JSP
- Checked exceptions
- Runtime exceptions
- Methods of handling exceptions:
- Exception handling using exception implicit object
- Exception handling using try-catch blocks within scriptlets
- Using JSTL tags for Error Page
- JSP Error Page
- JSP Error Page Configuration
- Error Page Deployment Descriptor Configuration
- Example of exception handling in JSP by the elements of page directive
- Example of exception handling in JSP by specifying the error-page element in web.xml file
- Catching exceptions directly on the JSP page
- Page-level exception handling
- Application-level exception handling
JSP – Debugging
- Using System.out.println()
- Using the JDB Logger
- Debugging Tools
- Using JDB Debugger
- Using Comments
- Client and Server Headers
JSP – Security
- Overview of JSP Security
- Authentication vs Authorization
- Authentication Mechanisms
- Basic Authentication with an Example
- Role-Based Authentication with an Example
- Form-Based Authentication with an Example
- Token-Based Authentication with an Example
- Programmatic Security in a Servlet/JSP with an Example
JSP – Internationalization| i18n| l10n
- Detecting Locale
- Languages Setting
- Locale Specific Dates
- Locale Specific Currency
- Locale Specific Percentage
MVC in JSP
- What is MVC Architecture
- Advantage of MVC Architecture
- MVC Example in JSP
JUnit in JSP
- JUnit: What and Why?
- Types of Testing
- Annotations used in JUnit
- Assert class
- Test Cases
- Maven: What and Why?
- Ant Vs Maven
- How to install Maven?
- Maven Repository
- Understanding pom.xml
- Maven Example
- Maven Web App Example
- Maven using Eclipse
JSP Real-time Examples
- Registration & Login Form Example
- Page Redirecting Example in JSP
- Hits Counter Example in JDP and reset
- Auto Page Refresh Example
- File uploading and downloading Example
- Sending Email Example (Send an HTML Email, Send Attachment in Email, Using Forms to Send Email)
- Database CRUD Operations example using Stored Procedure
JSP Interview Questions and Answers
What do we expect from you?
We will do our level best to cover all the JSP concepts, but in the meantime, if you have any specific concept in your mind that you want us to cover, then please leave it as a comment on the comment box, and we will definitely discuss that concept(s) in this JSP course.
Finally, your valuable feedback is very important and means a lot to us. So, if you have a few minutes, then please let us know your thoughts and feedback on this JSP Tutorials course.