tags:

views:

572

answers:

6

Currently using the HTTPServletRequest class and specifically the .getQueryString method to retrieve an inputted URL.

The URL it will parse is say server/package/servlet?args1/args2/arg3..

I'd like to remove the question mark (?) from the URL however I have no idea how you would accomplish this. I'd just like to replace it with a forward slash (/) however every time I try this I just get errors. Does anyone know how I can change the the question mark to a forward slash?

A: 

You need a combination of HttpServletRequest.getRequestURI() and HttpServletRequest.getQueryString(), and join those together with a '/'.

However I'm interested in why you're doing this and what you're really trying to achieve.

Brian Agnew
Well ideally I'd just like a URL that's divided by slashes. However at the moment I have to put that question mark before the arguments. I assume it's something that tells Java that this is a bit of CGI??
day_trader
It's an HTTP URL specification. So perhaps the question is where does that originally come from, and can that be changed ?
Brian Agnew
I would most certainly like to know if it can be changed! From what I gather it can not..
day_trader
A: 

The question mark in the URL is defined by the HTTP specification. It will be a part of the URL, whenever any data is sent via the URL to the server. You cannot re-define the basic structure of a URL as defined by the HTTP spec.

Vineet Reynolds
This was my basic assumption..
day_trader
Servlets have the getRequestParameter() method to extract data from a request that conforms to the HTTP spec. So you unless you want to avoid getRequestParameter() calls, you should not redefine the URL format. If you intend to do so, use HTTP POST, and perform your own parsing of the POST request body.
Vineet Reynolds
A: 

sounds like you are after some kind of url rewriting.

this is supposed to be able to do it for tomcat and other j2ee servers.

never used it myself though.

Omry
Have you forgot to insert a link here??
day_trader
I guess. updated the text.
Omry
A: 

From your reply to Brian Agnew: "Well ideally I'd just like a URL that's divided by slashes. However at the moment I have to put that question mark before the arguments." Er, yes, the question mark is a fundamental part of the URL spec and it separates the resource being requested (e.g., page) from parameters to provide the resource.

It sounds like you're trying to do something RESTful. If so, you have to set up servlet mapping entries in your servlet configuration to map the URL to your servlet, which can then retrieve its parameters from the URL. If you search for "+java +RESTful" you'll find several resources to get you going with that.

T.J. Crowder
+3  A: 

Do you want it so the input URL can be http:/example.com/servlet/arg1/arg2/arg3?

If so then you want to add a servlet mapping on the lines of /sevletname/* to your web.xml.

So in your web.xml you want:

  <servlet>
    <servlet-name>ServletName</servlet-name>
    <servlet-class>biggle.whatever.Servlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletName</servlet-name>
    <url-pattern>/webxml_random/*</url-pattern>
  </servlet-mapping>

(example: http://michael-lloyd-lee.appspot.com/webxml%5Frandom/test/like/this)

mlk
I already have this in my web.xml and it doesn't allow me to enter a slash instead of the question mark... a little unfortunate! Unless of course I'm doing something wrong!
day_trader
How are arg1/arg2/arg3 being populated? Do you have a form or are they manually entered?
mlk
Note: You have to use getPathInfo or getRequestURI to get the "arguments" as they are no longer arguments in the URL-spec way.
mlk
They are manually entered and then parsed according to how they're identified in the url.i.e. server/package/servlet/AUTHOR:jkrowling/SERIES:harrypotter/BOOK:chamberofsecrets/that is what I would like. however at the moment it's in the form:i.e. server/package/servlet?AUTHOR:jkrowling/SERIES:harrypotter/BOOK:chamberofsecrets/
day_trader
Do you mind if I ask why you want them that way? I could understand if it was simply:http://example.com/book-servlet/jkrowling/happypotter/chamberofsecrets but just switching the ? to a / otherwise makes little sense to me.Anyway that works fine but you have to use getPathInfo as they are not arguments.http://michael-lloyd-lee.appspot.com/webxml_random/AUTHOR:jkrowling/SERIES:harrypotter/BOOK:chamberofsecrets/
mlk
you forgot the * in the pattern. if the * is there, you can request the * part with request.getPathInfo(), with is the part after the servletname
Salandur
Spot on!Cheers!
day_trader
+2  A: 

If you have something that will produce a request in the format that you desire, then you can specify a servlet mapping in the form /foo/*, and call getRequestURI() to get that path. Then you simply parse out the arguments, perhaps by calling String.split("/").

However, the real question is how you produce such a URL. As other posters have noted, the question mark is part of the URL specification, and an HTML GET form will produce URLs in that form -- that doesn't matter whether your server is written in Java, Python, PHP, or anything else.

kdgregory