Java JSP 究竟在哪里运行?在客户端还是服务器端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21295765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where exactly does a JSP runs ? on the client side or server side?
提问by JAN
I've read @BalusC's great answer HEREbut something is still not clear to me :
我在这里阅读了@BalusC 的精彩回答,但我仍然不清楚:
On the one hand , when I write a servlet , I do something like this :
一方面,当我写一个 servlet 时,我会做这样的事情:
String addressPath = "/WEB-INF/results/employee/employeePage.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(addressPath);
dispatcher.forward(request, response);
and then , the user sees the JSP page called employeePage.jsp
on his screen . Doesn't mean that the JSP runs on the client's side ?
然后,用户会employeePage.jsp
在其屏幕上看到调用的 JSP 页面。不是说 JSP 在客户端运行吗?
So the JSP runs on the client's side , or on the server's side (JSP = Java server page) ?
那么 JSP 是运行在客户端还是服务器端(JSP = Java 服务器页面)?
采纳答案by TacticalCoder
The JSP runs on the server-side but it is very common for the JSP to serve, in addition to HTML (and CSS), bits of JavaScript which is then run on the client-side.
JSP 在服务器端运行,但 JSP 服务于 HTML(和 CSS)之外的 JavaScript 位是很常见的,然后在客户端运行。
A very simple example would be a JSP including some Google Analytics tracker (which is in JavaScript) in the webpage served to your visitors.
一个非常简单的示例是在为访问者提供的网页中包含一些 Google Analytics 跟踪器(使用 JavaScript)的 JSP。
Note that I'm not saying that all JavaScript is always run on the client-side: there's also server-side JavaScript. All I'm saying is that JSPs often serves JavaScript and that the JavaScript served by JSPs is run on the client-side.
请注意,我并不是说所有 JavaScript 总是在客户端运行:还有服务器端 JavaScript。我要说的是,JSP 通常为 JavaScript 提供服务,而由 JSP 提供的 JavaScript 是在客户端运行的。
回答by Romski
JSP is a server-side technology built on Servlets. If you use a container like Tomcat you can see the Servlets generated form the JSP files. In essence the call dispatcher.forward(request, response);
is just a call to another Servlet.
JSP 是一种建立在 Servlet 之上的服务器端技术。如果您使用像 Tomcat 这样的容器,您可以看到从 JSP 文件生成的 Servlet。本质上,调用dispatcher.forward(request, response);
只是对另一个 Servlet 的调用。
回答by Sunil Singh Bora
A JSP is translated into Java servlet before being run, and it processes HTTP requests and generates responses like any servlet. However, JSP technology provides a more convenient way to code a servlet. Translation occurs the first time the application is run. A JSP translator is triggered by the .jsp file name extension in a URL. JSPs are fully interoperable with servlets. You can include output from a servlet or forward the output to a servlet, and a servlet can include output from a JSP or forward output to a JSP.
JSP 在运行之前被转换为 Java servlet,它像处理任何 servlet 一样处理 HTTP 请求并生成响应。但是,JSP 技术提供了一种更方便的方式来编写 servlet。应用程序第一次运行时会发生转换。JSP 转换器由 URL 中的 .jsp 文件扩展名触发。JSP 可以与 servlet 完全互操作。您可以包含来自 servlet 的输出或将输出转发到 servlet,而 servlet 可以包含来自 JSP 的输出或将输出转发到 JSP。
回答by Mitul Maheshwari
The jSP is running only in server side. it's the java code only the Developer's can easily make code in jsp.
jSP 仅在服务器端运行。这是Java代码,只有开发人员可以轻松地在jsp中制作代码。
the jsp is finally converted into a java servlet only.
when we use dispatcher.forward(request, response);
it will just redirect you to that servlet.
jsp 最终仅转换为 java servlet。当我们使用dispatcher.forward(request, response);
它时, 它只会将您重定向到那个 servlet。
回答by RishikeshD
i understand that i am late in answering this question, but may be it can help someone.
我知道我回答这个问题迟到了,但也许它可以帮助某人。
the JSP life cycle involves the following phases:
JSP 生命周期包括以下几个阶段:
1) Compilation
1) 编译
2) Initialization
2) 初始化
3) Execution
3) 执行
4) Cleanup
4) 清理
JSP Compilation
JSP编译
When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page. If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine compiles the page.
当浏览器请求 JSP 时,JSP 引擎首先检查它是否需要编译页面。如果页面从未编译过,或者自上次编译后 JSP 已被修改,则 JSP 引擎会编译该页面。
The compilation process involves three steps ?
编译过程包括三个步骤?
1) Parsing the JSP.
1)解析JSP。
2) Turning the JSP into a servlet.
2)将 JSP 变成 servlet。
3) Compiling the servlet.
3)编译servlet。
When the JSP is converted into an servlet, it has to be executed at the server side to serve the request.
当 JSP 被转换成 servlet 时,它必须在服务器端执行才能为请求提供服务。