Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 835
ehsun7b Avatar asked Jun 19 '11 09:06

ehsun7b


People also ask

What is web xml in JSP?

You can register a JSP as a servlet using the servlet element of the Java EE standard deployment descriptor web. xml. (The web. xml file is located in the WEB-INF directory of your Web application.) A servlet container maintains a map of the servlets known to it.

Does JSP need web xml?

No, you don't need, jsp file can be directly invoked by URL.

What is servlet in web xml?

Servlets and URL Pathsxml defines mappings between URL paths and the servlets that handle requests with those paths. The web server uses this configuration to identify the servlet to handle a given request and call the class method that corresponds to the request method (e.g. the doGet() method for HTTP GET requests).

What is servlet and JSP?

Java™ servlets and Java server pages (JSPs) are Java programs that run on a Java application server and extend the capabilities of the Web server. Java servlets are Java classes that are designed to respond to HTTP requests in the context of a Web application.


1 Answers

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.

like image 77
Vineet Reynolds Avatar answered Sep 22 '22 13:09

Vineet Reynolds