This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Servlets and the fly likes get parameter in Servlet in different way Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "get parameter in Servlet in different way" Watch "get parameter in Servlet in different way" New topic
Author

get parameter in Servlet in different way

Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90
Hi guys
I'm new in Servlet
I get parameters like this (localhost:1313/JEE/servlet?id=1) in post or get method

but I have a question
can I receive this parameter in this way "localhost:1313/JEE/servlet/1"

exactly get localhost:1313/JEE/servlet/1 (instead of) localhost:1313/JEE/servlet?id=1


Open source
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
no you can't in addition you will get the parameters in the query string only if you are using the GET method of the HTTP, however if you use the POST method the parameters will be inserted in the body of the HTTP request


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
The keyword you're looking for is "pretty url" or "friendly url".

You can use a Filter for this which redirects unfriendly urls and forwards friendly urls. You only need to map the pathinfo yourself.
Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90
many thanls Mr.Bauke Scholtz
I'll try to do that

& Mr. kababji
thank for reply but I know the difference between post and get method, it was just a example that show how I get a parameter

many thanks & respect
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

omar al kababji wrote:no you can't ...

Of course you can; just not via getParameter. You'll need to fetch and parse the path info yourself (or look to something like JAX-RS). The getPathInfo method will return the trailing path information if you map the servlet with a trailing wildcard.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
of course you can do it yourself there is nothing impossible in programming, but what i meant by "no you can't" is simply that you can't do it using some Servlet API, or by specifying how query parameters should be passed to the Servlet, since thats the way HTTP Protocol works.

and yes you can do it by parsing the string, but you will be doing at by yourself. my question would be why would you need to use a different encoding to pass parameters to your servlets? and why would you use such a way that will limit you to using only GET methods?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
and why would you use such a way that will limit you to using only GET methods?

Uh, as I said before, the keyword is "pretty url" or "friendly url". Google it.

This has completely nothing to do with encoding.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

As Bauke pointed out, it's a concept known as "friendly URLs". It's getting popular in many circles (have you looked at the URLs for this very site lately?) and is extremely important and central to RESTful web service APIs.
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
"friendly urls" honestly i don't like the idea, friendly for whom? for the programmer? i think that the normal url's are much more friendly for a programmer than having him to use a different url. in addition i don't a good explanation for having a friendly url for the user of a web application, in then end its better to not making him understand whats data gets in and out from the parameters, making the website more vulnerable.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

No one says you have to use them.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

P.S. obfuscation != security
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
omar al kababji wrote:"friendly urls" honestly i don't like the idea, friendly for whom? for the programmer? i think that the normal url's are much more friendly for a programmer than having him to use a different url. in addition i don't a good explanation for having a friendly url for the user of a web application, in then end its better to not making him understand whats data gets in and out from the parameters, making the website more vulnerable.

So you're either not a real web programmer or you've somehow never heard of SEO.

I also don't see how it increases the vulnerability, so you're just babbling somewhat randomly. Please come up with reasonable arguments.
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
Am sorry for you guys but searching for a lot of stuff that are not needed is something that i really hate. i am not babbling I only say that there are things that are important and others that are not important, and in the end if you replace an & with a / you are not making nothing more friendly. and if you do real web programming your query string would be used only when you get something from the server and not updating something so as an average there would be something around 4 to 5 parameters in the querystring.

Again i don't find a good reason for it.

have a nice day.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Your aversion is just caused by misunderstanding the concept. You still don't understand it. It is certainly not intented to be used for submitting forms or so, you of course use POST for this, it is just navigational. Google SEO and read on about it.
Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90
many many thanks my teachers

Alright i changed web.xml for one of the servlet as this



now it will not return error in some link like localhost:1313/JEE/testShow/1

is it good solution? or not?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

ArAsh Dex wrote:now it will not return error in some link like localhost:1313/JEE/testShow/1

Correct? It's perfectly valid according to your mapping.

is it good solution? or not?

That depends. Why are you wanting to do it this way? Using the path info rather than the usual request parameters is more work and is not directly supported by the Servlet API.

So if you have a good reason for doing it this way, then maybe it's worth it. If you are doing it "just because", not so much.
Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90
no it's just a solution I thought maybe i can solve with this way

I wanna know, do we have better way to solve it?

i don't have a problem with this solution right now, but it's really important for me, is it standard or not

respect Mr. Bear Bibeault(my teacher)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Well, what problem are you trying to solve?

Normally, data is passed into a servlet (or other web resource) as request parameters. Unless you have a good reason to do otherwise, that's really the best way to go.
Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90
it is exactly such as my first question

get localhost:1313/JEE/servlet/1 (instead of) localhost:1313/JEE/servlet?id=1

and i thought maybe I can solve it with servlet mapping in web.xml
and don't have problem right now

I'll try to find other way such as 'pretty urls', and i hope can find the best way

many many many thanks my amigos
Mr. Bear Bibeault(as always) , Mr. Bauke Scholtz & Mr. Omar al kababji
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Actually, using a URL such as localhost:1313/JEE/testShow/1
where testShow identifies the servlet and "1" has to be interpreted by the servlet is perfectly good REST style programming.

Not perhaps where a complete beginner should start but a valid approach and supported by Sun's Jersey project and numerous others.


Bill
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Yes, Jersey is what I was referring to earlier as JAX-RS (Jersey being the reference implementation).

But I will reiterate that, especially for a novice, learning the usual request parameter mechanisms is a better idea than immediately diving off into RESTful APIs.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get parameter in Servlet in different way