Introduction to JSP
JSP is a server side technology that does all the processing at server. It is used for creating dynamic web applications, using java as programming language.
Basically, any html file can be converted to JSP file by just changing the file extension from “.html” to “.jsp”, it would run just fine. What differentiates JSP from HTML is the ability to use java code inside HTML. In JSP, you can embed Java code in HTML using JSP tags. for e.g. run the code below, every time you run this, it would display the current time. That is what makes this code dynamic.
<HTML> <BODY> Hello BeginnersBook Readers! Current time is: <%= new java.util.Date() %> </BODY> </HTML>
Your First JSP
Let’s start learning JSP with a simple JSP.
<%-- JSP comment --%> <HTML> <HEAD> <TITLE>MESSAGE</TITLE> </HEAD> <BODY> <%out.print("Hello, Sample JSP code");%> </BODY> </HTML>
The above JSP generates the following output:
Hello, Sample JSP code.
Explanation of above code
1) The line <%–JSP Comment–%> represents the JSP element called JSP Comment, While adding comments to a JSP page you can use this tag, we will discuss this in detail in coming posts.
Note: JSP Comments must starts with a tag <%– and ends with –%>
2) Head, Title and Body tags are HTML tags – They are HTML tags, frequently used for static web pages. Whatever content they have is delivered to client(Web browser) as such.
3) <%out.print(“ Hello, Sample JSP code ”);%> is a JSP element, which is known as Scriptlet. Scriptlets can contain Java codes. syntax of scriptlet is: <%Executable java code%>. As the code in Scriptlets is java statement, they must end with a semicolon(;). out.print(“ Hello, Sample JSP code ”) is a java statement, which prints“ Hello, Sample JSP code”.
As discussed, JSP is used for creating dynamic webpages. Dynamic webpages are usually a mix of static & dynamic content.
The static content can have text-based formats such as HTML, XML etc and the dynamic content is generated by JSP tags using java code inside HTML .
Servlet Vs JSP
Like JSP, Servlets are also used for generating dynamic webpages. Here is the comparison between them.
The major difference between them is that servlet adds HTML code inside java while JSP adds java code inside HTML. There are few other noticeable points that are as follows:
- Servlet is a Java program which supports HTML tags too.
- Generally used for developing business layer(the complex computational code) of an enterprise application.
- Servlets are created and maintained by Java developers.
- JSP program is a HTML code which supports java statements too.To be more precise, JSP embed java in html using JSP tags.
- Used for developing presentation layer of an enterprise application
- Frequently used for designing websites and used by web developers.
Advantages of JSP
- JSP has all the advantages of servlet, like: Better performance than CGI Built in session features, it also inherits the the features of java technology like – multithreading, exception handling, Database connectivity,etc.
- JSP Enables the separation of content generation from content presentation. Which makes it more flexible.
- With the JSP, it is now easy for web designers to show case the information what is needed.
- Web Application Programmers can concentrate on how to process/build the information.