Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Additionally, the servlet specification also states:

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.

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.

Is it like code behind architecture in 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...