Problems with Java Servlet

Servlets are not useful for generating presentation content such as HTML. Consider the following piece of code.

out.println(”<html>”); out.println(” <head>”);

out.println(”             <title>Hello World Servlet</title>”);

out.println(” </head>”); out.println(” <body>”);

out.println(”             <h1>Hello World!</h1>”);

out.println(” </body>”); out.println(”</html>”);

The purpose of the code is to generate the following HTML code.



<title>Hello World Servlet</title>



<h1>Hello World!</h1>



This is acceptable if you want to generate smaller HTML files. However, if the size of the HTML document is large, it is a tedious process to generate it.

Another drawback of the servlet over Java Server Pages (JSP) is that you have to recompile the source yourself after any modification is done. Moreover, the web server has to be restarted to get the effect of modified code. However, JSP can perform these tasks automatically on our behalf.

Servlets often contain presentation logic as well as processing logic, which makes the code difficult to read, understand and extend. If the presentation logic changes, the servlet code has to be modified, recompiled, and redeployed.

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

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