Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet virtual path  RSS feed

 
Steve Wood
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Is there a way to get a servlet to accept a path and forward this to a particular JSP page? I'm new to servlets.

So, something like this:

www.mywebsite.com/myproject

forwards to

www.mywebsite.com/workspaces/login.jsp?workspaceid=someidformyproject

The directory myproject would not exist but would be a simplified URL.

Basically, I want to create a system that allows users to create project workspaces similar to SharePoint - so they specify the name of the workspace and this automatically becomes a directory path on the server (though the directory will not actually exist).

I hope that makes sense - any help is very much appreciated.

Cheers,

Steve
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66184
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course. Declare a servlet mapping for the servlet and the use a request dispatcher to forward to the JSP.
 
Steve Wood
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear,

When you say delare a servlet mapping, is that in the web.xml? If you could give me an example, that would be really helpful as I'm really new to this - I'm one of those annoying JSP developers that doesn't understand servlets (just knows they're there).

Do I need to declare a servlet mapping for every directory or can I do something like:

/workspaces (goes to the servlet)
/workspaces/myproject (servlet picks up the myproject as a parameter)

As you can see, I need some guidance!

Thanks for your help.

Steve
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66184
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Wood:

When you say delare a servlet mapping, is that in the web.xml? If you could give me an example..
Yes.

Here's a simple sample:



After that, every URL mapped to your web app context that begins with /whatever/ will map to the servlet. The remainder of the URL is available from the request instance via getPathInfo().

I'm one of those annoying JSP developers that doesn't understand servlets (just knows they're there).


I highly recommend you wrap your mind around servlets. The best web app structures embrace both servlets and JSP, each to their best advantage. Grab a copy of the Servlet Spec and read it cover to cover. It's a surprisingly easy read.

Link to Spec can be found in the JSP FAQ.

Do I need to declare a servlet mapping for every directory or can I do something like:

/workspaces (goes to the servlet)
/workspaces/myproject (servlet picks up the myproject as a parameter)


See above.

As you can see, I need some guidance!


You're in the right place.

Thanks for your help.


My pleasure.
[ February 09, 2007: Message edited by: Bear Bibeault ]
 
Steve Wood
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear,

I'm up and running. I'll definitely be reading about servlets - I'm now at a stage where I really need to understand how they work. This is the first time I've thought "hmmmm... maybe a servlet could do that" - I'm sure they can do a lot more!

Cheers,

Steve
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a link to the servlet spec in my signature.
There is no better reference.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!