Federer
Federer

Reputation: 34815

Java URL - Passing Arguments - Servlets

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?

Upvotes: 1

Views: 6463

Answers (6)

Michael Lloyd Lee mlk
Michael Lloyd Lee mlk

Reputation: 14661

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>

Upvotes: 3

kdgregory
kdgregory

Reputation: 39606

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.

Upvotes: 2

Omry Yadan
Omry Yadan

Reputation: 33696

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.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1075755

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.

Upvotes: 0

Vineet Reynolds
Vineet Reynolds

Reputation: 76719

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.

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272417

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.

Upvotes: 0

Related Questions