0

I got a requirement to send one parameter through url pattern of servlet (like we send through <a href="example.jsp?id=1">send</a>) in the same way I need it through url pattern.

I do this with other possibilities like

  1. I can send that parameter as hidden type
  2. i can put in a request and session objects

these methods are working fine no problem

but through url it's not taking? I want to know whether it is possible or not?

the code I have tried

jsp page

<a href="download?filename=<%=filename%>" target="_blank"> <font color="black"><%=filename%> </font></a>

servlet code

 String  filename=request.getParameter("filename");

and i need one answer can we pass parameter through url pattern if yes how? i.e like same as through <a href="example?id=1">send</a> or differently?

  • so what is the actual filename being passed? If you call it in your browser's address bar, is the result the same? – Scary Wombat Mar 25 '15 at 05:40
  • in jsp what is the value of `filename`?? you can also use JavaScript!! – Prashant Mar 25 '15 at 05:51
  • here filename is string which is dynamically changes –  Mar 25 '15 at 05:56
  • you should check the url generated or use inspect element to see if `filename` is empty – singhakash Mar 25 '15 at 05:57
  • just i need one answer can we pass parameter through url pattern if yes how? i.e like same as through send or differently? –  Mar 25 '15 at 05:59
  • "output is blank page", you are getting the filename in servlet(on server side), so how you are getting blank page? – Arpit Aggarwal Mar 25 '15 at 06:05
  • @Arpit Here i will download the file in servlet by selecting the file from localpath based on the filename we are sending you can see the code for href target is blank that's why out pu is blank if file downloads then also it displays blank page but here file is not downloading –  Mar 25 '15 at 06:09
  • What I understood is that you on click of anchor tag you want to implement the download functionality and file name is dynamic and is placed in your local? – Arpit Aggarwal Mar 25 '15 at 06:12
  • Exactly @Arpit and my doubt is written 2nd comment –  Mar 25 '15 at 06:14
  • yes you can pass the query params as part of URL in form of GET request. – Braj Mar 25 '15 at 06:17
  • so your question "passing parameter from jsp to servlet through url pattern of servlet" should be changed how to download a file using servlet. As passing a filename from JSP to servlet will not download a file for you. You have to read a file using InputStream on server side and send it setting in response using proper Content type. – Arpit Aggarwal Mar 25 '15 at 06:18
  • what are you getting at server side inside servlet? Debug the code. – Braj Mar 25 '15 at 06:18
  • @Arpit file downloading is working i mentioned it in question 2 ways are working fine. main problem is it's not getting the parameter in servlet if i pass through url pattern –  Mar 25 '15 at 06:32

4 Answers4

1

I am just trying to give you an example

.jsp FILE

<% String filename ="nameofFile.txt"; %>
<a href="download?filename=<%= filename %>" ></a>

SERVLET CODE

String filename = (String)request.getParameter("filename");
BufferedReader fir= new BufferedReader(new FileReader(new FileInputStream(filename))); 
PrintWriter out = response.getWriter();
while(fir.ready())
out.println(fir.readLine())

I think you are getting blank page because you are not sending any response back to the client, here out.println will actually send response back to the client

saikumarm
  • 1,565
  • 1
  • 15
  • 30
1

Always encode the URL. In JSP try with <c:url> JSTL Tag.

For example

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<a href='<c:url value="/jsp/index.htm"/>'>TEST</a>

Read more..

Note: Use JSTL and EL instead of Scriplets.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
0

It is possible but it looks like your browser clears parameters after ?... in action="..." attribute. In that case try passing it via <input type="hidden" .../> like

<form action="sendFileToServlet" method="get">
    <input type="hidden" name="filename" value="<%=filename%>"/>
    <input type="submit" value="Send" />
</form> 

This way form should add them to URL as ?filename= value of <%=filename%>.

Junaid
  • 2,572
  • 6
  • 41
  • 77
  • I know this thing and i mentioned it in question as 1st way to send the parameters –  Mar 25 '15 at 06:17
0

Yes you can send as like trough jsp.

i just tested now it's working fine the blank page is coming because of other statements written in your servlet code so make sure servlet code is correct.

  • thanks @Mahender Reddy Yasa I solved it another statement giving nullpointer exception i removed it. –  Mar 25 '15 at 06:46