开发者

Passing data from one page to other on click of a hyperlink

I have a link, on click of which I have to got开发者_Go百科 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.


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?


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜