Intro to Java Server Pages (JSP)

What is JSP?

JavaServer Pages (JSP) is a technology for developing web pages by inserting java code in HTML pages using special JSP tags, most of which start with <% and end with %>.

JSP can be used as part of the front end (client side) as well as the back end (server side). That means:

<ul><li>JSP is written inside HTML file (as a client side code) to perform some logic </li><li>JSP can be used just like Java Servlet (as a server side code) - create connection to database, perform query, perform validation logic and so on.</li></ul>
Why JSP? Why not just use Java Servlet?

JSP vs. Pure Servlets :

It is more convenient to write (and to modify!) regular HTML file than to have plenty of println statements in Java Servlets that generate the HTML response.

However, JSP pages can also be used in combination with Java Servlets that handle the business logic and this is usually the code. Java Servlet is usually required to perform more powerful business logic.

JSP Processing:
<ul><li>Like normal page, your browser sends an HTTP request to the web server.</li><li>The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. This is done by using the file page which ends with .jsp instead of .html i.e. index.jsp instead of index.html</li><li>The JSP engine converts the JSP page into a Java Servlet. All JSP elements are converted to Java code inside this generated Java Servlet.</li><li>The generated Java Servlet is then compiled into an executable class.</li><li>Servlet container loads the servlet class and executes it.</li><li>During execution, the servlet produces an output in HTML format inside an HTTP Response.</li><li>The web server forwards the HTTP response (in HTML) to your browser.</li><li>Finally web browser generates the HTML page inside the HTTP response.</li></ul>All the above mentioned steps can be shown below in the following diagram
<div class="separator" style="clear: both; text-align: center;"></div>

So in a way, a JSP page is really just another way to write a Java Servlet without having to be a Java programming wizard. Except for the translation phase, a JSP page is handled exactly like a regular Java Servlet

Learn JSP Syntax. Proceed to

Try doing this simple project :