3

HI,

We are declaring the FacesServlet and its URL mapping in the Web.xml. From my understanding,

FacesServlet loaded only once at the server startup. URL mapping is used only when first time JSP application accessed from the external context.

One of the new learner for JSF has asked me the questions, these two things are used only once by the application. Is it true? Also is there any other way by not including in the web.xml?

What I should answer?

Updated

For example, I am accessing the application using the URL http://localhost:8080/webapp/index.jsf. When we are accessing this URL, FacesServlet invoked and view is rendered. The following is my question:

  • In JSF, we never seen changing the URL in the address bar. In that case, how it is handling the new request with the same URL?
  • In faces-config.xml we are giving the navigation cases as follows:

    to-view-id>failure.jsp /to-view-id>

    • Why we need not give the view name as failure.jsf? We are just giving the *.jsp in the faces-config.xml. How it is handled internally?
jmj
  • 225,392
  • 41
  • 383
  • 426
Krishna
  • 6,706
  • 15
  • 64
  • 79
  • `In JSF, we never seen changing the URL in the address bar. In that case, how it is handling the new request with the same URL?` can you please give us example to explain this scenario – jmj Jan 27 '11 at 17:16

2 Answers2

3

FacesServlet loaded only once at the server startup.

Correct.

URL mapping is used only when first time JSP application accessed from the external context.

Incorrect. It's been tested on every incoming HttpServletRequest. How else should the container know which servlet to invoke?

Also is there any other way by not including in the web.xml?

If you're using a servletcontainer which supports Servlet 3.0, you can also do this by @WebServlet annotation. JSF 2.0, however, is designed to be backwards compatible with Servlet 2.5, so it doesn't ship with that annotation and you need to explicitly declare it in web.xml.

See also:


Update as per the new series of questions (which should each belong in its own question, but ala)

In JSF, we never seen changing the URL in the address bar. In that case, how it is handling the new request with the same URL?

This happens only if under the covers a forward by RequestDispatcher#forward() takes place. In a forward, the servletcontainer basically reuses the same HTTP request/response for a view (JSP/XHTML page). It does not force/instruct the webbrowser to send a brand new request. On the other hand, the HttpServletResponse#sendRedirect() will instruct the client (the webbrowser) to fire a new GET request and thus the URL will change. You can force this in the JSF sice by adding <redirect/> to the <navigation-case>. Note that since this causes a new request, all request scoped beans of the initial request will be lost.

Why we need not give the view name as failure.jsf? We are just giving the *.jsp in the faces-config.xml. How it is handled internally?

The FacesServlet knows its own url-pattern.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • I think last question is not clear. My question is the URL pattern for the JSF is *.jsf, it is mentioned in the web.xml, in that case every view has to be accessed as with .jsf extension to invoke in the FacesContext, but how it is working fine if we give the normal .jsp extension. – Krishna Jan 27 '11 at 17:24
  • I am happy to write new question, but I felt it is related to the same question – Krishna Jan 27 '11 at 17:25
  • 2
    The `*.jsf` is just an `url-pattern` to invoke the FacesServlet. It will convert it to `*.jsp` anyway to locate the concrete view files. – BalusC Jan 27 '11 at 17:26
2

Yes. while loading your application container will load web.xml and will extract the data for

particular URL pattern to servlet . when request comes it checks from memory that for this pattern which servlet to invoke and then if servlet is already loaded it will take it from memory otherwise it will create an instance of servlet and it will invoke doGet() or doPost() depending on the request type.

and there is another way to delcare URL mapping as from JAVAEE-6 by annotation

something like

import javax.servlet.annotation.InitParam;
import javax.servlet.annotation.WebServlet;

@WebServlet(
    name = "SimpleServlet", 
    urlPatterns = {"/login"}, 
    initParams = {
        @InitParam(name = "param1", value = "value1"),
        @InitParam(name = "param2", value = "value2")}
)
public class SimpleServlet {
}

In faces-config.xml we are giving the navigation cases as follows:

<to-view-id>failure.jsp </to-view-id>

Why we need not give the view name as failure.jsf? We are just giving the *.jsp in the faces-config.xml. How it is handled internally?

it is view identifier not the URL FacesServlet will load that view upon invocation of that navigation case.

jmj
  • 225,392
  • 41
  • 383
  • 426