Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Using html Radio Buttons with Servlets

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using html Radio Buttons with Servlets

    Hello all,

    I'm working through some examples of java servlets. I have one example of a servlet taking text input, the lines of code I have an example of are below, this example uses a html text area to submit input:

    String text = req.getParameter("text");
            String translation = translate(text);
            res.setContentType("text/html");
            ServletOutputStream out = res.getOutputStream();

    I am doing my own example working with html radio buttons and was thinking of altering the code to how it is below, my questions concern mainly the first 4 lines:

    String colour = req.getParameter("colour");
            res.setContentType("radio/html");
            ServletOutputStream out = res.getOutputStream();
    		out.println("<html>");
    			out.println("<body>");
    			out.println("<head><title>Change Colour</title></head>");
    			out.println("<h1><p>Background Colour</p></h1>");
    			out.println("<p>What colour do you want the background?</p>");
    			out.println("<form>");
    			out.println("Yellow <input type=\"radio\" name=\"colour\" value=\"Yellow\" /> </br>");
    			out.println("Green <input type=\"radio\" name=\"colour\" value=\"Green\" /> </br>");
    			out.println("Blue <input type=\"radio\" name=\"colour\" value=\"Blue\" /> </br>");
    			out.println("<input type=submit value=\"Change Background\"/>");
    			out.println("</form>");
    			out.println("</body>");
    			out.println("</html>");

    All I would like to know is am I passing the correct parameter into the HttpServletResponse method getParameter()? Colour is the name of the radio button group. Am I write in making the object a string? Finally once I get the parameter will I be able to check against it if (colour == "Yellow") for example. Any help much appreciated.
    Last edited by oss_2008; June 25th, 2009 at 04:26 AM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Using html Radio Buttons with Servlets

    Hello oss_2008,

    I have moved this thread over to the Java Servlet forum:

    Java Servlet - Java Programming Forums

    That all looks good to me. Yes I think it will be OK to declare colour as a String. Are you getting any errors?

    Rather than:

    if(colour == "Yellow")

    I think you will need to use:

    if(colour.equals("Yellow"))
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using html Radio Buttons with Servlets

    Thank you for your help. I'm not getting any errors as I haven't compiled it yet. I was just checking I had understood the concept correctly.

Similar Threads

  1. How to Grab the HTML source code of a website URL index page?
    By JavaPF in forum Java Networking Tutorials
    Replies: 6
    Last Post: April 22nd, 2010, 02:46 PM
  2. Replies: 1
    Last Post: May 19th, 2009, 09:19 AM
  3. [SOLVED] How to link two servlets?
    By bhmadhukar in forum Java Servlet
    Replies: 4
    Last Post: May 19th, 2009, 08:24 AM