Mukesh
Mukesh

Reputation: 182

Passing request parameter with href without they visible in request (Making href Post instead of Get)

I have a html table in my JSP where one column is a anchor element.On click of this anchor tag I need to call a Servlet.In this request I append the file name to the as a request parameter along with the servlet call. like

<table>
<tr><td>
<a href="/FileDownLoadServlet.dwnld?fileName=Test1.pdf&key=123456" target="_blank">Test1Container</a>
<td></tr>
<tr><td>
<a href="/FileDownLoadServlet.dwnld?fileName=Test2.pdf&key=789123" target="_blank">Test2Container</a>
<td></tr>
</table>

The servlet in writes the file to response so the respective pdf gets opened in new tab.

The issue is the new tab that is opened in its address bar the entire URL is getting showed up.May be because of GET request if href. I want to avoid this and show the original URL without the name of servlet and its request parameters.May be if possible showing only file name in the address bar after the context name will do. Please suggest.

Upvotes: 0

Views: 3138

Answers (1)

Ahsan
Ahsan

Reputation: 1084

JS Fiddle: http://jsfiddle.net/9qacod03/

<form id = "pdfViewer" method = "post" action = "/FileDownLoadServlet.dwnld" target="_blank">
  <input type = "hidden" name = "fileName" value = "" />
  <input type = "hidden" name = "key" value = "" />
</form>

<table>
  <tr>
    <td>
      <a class = "pdfLink" fileName = "Test1.pdf" key = "123456" href = "#">Test1Container</a>
    </td>
  </tr>
  <tr>
    <td>
      <a class = "pdfLink" fileName = "Test2.pdf" key = "789123" href = "#">Test2Container</a>
    <td>
  </tr>
</table>

$(".pdfLink").click(function () {

  var fileName = $(this).attr("fileName");
  var key = $(this).attr("key");

  $("#pdfViewer").find("[name=fileName]").val(fileName);
  $("#pdfViewer").find("[name=key]").val(key);

  $("#pdfViewer").submit();

});

Upvotes: 1

Related Questions