tags:

views:

455

answers:

3

Hi All,

I have a link, on click of which I have to got the next page, I'm giving the url for the link as
http://localhost/test/controller?name=""&image="url"

Is there a way to pass the data without appending to the url on click of a link?Its normal a href.

+2  A: 

If the other page is on the same domain, then any information stored in cookies can be accessed by both of them, and doesn't need to be appended to the link. Typically if you're using JSPs, the servlet container will automagically manage sessions for you (based on a cookie behind the scenes) - so you can set attributes on the session in one page, and then read them from another. See for example this Sun tutorial on storing client state in servlets.

Bear in mind though that this would need to happen on the server-side, so if it was something that needed to be done exactly when the user clicked on the link (such as recording the time they clicked), then this could not be done by pure JSP session logic. You'd need to provide some kind of cookie-based logic yourself.


And if the target page is on another domain altogether, then appending attributes to the request is the only way to communicate this information. I'm not sure what your objection is to doing this, whether it's technical or otherwise; it's a very common technique and one that works well. There's a variety of methods to apply "cosmetic" tweaks to the process, such as a landing page to read the information from the URL, put it in the client's session on that site, and then replace the URL with a "cleaner" version (i.e. one with all the added parameters removed). Alternatively, POSTing a request will mean the attributes don't appear in the actual URL itself, and would make sense if the link represents some kind of action or non-repeatable (i.e. non-bookmarkable) state.

What problem exactly are you having with putting attributes in the URL?

Andrzej Doyle
Actually the url is having card name as well the image url which long ,i can append it but i dont want to do so?do i hv to use response.encodeurl?
sarah
I'm sorry, I don't understand exactly what you're trying to say in your comment. It does read to me, however, that you've simply repeated that you *can* append the query parameters but don't *want* to. Could you clarify *why* you don't want to? (And yes, you'd need to encode the parameters if they were to appear in the URL)
Andrzej Doyle
how to encode the parameters getting appended in url?
sarah
According to your platform's library method(s) to do this - in Java, it would be `java.net.URLEncoder.encode(inputParameter, "UTF-8")`.
Andrzej Doyle
sarah
Erm, yes you are. I suggest you read up on a step-by-step guideline of how URL encoding would be used and ask in a forum if you're still having problems; your case is *exactly* what URL encoding is designed for.
Andrzej Doyle
A: 

Sorry for the obvious question, is a POST request not possible? (e.g. having the link do a form.submit(), the form target will be your target URL, and the form method will be POST and your data inside hidden fields)

Ehrann Mehdan
+1  A: 

There are two ways to achieve this:

  1. Make use of a POST form.

    <form method="post" action="controller">
        <input type="hidden" name="name" value="">
        <input type="hidden" name="image" value="url">
        <input type="submit" value="click">
    </form>
    

    You can use CSS to style the button as if it's a link. Of course you need to implement the doPost() in the controller servlet accordingly.

  2. Make use of a "RESTFul" (and SEO friendly) URL pattern (ensure that your controller servlet is mapped on /controller/* instead of /controller).

    <a href="controller/name/url">click</a>
    

    in combination with the following in doGet() (of course along with some trivial checks to avoid runtimeexceptions like array index out of bounds):

    String pathInfoParts = request.getPathInfo().split("/");
    String name = pathInfoParts[1];
    String image = pathInfoParts[2];
    

    It's not clear what you actually mean with "url", but if it's a full http:// web URL, then you should consoder to just keep them in a map in the server side and just pass the map's key as parameter/pathinfo. Or if it is in your own context, just make it relative.

BalusC