简体   繁体   English

当我们有可用的 java 文件时,为什么我们需要 JSP 文件?

[英]Why do we need JSP files when we have .java files available?

I am an absolute beginner at JSP and Servlet Web development thus I am struggling a little with some conceptual things.我是 JSP 和 Servlet Web 开发的绝对初学者,因此我在一些概念性的事情上有点挣扎。

1) I don't really understand why we need to have pure Java code files which we compile but also have separate JSP files... Don't they both end up implementing logic/functions in Java? 1)我真的不明白为什么我们需要有我们编译的纯 Java 代码文件,但也有单独的 JSP 文件......他们最终不会在 ZD52387880E13EA2817AZ72 中实现逻辑/功能吗?

2) How do you determine which functions/operations you should include in the java file and which you should include in the JSP files...? 2) 您如何确定哪些功能/操作应包含在 java 文件中,哪些应包含在 JSP 文件中...?

3) Lastly, there is HTML code in both the Java and JSP file (although in the Java file we are dynamically printing the HTML code. But doesn't this result in repeat HTML code? 3) Lastly, there is HTML code in both the Java and JSP file (although in the Java file we are dynamically printing the HTML code. But doesn't this result in repeat HTML code?

1) I don't really understand why we need to have pure Java code files which we compile but also have separate JSP files... Don't they both end up implementing logic/functions in Java? 1)我真的不明白为什么我们需要有我们编译的纯 Java 代码文件,但也有单独的 JSP 文件......他们最终不会在 ZD52387880E13EA2817AZ72 中实现逻辑/功能吗?

I think the pure Java code files you mean is the Servlet.In fact, You can write the whole web application only using the Servlet, but you will find the following downside in your application:我认为你所说的纯Java代码文件是Servlet。实际上,你可以只使用Servlet编写整个web应用程序,但是你会在你的应用程序中发现以下缺点:

  • HTML is generated using the println() , which means that if want to make some changes to the HTML, the java code has to be changed, recompiled and then redeployed.It results in the maintenance problem. HTML 是使用println()生成的,也就是说如果要对 HTML 做一些更改,java 代码必须更改,重新编译,然后重新部署。

  • The java code is hard to understand for the web page designer. java 代码对于 web 页面设计器来说很难理解。

  • Weak separation between the contents and the presentation as every things are expressed in the Java code.内容和表示之间的分离很弱,因为每件事都在 Java 代码中表示。

JSP is designed to address these downsides: JSP 旨在解决这些缺点:

  • You don't have to re-compile the JSP to reflect the changes.您不必重新编译 JSP 以反映更改。 Just update and save the JSP, the web container will automatically recompile the code when the JSP is retrieved in the next request.只需更新并保存 JSP,web 容器会在下次请求检索到 JSP 时自动重新编译代码。

  • JSP pages look very similar to HTML pages, it is a lot easier for Web page designers to work with JSP pages instead of Servlet. JSP pages look very similar to HTML pages, it is a lot easier for Web page designers to work with JSP pages instead of Servlet.

  • JSP helps separate between the presentation and business logic code, in which the presentation can be expressed in the JSP while the business logic can be implemented in the form of Java Beans or custom tags.This separation will result in a better maintainability. JSP 有助于分离表示和业务逻辑代码,其中表示可以用 JSP 表示,而业务逻辑可以用 ZD52387880E1EA22817A72D3759213819 的形式实现,从而更好地维护 Bean 或自定义标签。

2) How do you determine which functions/operations you should include in the java file and which you should include in the JSP files...? 2) 您如何确定哪些功能/操作应包含在 java 文件中,哪些应包含在 JSP 文件中...?

The strength of JSP is displaying HTML.You should use JSP to define the view to present the data to the user. JSP 的强度是显示 HTML。您应该使用 JSP 定义视图以将数据呈现给用户。

Servlet's strength is in the area of controlling and dispatching.For example, if you want to display different contents depending on some input parameters or some business logic processing,you should do this dispatching work and choose which JSP to display the result in the Servlet. Servlet的强项是在控制和调度方面。例如,如果你想根据一些输入参数或一些业务逻辑处理显示不同的内容,你应该做这个调度工作,并选择JSP在Servlet中显示结果。

If the request 's output does not contain HTML, for example generating a graph or downloading a file, you should do it in the Servlet.如果请求的 output 不包含 HTML,例如生成图形或下载文件,则应在 Servlet 中进行。

So, from the MVC pattern point 's of the view, Servlet plays the role of Controller while JSP plays the role of View.所以,从MVC模式来看,Servlet扮演Controller的角色,而JSP扮演View的角色。

You can found the best practices of using Servlets and JSP here:http://www.oracle.com/technetwork/articles/javase/servlets-jsp-140445.html You can found the best practices of using Servlets and JSP here:http://www.oracle.com/technetwork/articles/javase/servlets-jsp-140445.html

An explanation of java servlets vs. JSP can be found here: java servlets 与 JSP 的解释可以在这里找到:

http://www.oxxus.net/tutorials/jsp/why-jsp http://www.oxxus.net/tutorials/jsp/why-jsp

Most importantly最重要的是

JSP enables a clean separation of business logic from presentation. JSP 实现了业务逻辑与表示的清晰分离。 JSP, by using Java as the scripting language, is not limited to a specific vendor platform. JSP,通过使用 Java 作为脚本语言,不限于特定的供应商平台。 JSP, as an integral part of the J2EE architecture, has full access to server-side resources. JSP 作为 J2EE 架构的一个组成部分,可以完全访问服务器端资源。

2: If the function is only going to be called by that JSP, go ahead and throw it in there. 2:如果 function 只会被那个 JSP 调用,go 把它扔进去。 In general you should just be calling out to the java's from the JSPs and not leaving too much code in them.一般来说,您应该只是从 JSP 中调用 java,而不是在其中留下太多代码。 Building the functions into the.java's and calling them from the JSPs will promote code reuse and make you think more carefully about how you implement those functions.将函数构建到 .java 中并从 JSP 中调用它们将促进代码重用,并使您更仔细地考虑如何实现这些函数。

3: Try to avoid mixing presentation (HTML, and otherwise) with the business logic (Java). 3:尽量避免将表示(HTML 等)与业务逻辑(Java)混合。

doesn't this result in repeat html code?这不会导致重复 html 代码吗?

It does, don't do that.会的,不要那样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM