• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get parameter in Servlet in different way

 
Zaeed McColin
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No one says you have to use them.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. obfuscation != security
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic