Servlet JSP web.xml [英] Servlet JSP web.xml

查看:70
本文介绍了Servlet JSP web.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了NetBeans中为Servlet选择JSP的功能,并且web.xml中的XML结果是这样的:

I see a feature in NetBeans for selecting a JSP for a Servlet and the result XML in web.xml is like this:

<servlet>
    <servlet-name>TestServlet</servlet-name>
    <jsp-file>/index.jsp</jsp-file>
</servlet>

是什么意思?它是做什么用的? 就像ASP .NET中的幕后代码体系结构一样吗?

What does it mean? And what is it for? Is it like code behind architecture in ASP .NET?

推荐答案

这是什么意思?它是做什么用的?

它用于将servlet(不是您编写的实际Servlet类)的规范名称映射到JSP(恰好是servlet).单靠它并不是很有用.您通常需要按以下方式将servlet映射到url-pattern:

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-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>

现在所有到达/test/*的请求都将由JSP处理.

All requests now arriving at /test/* will now be serviced by the JSP.

此外,servlet规范还指出:

Additionally, the servlet specification also states:

jsp-file元素包含完整的 Web中JSP文件的路径 以"/"开头的应用程序.如果一个 指定了jsp-file并且 load-onstartup元素存在, 然后应预先编译JSP并 加载.

The jsp-file element contains the full path to a JSP file within the web application beginning with a "/". If a jsp-file is specified and the load-onstartup element is present, then the JSP should be precompiled and loaded.

因此,它可以用于预编译servlet,以防您的构建过程尚未对其进行预编译的情况.请记住,以这种方式预编译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.

这是ASP.NET中体系结构背后的代码吗?

否,如果您正在寻找代码隐藏的体系结构,则与此最相似的是JSF提供的Managed Beans支持.

No, if you're looking for code-behind architecture, the closest resemblance to such, is in the Managed Beans support offered by JSF.

这篇关于Servlet JSP web.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆