wood burning stoves 2.0*
The moose likes Servlets and the fly likes problem with form submission Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem with form submission" Watch "problem with form submission" New topic
Author

problem with form submission

arpit singla
Ranch Hand

Joined: Jun 18, 2001
Posts: 66
A servlet passes parameters to a jsp. this jsp is using javascript. there is a button in this jsp "Evaluate" which when clicked i want to update the grandtotal.but when i click the button, gives me nullpointer exception because values passed from servlet are null. I dont know when i click Evaluate, why it gets the parameters from servlet again. i am not even submitting the form again.
my jsp:
<html>
<head>
<title> Check Out</title>
<script language="JavaScript">
function mode(field)
{
var val = field.value;
document.write(val);
}
</script>
</head>
<body>
<%
// code for getting values from Servlet
%>
<form name="chkout">
<table>
<tr>
<input type="radio" name="Overnight" value="overnight"> Overnight<br>
<input type="radio" name="Overnight" value="snail"> 3-5 days<br>
<input type="submit" name="evaluate" value="Evaluate" onClick="mode(Overnight)">
</td>
</tr>
</table>
</form>
</body>
</html>
if sb. could tell me what i am doing wrong and if could suggest me a right way to do it...
thanks a lot in advance.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
I think the first step would be to examine the generated HTML to see if the values are really there. Do you have a prototype in plain HTML that works correctly?
Bill

------------------
author of:
DAYANAND BURAMSHETTY
Ranch Hand

Joined: Aug 06, 2001
Posts: 34

servletClass
|
|
myjsp
|
|
chkoutServlet
//servlet.........
class servletClass{
.....
.....
public void forward(HttpServletRequest req,HttpServletResponse res,String url)
throws ServletException, IOException{
res.setContentType("text/html");
RequestDispatcher rd = getServletContext()
.getRequestDispatcher (url);
rd.forward(req, res);
}
public void service(HttpServletRequest req, HttpServletResponse res)throws IOException,ServletException{


//....
String overnight="Overnight";
String snail="3-5 Days";
req.setAttribute("Overnight" , overnight);
req.setAttribute("snail" , snail);
this.forward(req,res,"my.jsp");
}
}

my.jsp
-------------------
<html>
<head>
<title> Check Out</title>
<script language="JavaScript">
function mode(field)
{
var val = field.value;
document.write(val);
}
</script>
</head>
<body>
<%
String overnight=(String)request.getAttribute("Overnight");
String snail=(String)requset.getAttribute("snail");
%>
<form name="chkout" action="../chkoutServlet">
<table>
<tr>
<input type="radio" name="Overnight" value="<%=overnight%>"> Overnight<br>
<input type="radio" name="Snail" value="<%=snail%>"> 3-5 days<br>
<input type="submit" name="evaluate" value="Evaluate" onClick="mode(Overnight)">
</td>
</tr>
</table>
</form>
</body>
</html>

---------------------
class chkoutServlet{
.....
....
public void service(HttpServletRequest req, HttpServletResponse res)throws IOException,ServletException{
......
String overnight=request.getParameter("Overnight");
String snail=request.getParameter("Snail");
......
}

}
If I have done wrongly...I am very sorry and correct me


Dil se....,<BR>Dayanand<BR>0065-8839071(off)<BR>0065-7547034(Res0
arpit singla
Ranch Hand

Joined: Jun 18, 2001
Posts: 66
dayanand, Bill
thanks for responding..
i found out that when i click "Evaluate" button, form is being submitted which i dont want. So I made Evaluate a simple button which calls a javascript.

<input type="radio" name="Overnight" value="overnight"> Overnight<br>
<input type="radio" name="Overnight" value="snail"> 3-5 days<br>
<button type="button" name="evaluate" onClick="mode()">Evaluate</button>

now when i click Evaluate button, i want to know which radio field was selected, thats why I kept the name of both radio same.

now problem is :
document.myform.Overnight.value; does not seem to return anything.......
my javascript function is :
function mode()
{
var test = document.myform.Overnight.value;
if( test == "overnight")
{
alert('overnight');
}
else if( test == "snail")
{
alert('snail');
}

}
i donot get the alerts when any of the radio is selected.. please let me know the reason..
thanks to you all.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
For radio buttons and checkboxes, you want to check the "checked" state.
if(yourControlName.checked)
doWhatEver....

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
arpit singla
Ranch Hand

Joined: Jun 18, 2001
Posts: 66
bosun
how do i retrieve the value of the checkbox.
I have following 2 checkboxes:
<input type="radio" name="Overnight" value="overnight"> Overnight<br>
<input type="radio" name="Overnight" value="snail"> 3-5 days<br>
document.myform.Overnight.checked, will let me know that checkbox is selected but wouldn't tell me which checkbox ?? how can i detemine which checkbox is selected ?
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with form submission
 
Similar Threads
jsp file with java script error?
servlets/jsp
setting a radio button
radio button script error?
radio button error problem?