jspservletsweb-container

JSP as view technology?


I have some doubt on JSP as a view technology. As i understand JSP eventually gets converted to a Servlet (by the web container if I have read correct documentation).

Based on this I have following questions:

  1. If JSP gets converted to a Servlet, then how come it is a view technology?

  2. Is it possible to write GUI elements (like button etc.) using plain Servlets? Personally I haven't seen any Servlet code which has GUI elements, then how come JSP (which has GUI elements) gets converted to Servlet (and where does those GUI elements gets translated to?).

Can anyone help me understand this? I have been keeping this doubt and searching on net I am not able to clear it.


Solution

  • JSP is a templating (or view) technology.

    JSP-files are compiled to Servlet classes at runtime, which return the contents of the JSP-file to the HTTPServletResponse's writer. It is a convenient way of getting the result of:

    response.getWriter().println("<html>"); response.getWriter().println("<head>"); response.getWriter().println("<title>foobar</title>");

    etc. in addition to optional Java-code, embedded inside the JSP-file. So it's a little like a PHP-flavor for Java, if you want.

    It's not super-clean MVC-separation if you don't work too careful and add Java-code to your HTML.

    You can output GUI-Elements in pure Servlets like I've written above (or with more elegant code). A JSP renderer is a cleaner MVC approach, though.

    see: Java Server Pages at Wikipedia