January 17, 2017

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:

  • JSP is written inside HTML file (as a client side code) to perform some logic 
  • 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.

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:
  • Like normal page, your browser sends an HTTP request to the web server.
  • 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
  • The JSP engine converts the JSP page into a Java Servlet. All JSP elements are converted to Java code inside this generated Java Servlet.
  • The generated Java Servlet is then compiled into an executable class.
  • Servlet container loads the servlet class and executes it.
  • During execution, the servlet produces an output in HTML format inside an HTTP Response.
  • The web server forwards the HTTP response (in HTML) to your browser.
  • Finally web browser generates the HTML page inside the HTTP response.
All the above mentioned steps can be shown below in the following diagram


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 https://www.tutorialspoint.com/jsp/jsp_syntax.htm

Try doing this simple project :
http://www.javaknowledge.info/login-and-registration-example-in-jsp-with-session/