JSP is also referred as Java server pages. It is a language used for web development and is similar to PHP or ASP. It is used extensively for dynamic web page generation. JSP was first released in 1999 by Sun Microsystems.
It is developed alongside HTML and other XML document types. While HTML is suitable to generate static web pages, JSP, on the other hand, is suitable to create dynamic web pages. The JSP files have an extension .jsp. It controls the content of web pages using servlets.
With JSP you can easily add Java code within your HTML based script. It is a server-side technology. JSP is interlinked with Java APIs and can access all the APIs available in java development.
Basic prerequisites for JSP development:
Since JSP is used for web development, any prior knowledge about web development is beneficial. This includes understanding concepts such as HTTP, Web server and application server. To get started with JSP you would require java installation. This could be any latest JDK version such as JDK 8.0.
Additionally, since this requires a web server, you would also need a compatible web server which supports servlet container. Few of the commonly used servers are Apache Tomcat, Jetty or GlassFish.
JSP is closely associated with servlets and hence a basic understanding of servlets would be required. In most cases, the first step is to learn servlets and then start with JSP. Another important aspect is the knowledge of java coding. Since JSP uses Java extensively, you would require to know basic java programming constructs.
Background of JSP?
Post basic HTML, CGI also known as Common Gateway Interface was the only tool available for dynamic web content. CGI had its own downsides which eventually led to a more sophisticated specification such as Java servlets. While Java servlets are scalable and provide good performance, it has few downsides.
One of the main issues with java servlets is its complexity in coding. It embeds HTML directly into the java code. This makes coding difficult and is tightly coupled with the business logic. As an enhancement to servlets, JSP was introduced.
JSP allows developers to add java code embedded within HTML constructs. It supports scripting as well as an element based dynamic content generation. A developer can create custom tag libraries based on their requirement. On compilation, internally a JSP page is converted to a servlet.
In most implementations, JSP is used in combination with servlets where the servlets are responsible for the business logic. JSP forms an integral part of J2EE, enterprise-class applications.
While there have been more advanced frameworks such as Struts, Spring in the market, still JSP remains an integral part of Java web development. As a thumb rule, choose JSP when you are more familiar with Java as a programming language and have worked with servlets.
JSP has been around since December 1999 (JSP v1.1), and the JSP v2.0 is released in November 2003.
One can easily check the JSP version and other details by embedding the below-mentioned code.
- Server Information: <%= application.getServerInfo() %>
- Servlet Specification: <%= application.getMajorVersion() %>.<%= application.getMinorVersion() %>
- JSP version: <%= JspFactory.getDefaultFactory().getEngineInfo().get SpecificationVersion() %>
JSP contains scriptlet which can contain a number of Java code. The syntax for scriptlet is <% code fragment %>.
In JSP you can also add declarations. The syntax for declaration is <%! Declaration fragment %>. One such example is - <%! int count = 0; %>
JSP expression can be provided using syntax - <%= expression %>. An example for JSP expression is - <%= (new java.util.Date()).toLocaleString()%>
A JSP comment is given as <%-- JSP comment --%>. This will be ignored by the JSP compiler. In the same page you can also have HTML comments given by syntax . This will be ignored by the browser.
JSP directives impact the structure of the servlet class. This can be defined using syntax <%@ directive attribute=value%>. There are 3 directives- Page, Include and Taglib. One such example is - <%@ page language="java"%>
While these are basic constructs, JSP is more flexible and has several other advance constructs which also includes Expression Language commonly referred as EL.