Java Server Pages or JSP technology allows us to combine the static HTML with the Java code, which makes a webpage dynamic.
Hence, a JSP page consists of two parts -
Static content, comprising of regular HTML.
Dynamic content, comprising of Java code.
Adding the static content(HTML) to a JSP page doesn't need anything special, but in order to add the dynamic content to a JSP page, we need to use the
special JSP tags(which you will study later).
Like this, JSP allows us to separate the dynamic logic of a webpage from its static presentation logic.
Main features of JSP
Make interactive websites.
One of the main uses of JSP is to make interactive webpages. Webpages that not just static but alive, dynamic and are able to interact with
the user in real time.
Easier to read data from user.
User interacting with JSP controls like textbox, button, dropdown list, checkbox can enter some information. Using JSP,
it is easier to read this information entered by the user and send it to the server. For example, A user entering a form
and clicking the "submit" button. Using JSP, the data entered in a form is read(when the submit button is clicked) and sent to the server side for further processing.
Easier to display server response.
After the data read from client's input is sent to the server, its response is sent back to the client and it is easily displayed. For example, after a user
submits a form, a response from the server is displayed using JSP, such as - "Thanks for filling the form".
Allows to add Java to your website.
As you know JSP stands for Java Server Pages, one of the main feature of JSP is to allow Java code to be added in
between your HTML code, in order to give our webpage the power of Java and make it further powerful and interactive. JSP
page is internally converted to a bytecode java file, hence all the Java features like security, flexibility, platform
independent are available to JSP as well.
Easier to connect to the database.
One of the main features of JSP is to easily allow us to connect our website with the database, so that we can
send the data entered by each user to a database and read it back from database, when required.
Tracking the User.
JSP allows us to track the selections made by the user during user interaction with the website by maintaining the information
in the Session object, Application
object or in the Cookies(storing user specific information on the user's system.
This can be helpful when you need to track one or multiple users or the selections made by a user during the current interaction with the website.
Easy to code.
JSP is mainly about adding Java to your webpage's HTML and it is fairly easy to learn and code. Anyone with a decent
knowledge of Java(and HTML) can easily learn JSP in a very short time.
Why prefer JSP over Servlet?
A JSP webpage is always converted to a Java Servlet. However, creating a dynamic webpage using JSP is easy,
but creating it using Java Servlet is quite a tedious task. You would ask, why? Okay, here's the answer.
To generate a webpage only from a Java Servlet requires some additional files such as -
A Java class comprising only Java code(dynamic logic).
This Java class also contains some HTML code(static logic) embedded within its Java code.
A web.xml file.
This Java class is quite complicated to create for a Web Designer who doesn't know much of Java.
However, for the same Web Designer, it is easier to create a dynamic webpage from a JSP page, which mainly comprises of the HTML code mixed with some Java code(within JSP tags).
You need to recompile your Java Servlet class every time you modify its code to see the change in a dynamic webpage created by it.
While, the same dynamic page created using JSP doesn't need to be compiled, as it's done automatically by Web Container like Tomcat or WebLogic(used to execute JSP pages ).