1

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?

6 Answers 6

3

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>
Sign up to request clarification or add additional context in comments.

6 Comments

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!
How are arg1/arg2/arg3 being populated? Do you have a form or are they manually entered?
Note: You have to use getPathInfo or getRequestURI to get the "arguments" as they are no longer arguments in the URL-spec way.
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/
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
|
2

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.

Comments

0

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.

3 Comments

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??
It's an HTTP URL specification. So perhaps the question is where does that originally come from, and can that be changed ?
I would most certainly like to know if it can be changed! From what I gather it can not..
0

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.

1 Comment

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.
0

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.

Comments

0

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.

1 Comment

Have you forgot to insert a link here??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.