href tags in jsp's and passing data by clicking on href tag

大城市里の小女人 提交于 2020-01-03 03:54:05

问题


this is my program

<form method="post">
Movie Name :<input type="text" name="movie"/><br>
Hero:<input type="text" name="hero"><br>
Director:<input type="text" name="dir"><br>

<a href="insert.jsp">Insert</a>
<a href="update.jsp">update</a>
<a href="delete.jsp">Delete</a>

when i click on the any of the href link the values of above text boxes should also to be carried to that particular jsp page i used getparameter method but i am not getting what i entered but they are taking null values


回答1:


This is because your href elements are simply redirecting to these other pages -- the form's data is scrapped.

What you want to do is allow the form to be submitted -- you will receive the values in your Servlet's doPost() implementation, and in that method, you should then redirect to the page by, perhaps, adding the values in the URL, for instance:

insert.jsp?movie=jaws&hero=hercules&director=spielberg

The values will then be available to that page.

EDIT: based on my latest comment, to do this without POST/servlets, your code would look like that with jQuery enabled:

<form method="get" id="myForm">
    Movie Name :<input type="text" name="movie"/><br>
    Hero:<input type="text" name="hero"><br>
    Director:<input type="text" name="dir"><br>

    <a href="insert.jsp">Insert</a>
    <a href="update.jsp">update</a>
    <a href="delete.jsp">Delete</a>
</form>
<script>
$('#myForm A').click(function(e)
{
   e.preventDefault(); // prevent the link from actually redirecting

   var destination = $(this).attr('href');
   $('#myForm').attr('action', destination);
   $('#myForm').submit();
});
</script>


来源:https://stackoverflow.com/questions/23914394/href-tags-in-jsps-and-passing-data-by-clicking-on-href-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!