Oumayma Bouzidi
Oumayma Bouzidi

Reputation: 11

getParameter returns null from Method get

I'm tryring to get an id from url but getParameter return null this is how I'm sending the id in the url (tool.jsp):

<a href="http://localhost:8080/Projectpfeweb/executetool.jsp?id=${l.tool_id}" class="btn btn-info" role="button">Execute</a>

this the doGet method where I want the id value

 protected void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
      ToolDAO dao=new ToolDAO();

    String id= req.getParameter("id");

    Tool t=dao.getToolById(Integer.parseInt(id));
      String first = req.getParameter("first");
     byte[] bytes = first.getBytes(StandardCharsets.ISO_8859_1);
     first= new String(bytes, StandardCharsets.UTF_8);
   if(first!=null)
   {
    String [] input=first.split(" ");
      System.out.println("input"+input[0]);
      EXEJAVA exe=new EXEJAVA();
      FileRead f=new FileRead();

      f.writeinputfile(input);
       ArrayList l=exe.executetool(t.getTool_path_exe());

       req.setAttribute("l",l);
       req.setAttribute("first", first);
       req.getRequestDispatcher("executetool.jsp").forward(req, res);
}

and this is the form (executetool.jsp)

 <form accept-charset="UTF-8" name="form" action="executetool" method="get">

<div class="centre">
<div class="row">
<div class="inline-div">
  <label for="ta1">Text in Latin script</label>
<textarea cols="60" rows="15" id="first" name="first" class="inline-
txtarea">${first}</textarea>
</div>
&nbsp;
<div class="inline-div">
<input type="button" value="Clear" onclick="javascript:eraseText();"> 
  </div>
  &nbsp;
  <div class="inline-div">
    <label for="ta2" >Text in Arabic script</label>
    <textarea cols="60" rows="15" id="second" name="second" class="inline-
    txtarea"><c:forEach items="${l}" var="s">${s}&nbsp;</c:forEach>
    </textarea>
     </div>
     </div>
   </div>

     </form>

Since it's method get the url keeps changing everytime the page is refreshed and so the "id=something" part gets replaced by the value of the two text areas that I have in the form what sould I do to always keep that part in the url?

Upvotes: 0

Views: 166

Answers (1)

Wax
Wax

Reputation: 323

Place a hidden field instead

<input type='hidden' name='id' value='${l.tool_id}'>

Then use input type submit for the button, not a generic <a> tag as that won't submit the form unless you have a javascript code somewhere that will submit the form for you.

You can also place the id in the action attribute of the form.

<form accept-charset="UTF-8" name="form" action="executetool?id=${l.tool_id}" method="get">

Upvotes: 1

Related Questions