14

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
ehsun7b
  • 4,796
  • 14
  • 59
  • 98

3 Answers3

38

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.

didxga
  • 5,935
  • 4
  • 43
  • 58
Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
  • Actually I don't prefer the code behind architecture and I'm happy that it is not like that. But the JSF architecture is so better than code behind. But I'm still confused. In my example, what will produce the result? `TestServlet` or `index.jsp` ? – ehsun7b Jun 19 '11 at 10:00
  • I'm not sure I understood your comment. The request will be processed by `index.jsp`. `TestServlet` like I stated in the answer is just a canonical name for the servlet, so that you can reference it as a servlet in web.xml. You'll need to affix a URL pattern, and requests have to be sent to URLs matching that pattern, for any processing to occur. – Vineet Reynolds Jun 19 '11 at 10:09
  • Aha, so I don't need to write any code for `TestServlet`, right? – ehsun7b Jun 19 '11 at 10:10
  • Where should I place `index.jsp`? – Rudziankoŭ Dec 18 '15 at 10:41
  • Rudziankoŭ, put it too `/ROOT` directory. – Rudziankoŭ Dec 18 '15 at 10:44
8

JSPs are servlets. 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.

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.

I don't know ASP or .NET well enough to say whether this is the same as "code behind".

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Good, but I'm still confused. In my example, what will produce the result? `TestServlet` or `index.jsp`? The `TestServlet` is a servlet that I need to write or not? – ehsun7b Jun 19 '11 at 10:01
  • 1
    @ehsun7b You don't need to write a `TestServlet` class; that's just a name that you use to refer to your servlet in your application. `index.jsp` would handle all the requests listed in the `` body for `TestServlet` in `web.xml`. – Bharat Khatri Aug 12 '13 at 11:23
  • @duffymo is there a way to remove this jsp mapping from web.xml ? – Gaurav Mar 11 '20 at 09:34
6

JSPs are kind of servlet. JSP pages are compiled into servlet. This servlet run in the servlet container provided by any java web server.

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.

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

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Hafiz Shehbaz Ali
  • 2,566
  • 25
  • 21