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 9 of 9

Thread: checkbox not working right

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default checkbox not working right

    forgive me , but im not sure if this is the right forum for servlet help. im getting to that desperate state with my project , my teacher is no help at all and im hoping to get some help here.

    im trying to finish this servlet that adds a new course to my courses display. im using a check box so the user can pick what the prerequisite are for the new course they want to add. so if they check cs201 , it will add cs201 as a prerequisite for that new course.

    for some reason my checkbox will only add the first checkbox that is clicked. like if i add a subject , and click the check boxs for cs203 , cs202, and cs201. it will only grab cs201 and add it to my course display. it wont grab all 3. how can i grab the other info thats checked?

    package cs320Homework1.servlet;
     
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.List;
     
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import cs320Homework1.servlet.Courses;
    @WebServlet("/AddCourse")
    public class AddCourse extends HttpServlet {
    	private static final long serialVersionUID = 1L;
     
    	public AddCourse() {
    		super();
     
    	}
     
    	protected void doGet(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
     
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    		out.println("<html><head><title>AddCourse</title></head><body>");
    		out.println("<form action='AddCourse' method ='post'/> ");
    		out.println("Code: <input type='text' name='code'/> <br /> ");
    		out.println("Title: <input type='text' name='title' /> <br />");
    		out.println("Prerequisite(s): <input type='checkbox' name='prerequisites value='CS120' >CS 120<br>");
    		out.println("Prerequisite(s): <input type='checkbox' name='prerequisites value='CS122' >CS 122<br>");
    		out.println("Prerequisite(s): <input type='checkbox' name='prerequisites value='CS201' >CS 201<br>");
    		out.println("Prerequisite(s): <input type='checkbox' name='prerequisites value='CS202' >CS 202<br>");
    		out.println("Prerequisite(s): <input type='checkbox' name='prerequisites value='CS203' >CS 203<br>");
    		out.println("Prerequisite(s): <input type='checkbox' name='prerequisites value='CS320' >CS 320<br>");
            out.println("<input type='submit' name='add' value='Add' /> <br />");
     
     
    		out.println("</form>");
    		out.println("</body></html>");
    	}
     
    	@SuppressWarnings("unchecked")
    	protected void doPost(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
     
    		String code = request.getParameter("code");
    		String title = request.getParameter("title");
    		String  prerequisites= request.getParameter("prerequisites");
     
    		List<Courses> entries = (List<Courses>) getServletContext()
    				.getAttribute("entries");
     
    		entries.add(new Courses (code,title, prerequisites));
     
    		response.sendRedirect( "Courses" );
     
    	}
     
    }


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: checkbox not working right

    Quote Originally Posted by zerocool18 View Post
    		out.println("Prerequisite(s): <input type='checkbox' name='prerequisites value='CS120' >CS 120<br>");
    		out.println("Prerequisite(s): <input type='checkbox' name='prerequisites value='CS122' >CS 122<br>");
    		out.println("Prerequisite(s): <input type='checkbox' name='prerequisites value='CS201' >CS 201<br>");
    		out.println("Prerequisite(s): <input type='checkbox' name='prerequisites value='CS202' >CS 202<br>");
    		out.println("Prerequisite(s): <input type='checkbox' name='prerequisites value='CS203' >CS 203<br>");
    		out.println("Prerequisite(s): <input type='checkbox' name='prerequisites value='CS320' >CS 320<br>");
    First, your HTML output seems not correct. You should put a " ' " after prerequisites.

    out.println("Prerequisite(s): <input type='checkbox' name='prerequisites' value='CS320' >

    Quote Originally Posted by zerocool18 View Post
    		String  prerequisites= request.getParameter("prerequisites");
    Since you have the same name "prerequisites" but with more different values, getParameter is not sufficient. You need to get all the values for that parameter name:

    String[] prerequisites = request.getParameterValues("prerequisites");
    The prerequisites array will contains only the values for the checked boxes.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: checkbox not working right

    alright , thhat means i have to change my contructor to accept arrays right?

     
    entries.add(new Courses (code,title, prerequisites));

    so i changed my contructor to

    public Courses(int id, String code, String title, String[] prerequisites2) {
            this.id=id;
    		this.code = code;
    		this.title = title;
    		this.prerequisites = prerequisites2;
     
    	}

    but now my data entries get errors

    	entries.add(new Courses(6, "CS320", "Web and Internet Programming",
    				"CS120 CS122 CS203"));

    god , changing prerequisite to an array , created alot more porblems lol

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: checkbox not working right

    Quote Originally Posted by zerocool18 View Post
    entries.add(new Courses (code,title, prerequisites));

    public Courses(int id, String code, String title, String[] prerequisites2) {
            this.id=id;
    		this.code = code;
    		this.title = title;
    		this.prerequisites = prerequisites2;
     
    	}
    Here I see a constructor with 4 parameters but in the new Courses (code,title, prerequisites) you pass 3 arguments.

    How many constructors do you have? Several in overload? Then it's ok.

    Quote Originally Posted by zerocool18 View Post
    	entries.add(new Courses(6, "CS320", "Web and Internet Programming",
    				"CS120 CS122 CS203"));
    If you want to create a Courses with "fixed" values, it's obvious that you can't pass "CS120 CS122 CS203" for an array.

    At least pass new String[] { "CS120", "CS122", "CS203" }
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: checkbox not working right

    alright , it works but the only thing i that the output comes out as [Ljava.lang.String;@16fd116 for my prerequisites. how can i fix it so it displays CS120


    entries.add(new Courses(1, "CS120",
    				"Introduction to Web Site Development", new String[] { "CS120", "CS122", "CS203" }));

  6. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: checkbox not working right

    Quote Originally Posted by zerocool18 View Post
    alright , it works but the only thing i that the output comes out as [Ljava.lang.String;@16fd116 for my prerequisites.
    static method toString of java.util.Arrays. It formats an array into a string with a fixed format like [xxx,yyy ...... ]

    If you don't like this format, use a simple for loop to print or format a string with all values.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: checkbox not working right

    alright , so i was goin at this program all night and i ended sleeping after i couldnt get it to work. so far im at this point

    now i know i need to use an array to get out all the values. so i did

    String[] prerequisites = request.getParameterValues("prerequisites");

    now with that comes more problems lol. Im assuming i have no choice but to redo my Constructor from

    public Courses(int id, String code, String title, String prerequisites) {
            this.id=id;
    		this.code = code;
    		this.title = title;
    		this.prerequisites = prerequisites;
     
    	}
    to this right?

    public Courses(int id, String code, String title, String [] prerequisites) {
            this.id=id;
    		this.code = code;
    		this.title = title;
    		this.prerequisites = prerequisites;
     
    	}

    so now with that updated , i dont know what do next. is that all i have to do for it to be able to get all the checkbox values? if thats right then all thats left to do is to update my entries. like i did below. but whan those print out to the display. i get some weird characters

    entries.add(new Courses(1, "CS120",
    				"Introduction to Web Site Development", new String[]{"CS201, CS202"}));

    i get [Ljava.lang.String;@6fbb45 , in my prerequiite table dipsplay. i tried using toString , but it i get errors that i dont know how to fix.

    entries.add(new Courses(1, "CS120",
    				"Introduction to Web Site Development",Arrays.asList(new String[]{"c203"}).toString());

  8. #8
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: checkbox not working right

    Quote Originally Posted by zerocool18 View Post
    to this right?

    public Courses(int id, String code, String title, String [] prerequisites) {
            this.id=id;
    		this.code = code;
    		this.title = title;
    		this.prerequisites = prerequisites;
     
    	}

    so now with that updated , i dont know what do next. is that all i have to do for it to be able to get all the checkbox values?
    For what I can know/understand, at conceptual level, yes a "course" can have many prerequisites, so it has sense to keep id, code, title and N prerequisites (as an array).

    And note: your class should be Course ... not Courses, it models 1 course, not more.

    Quote Originally Posted by zerocool18 View Post
    if thats right then all thats left to do is to update my entries. like i did below. but whan those print out to the display. i get some weird characters

    i get [Ljava.lang.String;@6fbb45 , in my prerequiite table dipsplay
    All arrays derive from java.lang.Object but they don't redefine toString(), so remains the toString() inherited from Object, that can print only this format you have noticed.

    And I repeat: see Arrays.toString.
    Or do a simple for loop to print or compose a string.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  9. #9
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: checkbox not working right

    Hi,

    Please could you once again post the recent code? Please ignore this if the issue has been fixed already and you are happy with the code and output.

    --- Update ---

    There is something I would like you to know. A Servlet never understands a constructor which you have mentioned in your program. Servlet life cycle covers init, service methods and destroy. So, code written in constructors are un-used codes.
    Thanks and regards,
    Sambit Swain

Similar Threads

  1. checkbox on a JTable
    By jetjet21faja in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 1st, 2013, 08:11 AM
  2. Flickering Checkbox in jtable ?
    By harshilshah in forum AWT / Java Swing
    Replies: 12
    Last Post: April 26th, 2013, 06:03 AM
  3. own Tablemodel with checkbox
    By Voldimore in forum AWT / Java Swing
    Replies: 4
    Last Post: February 23rd, 2012, 06:19 AM
  4. See if checkbox is checked
    By kney in forum Java Theory & Questions
    Replies: 9
    Last Post: December 7th, 2011, 07:23 AM
  5. how to delete record based on checkbox selected checkbox
    By -_- in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: December 15th, 2009, 09:26 PM