Icche Guri Icche Guri - 8 months ago 99
HTML Question

how can I pass data by clicking on href tag in jsp

I am in

index.jsp
and I want to pass some data after clicking
href
tag. The code of this page is as following:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="js/jquery-3.2.1.min.js"></script>
<style>
a{
padding: 10px !important;width:200px;
text-decoration:none !important;
background-color: #0B70BE;
background-image: -moz-linear-gradient(center center , rgb(11, 112, 190) 0%, rgb(11, 112, 190) 100%) !important;
border-radius: 2px !important;
border: 2px solid rgb(43, 125, 185) !important;
font-weight: bold;
font-size: 13px !important;
font-family: Helvetica Neue,Helvetica,Arial,sans-serif;
height: 26px !important;
text-shadow: none !important;
color: rgb(255, 255, 255) !important;
}
</style>
<script type="text/javascript">
$('#myForm1').click(function (e)
{
e.preventDefault(); // prevent the link from actually redirecting
$('#myForm1').submit();
});
</script>
</head>
<body>
<div>TODO write content</div>
<%
String name = "1231";
int rollNo = 121;
%>

<form method="get" id="myForm1" action="hello.jsp">
<Input type="Hidden" name="name" id="name" value="<%=name%>">
<Input type="Hidden" name="rollNo" id="rollNo" value="<%=rollNo%>">
<a href="hello.jsp">Please Enroll Finger 1</a>
</form>

</body>
</html>


After clicking
href
tag, I will go to
hello.jsp
. In that page, I want to retrieve
name
and
rollNo
value. So I have this code for
hello.jsp
page:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%
String name = request.getParameter("name");
String rollNo = request.getParameter("rollNo");
out.println("Student name "+name+"<br>");
out.println("Student Roll No is "+rollNo);

%>
</body>
</html>


After clicking
href
tag in first page, I go to
hello.jsp
page. But in 2nd page, I am getting the value of
name
and
rollNo
as null. I have got the following output:

Hello World!

Student name null
Student Roll No is null


I have taken help from this link . I cant understand why I am getting null value in 2nd page. How can I resolve this error? Please help me.

Updated Information



I have inspected code of
index.jsp
. THe code is as follows :

enter image description here

Answer Source

The hidden values are inside the form, so you need a form submit to submit the values to the next page. If you directly put a page in the href attribute, the browser will go to that page without doing any form submission.

Therefore, you need to put javascript code in href instead to trigger the form submit action, like below.

    <form method="get" id="myForm1" action="hello.jsp">
        <Input type="Hidden" name="name"  id="name" value="<%=name%>"> 
        <Input type="Hidden" name="rollNo"  id="rollNo" value="<%=rollNo%>"> 
        <a href="$('#myForm1').submit();">Please Enroll Finger 1</a> 
    </form>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download