简体   繁体   English

Servlet JSP web.xml

[英]Servlet JSP web.xml

I see a feature in NetBeans for selecting a JSP for a Servlet and the result XML in web.xml is like this: 我在NetBeans中看到一个用于为Servlet选择JSP的功能,而web.xml的结果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 behind architecture in ASP .NET? 它是否像ASP .NET中的架构背后的代码

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). 它用于将servlet的规范名称(不是您编写的实际Servlet类)映射到JSP(恰好是一个servlet)。 On its own it isn't quite useful. 它本身并不是很有用。 You'll often need to map the servlet to a url-pattern as: 您经常需要将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 “/”. jsp-file元素包含Web应用程序中以“/”开头的JSP文件的完整路径。 If a jsp-file is specified and the load-onstartup element is present, then the JSP should be precompiled and loaded. 如果指定了jsp-file并且存在load-onstartup元素,那么应该预编译并加载JSP。

So, it can be used for pre-compiling servlets, in case your build process hasn't precompiled them. 因此,它可用于预编译servlet,以防您的构建过程未预编译它们。 Do keep in mind, that precompiling JSPs this way, isn't exactly a best practice. 请记住,以这种方式预编译JSP并不是最佳实践。 Ideally, your build script ought to take care of such matters. 理想情况下,您的构建脚本应该处理这些问题。

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支持。

JSPs are servlets. JSP servlet。 JSP is a templating technology that parses the .jsp file and generates a servlet .java file. JSP是一种模板技术,它解析.jsp文件并生成servlet .java文件。 Once that's done, the .java file is compiled into a .class file that runs in the servlet/JSP engine context. 完成后,.java文件将编译为在servlet / JSP引擎上下文中运行的.class文件。

All the web.xml file is doing is associating a .jsp file with a servlet name. 所有web.xml文件都在将.jsp文件与servlet名称相关联。 There's more: you have to map that .jsp to a URL so the servlet/JSP engine can know when to invoke it. 还有更多:您必须将.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是否足以说明这是否与“代码隐藏”相同。

JSPs are kind of servlet. JSP是一种servlet。 JSP pages are compiled into servlet. JSP页面被编译成servlet。 This servlet run in the servlet container provided by any java web server. 此servlet在任何java Web服务器提供的servlet容器中运行。

In web.xml, <servlet> tag used to name the name servlet class and jsp file. 在web.xml中, <servlet>标记用于命名名称servlet类和jsp文件。 Then you can map those servlet and jsp file according to your own URLs. 然后,您可以根据自己的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. 如果你的hello.jsp文件位于JSP文件夹下。 When you try to open the URL with /helloworld. 当您尝试使用/ helloworld打开URL时。 It will open the page hello.jsp. 它将打开页面hello.jsp。

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

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