1 Attachment(s)
I am unable to read radio button actions in JSP
I have a set of radio buttons.These radio buttons are associated with some questions for survey.I also have a Submit button.I want that as soon as I click the Submit button, the application should read the status of radio buttons.But I am unable to figure out a way of doing this.My code is shown bellow.I am also posting a snapshot of the screen that I am getting when I run this code(Just for little help in understanding).Please help me with providing code for doing this.
Attachment 688
Code :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="javax.swing.*" %>
<%@ page import="java.awt.*" %>
<html>
<head>
<title>display</title>
</head>
<body>
<h2>Data</h2>
<%
try {
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
System.out.println("loading class") ;
Class.forName("com.ibm.db2.jcc.DB2Driver");
System.out.println("after loading class");
connection = DriverManager.getConnection("jdbc:db2://localhost:50000/Simple","administrator","welcome2sa2");
System.out.println("after conncetion class");
System.out.println(connection.getClass());
statement = connection.createStatement();
String QueryString = "select * from question";
rs = statement.executeQuery(QueryString);
int i=1;
%>
<TABLE cellpadding="15" border="1" style="background-color: #ffffcc;">
<%
while (rs.next()&&(i<=4)) {
%>
<TR>
<TD><%=rs.getString(1)%></TD>
<TD><%=rs.getString(2)%></TD>
<TD><input type='radio' name="radio<%= i %>"><%=rs.getString(3)%></TD>
<TD><input type='radio' name="radio<%= i %>"><%=rs.getString(4)%></TD>
<TD><input type='radio' name="radio<%= i %>"><%=rs.getString(5)%></TD>
<TD><input type='radio' name="radio<%= i %>"><%=rs.getString(6)%></TD>
</TR>
<%i++; %>
<% } %>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
%>
<font size="+3" color="red"></font>
<%
out.println("Unable to connect to database.");
}
%>
</TABLE>
<script type="text/javascript">
function onsubmit()
{
document.write("Submitting");
return true;
}
</script>
<form name="myform">
<input type="submit" name="submit" value="Submit" align="middle" onclick="onsubmit();">
</form>
</body>
</html>
Re: I am unable to read radio button actions in JSP
There's quite a bit missing. What you need to do is to make an HTML form:
Forms in HTML documents
Bookmark that page and read it often.
Your form element should contain your input controls. You need to re-arrange your code so that the table containing your radio controls is enclosed by the form element. Choose a 'method' for your form - 'get' (the default) can be handy while you're developing because when you submit the form, the data is 'sent' back to the server as URL parameters so you can just eyeball the URL your browser uses to check to see if everything is working ok. It might look ugly in production. 'post' is usually more semantically correct (something is usually updated on the server).
Once you've changed your page so that it reliably sends the right data back to the server as URL parameters, you'll need to add some code to validate submitted form data before you build your page so that you can either update your database and (usually) redirect to a form-submitted-correctly page, or build the page with some indication of potential problems (user didn't complete all questions, for example).
I don't know JSP, but it looks like you're using it in an old-school way in which there should be some way of getting hold of a 'request' object which in turn would have a method for returning the values of named parameters in the current URL.
edit: this page looks as though it has everything you need:
http://www.roseindia.net/jsp/RequestObjectInJSP.shtml
I do something very similar to handle form data on my platform, but I usually use only one page / server-side entity to produce the original form and handle the input. That's a matter of taste, I guess.
Re: I am unable to read radio button actions in JSP
Quote:
Originally Posted by
Sean4u
There's quite a bit missing. What you need to do is to make an HTML form:
Forms in HTML documents
Bookmark that page and read it often.
Your form element should contain your input controls. You need to re-arrange your code so that the table containing your radio controls is enclosed by the form element. Choose a 'method' for your form - 'get' (the default) can be handy while you're developing because when you submit the form, the data is 'sent' back to the server as URL parameters so you can just eyeball the URL your browser uses to check to see if everything is working ok. It might look ugly in production. 'post' is usually more semantically correct (something is usually updated on the server).
Once you've changed your page so that it reliably sends the right data back to the server as URL parameters, you'll need to add some code to validate submitted form data before you build your page so that you can either update your database and (usually) redirect to a form-submitted-correctly page, or build the page with some indication of potential problems (user didn't complete all questions, for example).
I don't know JSP, but it looks like you're using it in an old-school way in which there should be some way of getting hold of a 'request' object which in turn would have a method for returning the values of named parameters in the current URL.
edit: this page looks as though it has everything you need:
Request Object In JSP
I do something very similar to handle form data on my platform, but I usually use only one page / server-side entity to produce the original form and handle the input. That's a matter of taste, I guess.
I will definitely keep the radio button in form syntax.But right now I m trying to configure the submit button.I am trying that as soon as submit is clicked it will transfer to a new file which I am taking as servlet.But in the program when I am clicking the submit it is only refreshing the existing page.My code for submit button is given below
Code :
<form name="myform" action="Collect.java" method="post">
<input type="submit" name="submit" value="Submit" align="middle" >
</form>
I am not understanding why it is not transferring to the servlet.I read somwhere that you need to create a web.xml file for matching.Is it required?What do we have to write in web.xml?
Re: I am unable to read radio button actions in JSP
This is why I mentioned 'get' and 'post' in my previous comment. When you 'post' a form from a web browser, you send a request to the bare URL specified in the FORM's ACTION attribute, but with HTTP request content:
HTTP/1.1: HTTP Message
Your form controls will be encoded and sent as parts of the HTTP request body.
If you use 'get' as your form method, you'll see your controls' data added to the URL as parameters ('query'). Try that and let us know how you get on.
Re: I am unable to read radio button actions in JSP
One other thing I notice from your previous post is the fragment of Java code. If you have a FORM element with only a submit button in it, nothing (except perhaps the name of the submit button) will be sent to the server because the form is empty. What you should have is
Code java:
<form name="myform" action="Collect.java" method="get">
<table>
<tr><!-- radio buttons for one question --></tr>
<tr><!-- radio buttons for another question --></tr>
</table>
<input type="submit" name="submit" value="Submit" align="middle" >
</form>
... like that
Re: I am unable to read radio button actions in JSP
Quote:
Originally Posted by
Sean4u
One other thing I notice from your previous post is the fragment of Java code. If you have a FORM element with only a submit button in it, nothing (except perhaps the name of the submit button) will be sent to the server because the form is empty. What you should have is
Code java:
<form name="myform" action="Collect.java" method="get">
<table>
<tr><!-- radio buttons for one question --></tr>
<tr><!-- radio buttons for another question --></tr>
</table>
<input type="submit" name="submit" value="Submit" align="middle" >
</form>
... like that
Thanks! Now its working fine.It is going to the servlet page on clicking the submit button.Thanks once again