Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

MVC/Servlet & URL links question

Thread view: 
howboutafresca@hotmail.com - 13 Nov 2006 17:29 GMT
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!
Manish Pandit - 13 Nov 2006 19:20 GMT
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
adwords@pulpjava.com - 14 Nov 2006 05:33 GMT
I do exactly that throughout my website, www.scja.com

If you navigate around, you'll see the content link is appened to the
url. Just hover over a link. Then, the page you call, the controlling
page, extracts the link name, and includes the page.

It's functional.

-Cameron McKenzie
www.scja.com www.mcnz.com www.pulpjava.com

> Hi everyone,
>
[quoted text clipped - 18 lines]
> for no good reason, but I thought I'd ask if there's a better way.
> Thanks!
CV - 14 Nov 2006 08:01 GMT
> Hi everyone,
>
[quoted text clipped - 18 lines]
> 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
howboutafresca@hotmail.com - 14 Nov 2006 22:09 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.