java – JSP:forward Question – Education Career Blog

I am just getting started on web app development. I have an index.jsp that has just ONE line.

< jsp:forward page=”landing.do?”/>

What does

  • the above line do?

  • page=”landing.do?” actually refer to?

  • what does the question mark “?” next to “landing.do?” signify?

As Bozho rightly pointed out, a servlet called “action” is mapped to handle “*.do” in my web.xml (as shown below).

<servlet-mapping>

    <servlet-name>action</servlet-name>

    <url-pattern>*.do</url-pattern>

</servlet-mapping>

Now

How do I find out what the servlet “action” corresponding to “landing.do” actually does?

Thanks.

,

The <jsp:forward> forwards a client request to the url declared on the page attribute.

I also need to mention that in your example, you should have a / as first character inside your page declaration, if you want to specify a relative URL, i.e.:

This, in effect, is translated as a redirection to (if localhost)

http://localhost:8080/MyAPP/landing.do? (yours would have been translated to http://localhost:8080/MyAPPLanding.do?)

The ? allows you to append application/x-www-form-urlencoded parameters into your declaration.

More info here.


To know what landing.do does, do the following:

  • Go to your struts-config.xml (found in WEB-INF folder in your project) file and find any action (<action>) that a path="/landing") attribute.
  • Once you find your action, there’s an attribute called type (inside that action). The type is a the class name of the action class that Struts calls to execute the action. The class name is fully qualified name.
  • Open the java file of the class (if it exists) and depending on the action (Action, DispatchAction, LookupDispatchAction), you will have to find its mappings and see what method Struts invokes.
  • In your example, my assumption will be based that your landing.do is of type Action. Therefore, read what the execute() method does. All actions actually, is execute() by Struts. The other actions are just Template Method patterns that knows what method to call by some mapping.

,

  • you probably have a servlet mapped to handle *.do in your web.xml
  • the ? means nothing here – generally it marks the start of get parameters (like ?param=value)
  • forward changes the current page with the specified, without the client knowing the change has happened.

,

This line will forward user to another page of the site, in particular to landing.do

page=”landing.do?” actually refer to some page of the site landing.do. I believe this page is written with Struts framework. (But can be other)

what does the question mark “?” next to “landing.do?” mean nothing in this case. Generally after “?” there should be a list of request parameters. In this cases there will just be no parameters.

Update:
You should find servlet class which is mapped to that servlet name. After that you will be able to try to understand what that servlet class does. Also, look at the Struts tutorials or specification to get understanding of Struts framework workflows.

Leave a Comment