Below is the scenario..

First Page Second Page

Radio button 1
Radio button 2

Submit.
I select the first Radio Button and submit and then i go to second page where i have some content. Now when i go back to first page the same radio button which i had selected must be viewed. Need help to view the selected radio button when i navigate to the first page.

Following is the code..
getName.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<<HTML>
<BODY>
<FORM METHOD=POST ACTION="SaveName.jsp">
    <a  href="getName.html" ><font color="red">First Page</font></a>
    <a href="Second.jsp"><font color="red">Second Page</font></a><BR><BR>
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
<input type="radio" name="notify"  value="Yes" >Yes

<br>
  <input type="radio" name="notify" value="No">No
  <br>
<P><INPUT TYPE=SUBMIT VALUE="Submit">
</FORM>
</BODY>
</HTML>

SaveName.jsp

<jsp:useBean id="user" class="User.UserData" scope="session"/>
<jsp:setProperty name="user" property="*"/>
<%
   String name = request.getParameter( "username" );
   session.setAttribute( "theName", name );
%>

<HTML>
<BODY>
You entered<BR>
Name:   <%=user.getUsername()%><br>
Choice: <%=user.getNotify()%>

</BODY>
</HTML>

UserData.java

package User;
public class UserData  {
String username;
 private String notify;
 
    public void setUsername( String value )
    {
        username = value;
    }
 
  
    public String getUsername() { return username; }
 
   
    public String getNotify() {
        return notify;
    }
 
    
    public void setNotify(String notify) {
        this.notify = notify;
    }
 
}

Please help..

nipa.pilla,

I think following block has no importance.

<%
   String name = request.getParameter( "username" );
   session.setAttribute( "theName", name );
%>

In SaveName.jsp -a UserData bean is already been added into the session scope.

You may write "checked" attribute for radio buttons based upon the value of notify.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<BODY>
    <%
      User.UserData usr=(User.UserData)session.getAttribute("user");
      String rad1="";
      String rad2="";         
      if(usr!=null)  
        {
          if(usr.getNotify().equals("Yes"))
             rad1="checked='checked'";
           else
             rad2="checked='checked'";
         }
     %>
<FORM METHOD=POST ACTION="SaveName.jsp">
    <a  href="getName.html" ><font color="red">First Page</font></a>
    <a href="Second.jsp"><font color="red">Second Page</font></a><BR><BR>
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
<input type="radio" name="notify" <%=rad1%>  value="Yes" >Yes
<br>
  <input type="radio" name="notify" <%=rad2%> value="No">No
  <br>
<P><INPUT TYPE=SUBMIT VALUE="Submit">
</FORM>
</BODY>
</HTML>
<jsp:useBean id="user" class="User.UserData" scope="session"/>
<jsp:setProperty name="user" property="*"/>
<HTML>
<BODY>
You entered<BR>
Name:   <%=user.getUsername()%><br>
Choice: <%=user.getNotify()%>
<a href="GetName.jsp">Show</a>
</BODY>
</HTML>

Hi Adapost,
Thanks a lot for your reply... And the code worked..! I am new to Java and Jsp and have been working on this since last two days and finally i thought to post it in a forum..:)

Hi Adapost,
Thanks a lot for your reply... And the code worked..! I am new to Java and Jsp and have been working on this since last two days and finally i thought to post it in a forum..

Thanks,

Mark this thread as "Solved" (a link at bottom of the page) if you get solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.