2

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.

4

2 回答 2

2

JSP 是一种模板(或视图)技术。

JSP 文件在运行时被编译为 Servlet 类,它将 JSP 文件的内容返回给HTTPServletResponse's writer。这是获得以下结果的便捷方法:

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

等等,除了可选的 Java 代码,嵌入在 JSP 文件中。因此,如果您愿意,它有点像 Java 的 PHP 风格。

如果您不小心工作并将 Java 代码添加到您的 HTML 中,这不是超级干净的 MVC 分离。

您可以像我上面写的那样(或者使用更优雅的代码)在纯 Servlet 中输出 GUI 元素。不过,JSP 渲染器是一种更简洁的 MVC 方法。

请参阅:维基百科上的 Java 服务器页面

于 2016-07-22T17:40:28.497 回答
0

您可以将所有 MVC(模型、视图、控制器)部分写入 JSP。但它不实用也不安全。

如果你想使用 JSP,我建议你使用Spring MVCSpring SecurityJSTL和 CSS Framework(Bootstrap等)。

这种结构对 JSP 非常有用。您可以使用 servlet、hibernate、更多的 GUI 效果、拦截器和很多 JavaScript 框架。

于 2016-07-23T20:24:47.203 回答