Java Servlet JSP web.xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6401588/
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
Servlet JSP web.xml
提问by ehsun7b
I see a feature in NetBeans for selecting a JSP
for a Servlet
and the result XML in web.xml
is like this:
我在 NetBeans 中看到一个用于JSP
为 a选择 a 的功能,Servlet
结果 XMLweb.xml
是这样的:
<servlet>
<servlet-name>TestServlet</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
What does it mean? And what is it for? Is it like code behindarchitecture in ASP .NET?
这是什么意思?它是做什么用的?它像ASP .NET 中的体系结构背后的代码吗?
采纳答案by Vineet Reynolds
What does it mean? and What is it for?
这是什么意思?它是做什么用的?
It is used to map a canonical name for a servlet (not an actual Servlet class that you've written) to a JSP (which happens to be a servlet). On its own it isn't quite useful. You'll often need to map the servlet to a url-pattern as:
它用于将 servlet(不是您编写的实际 Servlet 类)的规范名称映射到 JSP(恰好是一个 servlet)。就其本身而言,它不是很有用。您通常需要将 servlet 映射到 url-pattern,如下所示:
<servlet>
<servlet-name>TestServlet</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<!--mapping-->
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
All requests now arriving at /test/*
will now be serviced by the JSP.
现在到达的所有请求现在都/test/*
将由 JSP 提供服务。
Additionally, the servlet specification also states:
此外,servlet 规范还指出:
The
jsp-file
element contains the full path to a JSP file within the web application beginning with a “/”. If ajsp-file
is specified and theload-onstartup
element is present, then the JSP should be precompiled and loaded.
该
jsp-file
元素包含以“/”开头的 Web 应用程序中 JSP 文件的完整路径。如果jsp-file
指定了a 并且load-onstartup
元素存在,则应该预编译和加载 JSP。
So, it can be used for pre-compiling servlets, in case your build process hasn't precompiled them. Do keep in mind, that precompiling JSPs this way, isn't exactly a best practice. Ideally, your build script ought to take care of such matters.
因此,它可用于预编译 servlet,以防您的构建过程未预编译它们。请记住,以这种方式预编译 JSP 并不是最佳实践。理想情况下,您的构建脚本应该处理这些问题。
Is it like code behind architecture in ASP .NET?
它像 ASP .NET 中的体系结构背后的代码吗?
No, if you're looking for code-behind architecture, the closest resemblance to such, is in the Managed Beans support offered by JSF.
不,如果您正在寻找代码隐藏架构,那么与此最相似的是 JSF 提供的 Managed Beans 支持。
回答by duffymo
JSPs areservlets. JSP is a templating technology that parses the .jsp file and generates a servlet .java file. Once that's done, the .java file is compiled into a .class file that runs in the servlet/JSP engine context.
JSP是servlet。JSP 是一种模板技术,它解析 .jsp 文件并生成 servlet .java 文件。完成后,.java 文件被编译成在 servlet/JSP 引擎上下文中运行的 .class 文件。
All the web.xml file is doing is associating a .jsp file with a servlet name. There's more: you have to map that .jsp to a URL so the servlet/JSP engine can know when to invoke it.
web.xml 文件所做的只是将 .jsp 文件与 servlet 名称相关联。还有更多:您必须将该 .jsp 映射到 URL,以便 servlet/JSP 引擎可以知道何时调用它。
I don't know ASP or .NET well enough to say whether this is the same as "code behind".
我不太了解 ASP 或 .NET,无法说明这是否与“代码隐藏”相同。
回答by Hafiz Shehbaz Ali
JSPs are kind of servlet. JSP pages are compiled into servlet. This servlet run in the servlet container provided by any java web server.
JSP 是一种 servlet。JSP 页面被编译成servlet。这个servlet 运行在任何java web 服务器提供的servlet 容器中。
In web.xml, <servlet>
tag used to name the name servlet class and jsp file. Then you can map those servlet and jsp file according to your own URLs.
在web.xml中,<servlet>
标签用来命名servlet类和jsp文件。然后你可以根据你自己的 URL 映射那些 servlet 和 jsp 文件。
<servlet>
<servlet-name>hello</servlet-name>
<jsp-file>/jsp/hello.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
If your hello.jsp file located under JSP folder. When you try to open the URL with /helloworld. It will open the page hello.jsp.
如果您的 hello.jsp 文件位于 JSP 文件夹下。当您尝试使用 /helloworld 打开 URL 时。它将打开页面 hello.jsp。