2

When I click on a link in jspA, it will redirect to jspB with query string src. The message for src will be displayed in jspB without problem. However, why I tried to click on the submit, I am unable to retrieve the value of src in my servlet page. Is there a way for me to retrieve the value of src in servlet? Thanks.

Inside my jspB page:

<img src="<%= request.getParameter("src") %>" />
<table>     
    <form name="frmTest" action="test" method="post">
        <input type="submit" value="sub" name="sub" />
    </form>
</table>

Inside my Servlet test:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException{

    String imgUrl = req.getParameter("src");

I am retrieving null value from the imrUrl.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Sky
  • 163
  • 2
  • 4
  • 15

2 Answers2

2

When you submit an html form, only the input and select elements are sent as parameters. You don't have any that have a name attribute set to src.

You can use a hidden input

<form name="frmTest" action="test" method="post">
    <input type="submit" value="sub" name="sub" />
    <input type="hidden" name="src" value="<%= request.getParameter("src") %>" />
</form>

It is generally discouraged to use scriptlets. Read up on JSTL and EL and use those technologies instead.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Thanks and the hidden field solved my issue. I have changed to ${param.src} too. – Sky Sep 21 '13 at 21:13
0

I'm assuming you mean Submit from jspB? If so, you need to store the src value in a hidden field in the form so it is available when the servlet is called on submit. Something like the following

<form name="frmTest" action="test" method="post">
    <input type="hidden" value="<%= request.getParameter("src") %>" name="src" />
    <input type="submit" value="sub" name="sub" />
</form>

PS You should avoid using scriptlet (i.e. the code between <% and %>) and instead use jsp expression language

Simon Arsenault
  • 1,241
  • 11
  • 12