How to use radio button in jsp page

This tutorial is about how to use radio button in jsp page.

How to use radio button in jsp page

How to use radio button in jsp page

     

This is detailed java code how to use radio button in jsp code and display a message in another jsp page according to selected radio buttons. First page in this example lets the user enter its name and select stream, branch and course type. On submission of the request, entered information is shown in the next page.

first_page.jsp

<!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.*" %> 
<html> 
    <head> 
        <title>Create table in mysql database using jsp</title>
    </head> 
    <body bgcolor="#ffffcc">
        <form action="show_data.jsp" method="get">
            <TABLE style="background-color: #ECE5B6;" WIDTH="50%">
                <tr width="100%">
                    <td >Student Name</td>
                    <td ><input type="text" name="name"></td>
                </tr>
                <tr>
                    <td width="20%">Stream</td>
                    <td width="25%"><input type="radio" name="stream" 
                        value="B.Tech.">B.Tech.</td>
                    <td width="30%"><input type="radio" name="stream" value="MCA" 
					    checked>MCA</td>
                    <td width="25%"><input type="radio" name="stream" value="MBA">
					    MBA</td>
                </tr>
                <tr>
                    <td>Branch</td>
                    <td><input type="radio" name="branch" value="CS" checked>
					    CS</td>
                    <td><input type="radio" name="branch" value="IT"> IT</td>
                    <td><input type="radio" name="branch" value="EC"> EC</td>
                </tr>
                <tr>
                    <td>Course Type</td>
                    <td><input type="radio" name="course" value="Regular" checked>
					    Regular</td>
                    <td><input type="radio" name="course" value="Correspondance">
					    Correspondance</td>
                    <td><input type="radio" name="course" value="Online"> Online</td>
                </tr>
				<tr><td></td><td></td>
				<td><input type="submit" name="submit" value="submit"></td></tr>
            </TABLE>
        </form>
    </body> 
</html>

Save this code as a .jsp file named "first_page.jsp" in your application directory ('user', in our case) in Tomcat.

show_data.jsp

<!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.*" %> 
<html>
    <head>
        <title>student informations</title>
    </head>
    <body>
       <font color="green" size="5" >Congratulations !</font>
        <h2><p>Welcome <%=request.getParameter("name")%>...<br></p></h2>
   		<TABLE style="background-color: #ECE5B6;" WIDTH="30%">
              <tr width="100%">
                             <tr>
                    <td width="50%">Stream</td>
                    <td width="50%"><%=request.getParameter("stream")%></td>
                </tr>
                <tr>
                    <td>Branch</td>
                    <td><%=request.getParameter("branch")%></td>
                </tr>
                <tr>
                    <td>Course Type</td>
                    <td><%=request.getParameter("course")%></td>
                </tr>
				<tr><td></td><td align="right">
			  <A HREF="first_page.jsp">
					<font size="4" color="blue">edit</font></A>
			</td>
	    </tr>
        </table>   
    </body>
</html>

Save this code as a .jsp file named "show_data.jsp" in your application directory ('user', in our case) in Tomcat.

To start Tomcat server, run startup.bat file from directory Tomcat-6.0.16/bin/startup.bat. To run this application open browser and type http://localhost:8080/user/first_page.jsp in address bar and hit enter key.

Fill Student name (For example, Mahendra Singh) and select stream, branch, course type according to student data then click submit button that will call another jsp page named "show_data.jsp", shows information of student that has been selected in first page.

To change or edit displayed information, click on link "edit".

Download source code