To send an html page through a servlet, we need to put every html tag in a print statement. Or in other words, we are embedding html within java.
For a small html file, embedding html within java will work, but not for bigger html files. JSP is java’s solution to this problem.
JSP is a java technology developed on top of servlet-api for developing dynamic web applications like servlets, but instead of embedding html within java, in JSPs, we embed java within HTML.
JSPs can be considered as html files where java code is included within html using special elements called scriptlets and is similar to PHP and ASP in syntax.
JSPs are eventually converted to a servlet by the servlet container before execution. Instead of developers writing a print statement for all HTML tags, java will do that behind the scene.
A good understanding of servlets and servlet API is important for writing good, secure JSPs.
A standard JSP page has .jsp extension, but there are other types of JSP pages like JSP documents which doesn't follow this rule.
JSP documents are xml-based JSP pages which are well formed.
JSPs have become more cleaner now as they allow markups and custom tags within pages, reduicing the amount of Java code. Note that in most cases JSPs will be worked on by UI designers and this separation of java code from JSP helps a lot.
Except for the file extension (.jsp) and some extra lines, they look like normal html pages, but with the capability to insert java code at any place within this html file.
JSPs also provide 9 implicit variables which we can use without declaring or initializing. Behind the scene, these variables are declared and initialized while JSPs are converted to servlets.
You can find a simple JSP file below which does not contain any Java code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<h1>This is a simple JSP file</h1>
This is a simple JSP File.
<br> This JSP file does not contain any java code.
You may use only <!DOCTYPE html> if you want to be html5 compliant.