Advantages of jsp over servlets.

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

There are numerous articles stating the uses and advantages of jsp over servlets. However, I have seen many people understanding exactly what are the advantages of jsp over servlets. The common questions coming from them are :

  • JSP is ultimately compiled to a servlet only, then why bother creating and writing jsp?
  • We can put HTML code in servlets so why write jsp instead? What are the advantages of jsp over servlets?

Consider a situation where two colleagues are developing a web application using servlets and jsp. Pranil is a java developer and Prasad is a page designer. They are working together and they have a scenario.

  • There is a model class called as User with fields firstName, lastName, username
  • The web application should be able to print username , lastName and firstName in a tabular format.

Now Pranil has a task to create these users. He is thinking, “I will create a list of users and send the list in a request attribute to a JSP. Prasad should be able to handle this user list and display in tabular format“.

So Pranil writes a servlet as follows.

Now Pranil is carefree as he has forwarded the list to Prasad. Now Prasad has task of showing the list of users on web page. Now he doesn’t know how to even retrieve those users. He goes to Pranil for help. Pranil looks into the JSP Prasad is working on and writes something like this.

This produced output :

OutputTable

Prasad is happy to see the output and sees what Pranil has done. He sees the jsp and is terrified. He cannot understand understand anything other than

  • Table tags are written inside some  out.println().  He doesn’t know what out.println() .
  • Something like user.getXXX() . He concludes user properties are accessed with it.

He is scared because java code is written all over his page and if any modification request comes, he won’t be able to do it. He goes to Supriya for help. Supriya is a senior developer. She sees the code and asks Prasad to use JSTL. It contributes to one of the biggest advantages of jsp over servlets. Scriptless JSp pages. JSP without java code. She Writes something like:

This generates the same output as above.

OutputTable

Prasad sees the code and understands it readily. Its not anything he doesn’t understand now. The code is clean and clear. He doesn’t have to deal with java code at all. After all he is a page designer. All he needs to know is the object and its attributes which are accessed in JSP through JSTL.

Finally, he learns:

  • Servlets are capable of generating HTML output but its too tedious task.
  • He is a page designer and not a java developer so he should not write java code.
  • He can simply access objects in JSP as it provides JSTL.

 

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

One thought on “Advantages of jsp over servlets.

Leave a Reply

Your email address will not be published. Required fields are marked *