MVC/Servlet & URL links question

H

howboutafresca

Hi everyone,

I'm trying to incorporate the MVC into my J2EE 1.4 project for school.
I'm new to this and had a couple of questions?

I understand that a typical way of handiing form posts from a JSP page
is to post to a servlet, which examines a request variable or two,
maybe does a little processing using beans and whatnot, and then
forwards the request to a JSP for display. Sounds good.

However, how can I efficiently handle / code URL links in my JSP pages,
so that the servlet knows what to do? Say for instance I have a JSP
with a few hyperlinks in it. When I click on one of them I'll go to a
servlet, which then forwards me to the proper JSP. But how does the
servlet know where to forward me?

I suppose I could add a URL variable to the hyperlinks, to be used by
the servlet. I'm curious if there's a better way to go about this -
maybe via a few calls to request.setAttribute() in the initial JSP
page? I'm a little leery of appending info to the hyperlinks. Maybe
for no good reason, but I thought I'd ask if there's a better way.
Thanks!
 
M

Manish Pandit

If you have not looked at Struts - do consider it. It lets you put the
mappings externally in a configuration file, where in your servlet can
forward to mappings, which are identified by a string. The servlet (an
action class in Struts terminology) will produce a deterministic number
of outbound flows, which can be configured in struts-config.xml and the
Struts framework will take care of forwarding/redirecting the requests.


-cheers,
Manish
 
C

CV

Hi everyone,

I'm trying to incorporate the MVC into my J2EE 1.4 project for school.
I'm new to this and had a couple of questions?

I understand that a typical way of handiing form posts from a JSP page
is to post to a servlet, which examines a request variable or two,
maybe does a little processing using beans and whatnot, and then
forwards the request to a JSP for display. Sounds good.

However, how can I efficiently handle / code URL links in my JSP pages,
so that the servlet knows what to do? Say for instance I have a JSP
with a few hyperlinks in it. When I click on one of them I'll go to a
servlet, which then forwards me to the proper JSP. But how does the
servlet know where to forward me?

I suppose I could add a URL variable to the hyperlinks, to be used by
the servlet. I'm curious if there's a better way to go about this -
maybe via a few calls to request.setAttribute() in the initial JSP
page? I'm a little leery of appending info to the hyperlinks. Maybe
for no good reason, but I thought I'd ask if there's a better way.
Thanks!

You can add the following type of URLs as a href in your redirecting file:

<a href="Controller/RequestType1">Handle request type 1</a>
<a href="Controller/RequestType2">Handle request type 2</a>

In your deployment descriptor you then define a mapping for this servlet:

<servlet>
<display-name>ControllerServlet</display-name>
<servlet-name>ControllerServlet</servlet-name>
<sevlet-class>yourpackage.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/Controller/*</url-pattern>
</servlet-mapping>

The url-pattern tells the application server to forward requests
directed to an url of the form /Controller/* to the ControllerServlet.

Now, when handling the request in the ControllerServlet you can do the
following:

String requestType = request.getPathInfo().substring(1);

This returns RequestType1 or RequestType2 depending on which link you
clicked (because getPathInfo() returns a String /RequestType1 or
RequestType2, hence, substring(1) drops the /).
You can use these values to decide which view you redirect to after
changing the model.

Hope this helps.

Chris
 
H

howboutafresca

In your deployment descriptor you then define a mapping for this servlet:
.....


Thanks for the tips, everyone. I think I have enough to get started
with.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top