File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes 2 different url but link to the same page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "2 different url but link to the same page" Watch "2 different url but link to the same page" New topic
Author

2 different url but link to the same page

Derek Bright
Greenhorn

Joined: Aug 28, 2008
Posts: 28
Hi, currently i am working on a webpage which looks something like a blog or portal. As in when different user go to their own homepage... they type in the url based on their blog or portal name. For example:

Mike enters the url: localhost/Mike
Kimberly enters the url: localhost/Kimberly

currently, i can only access this home page if i use: localhost/index.jsp

my application only has 1 dynamic home page but loads the contents based on each users. My question is, how should i write the code so that this can be achieved?

Update: i have manage to do that, but only if i put my url as localhost/Mike/
i always need the "/" for my code to work...else my css and images wont load up...so what is likely to be my problem?


[ September 19, 2008: Message edited by: Derek Bright ]
[ September 19, 2008: Message edited by: Derek Bright ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

What do you already know about URL mapping in the deployment descriptor?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Derek Bright
Greenhorn

Joined: Aug 28, 2008
Posts: 28
hmm...as far as i know, i need to do something in the servlet, probably that is my controller servlet. I have my url mapping done, which means everytime i type in the url, i will definitely go into my controller.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

If the mapping to your servlet is something like whatever/*, then whatever servlet is mapped can obtains the remains of the URL (in your case, the member's name) by grabbing the path info fomr the request.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Please avoid "ninja edits" to your original posts.

Yeah, don't map "/". That's just asking for trouble!
Derek Bright
Greenhorn

Joined: Aug 28, 2008
Posts: 28
sorry about the edit...

as for the don't map "/", what do you mean by that? Currently i have the whatever/*...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

I thought your edit mentioned mapping just "/" and having trouble with other resources. Yeah, don't do that.
[ September 20, 2008: Message edited by: Bear Bibeault ]
Derek Bright
Greenhorn

Joined: Aug 28, 2008
Posts: 28
hmm...i shall summarize again.

currently, to access say...Bryan home page, i need to type the URL as follows:
localhost/whatever/Bryan/
take note that i need a "/" after Bryan to have all my resources (e.g. images, css, javascript) to work.

what i wanted is to have Bryan to access the home page without the "/". That means i should just need to type the URL as "localhost/whatever/Bryan" to have my stuff to work.

My web.xml has the follwing:

<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/whatever/*</url-pattern>
</servlet-mapping>

So any suggestion how to get this done?
Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Originally posted by Derek Bright:
currently, to access say...Bryan home page, i need to type the URL as follows:
localhost/whatever/Bryan/
take note that i need a "/" after Bryan to have all my resources (e.g. images, css, javascript) to work.

Why? They should be independent of the servlet URL. Are you using page-relative addressing to access these resources within your pages? If so, don't. See the JSP FAQ on resource URLs to learn how to make them independent.
Derek Bright
Greenhorn

Joined: Aug 28, 2008
Posts: 28
hi bear,

thanks for your replies, i finally get what you are trying to say, that is using the context path to link to all my resources right. Thanks!
Derek Bright
Greenhorn

Joined: Aug 28, 2008
Posts: 28
i have 1 more question: that is suppose i need to have other links from the home page...how should i generate the <a href> - that is to have something like this

localhost/whatever/Bryan/blog
localhost/whatever/Kelly/blog/archive

am i suppose to do something like this?
<a href="${pageContext.request.contextPath}/whatever/${userName}/blog">blog</a>

would it be this way...or there is suppose to be some correct method to achieve this? thanks again~
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

That way is fine. Using the JSTL <c:url> tag is easier for some.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: 2 different url but link to the same page
 
Similar Threads
Testing Struts applications with Selenium
maze solve
Types of Blog you'd like to see
Configuring jboss portal server to listen to apache using proxypass..?
wait() question from kb book