• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

problem with form submission

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
arpit singla
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For radio buttons and checkboxes, you want to check the "checked" state.
if(yourControlName.checked)
doWhatEver....

Bosun
 
arpit singla
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!