tags:

views:

54

answers:

2

Hi I like to know like whenever user requests a jsp page we write hello.jsp or any html file we write hello.html or any image hello.jpeg. My question is does servlet not have any extension ? Is it called directly called by name?

+3  A: 

For Servlets, you have to explicitly tell the Servlet Container what URLs (either specific URLs or wildcards) map to what servlet. For example:

<servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>com.example.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/hello</url-pattern>
</servlet-mapping>

The above example would map the URL /hello to the servlet com.example.HelloWorld.

You can also do some wildcard mapping. For example:

<servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

This would map requests ending in ".html" to the HelloWorld servlet. But you aren't limited to any particular extensions. You could use anything you want:

<servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>*.foo</url-pattern>
</servlet-mapping>

All of this configuration takes place in your web application's web.xml file.

Adam Batkin
+1. Maybe also mention that in addition to extensions, you can also register path prefixes.
Thilo
+1  A: 

No, you have it fully in your own hands. It's not necessarily called by its name, it's called by its url-pattern. You can name it to whatever you want, e.g. /pages/* will run the servlet whenever you call http://example.com/pages/foo.jsp or http://example.com/pages/foo (which in turn gives the pathinfo back by request.getPathInfo(), so that you can determine what action to take and/or where to forward the request to). Or *.page which runs the servlet whenever you call http://example.com/foo.page (which in turn gives URI back by request.getRequestURI()).

To preprocess requests (when one requests a page for view) you normally use doGet() method. To postprocess requests (after a POST form submit), you normally use doPost() method.

You can in fact create as many servlets as you want, e.g. RegisterServlet listening on /register which is backed by a register.jsp as view and a LoginServlet listening on /login and backed by a login.jsp as view, etcetera. You can hide JSPs from direct access by placing them in /WEB-INF so that users are forced to call them through the servlet.

In MVC world, there's usually means of only one servlet listening on a certain url-pattern, which is called the Front Controller. In Sun JSF for example, there's the FacesServlet which runs whenever an URL matching by default *.jsf or /faces/* is called. In Apache Struts for example, there's the ActionServlet which listens on by default *.do. They determines which action to take and/or which view (the JSP file) to display based on the URL, request parameters and/or mappings. You're however free to change those default url-patterns. You can even change the default url-pattern of the JspServlet in servlercontainer's web.xml, which by default listens on *.jsp. It's however recommended to stick to a sensible and standardizedurl-pattern.

It might be interesting to know that any other "undefinied" URL patterns are covered by a "default" servlet. Check the servletcontainer's web.xml, you'll see one servlet which listens on / and thus in fact serves everything. It also manages display of directoy listings. In Tomcat for example it's called the DefaultServlet and described here.

BalusC