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

和自甴很熟 提交于 2019-12-06 21:03:32

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