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

Thread: How do I set an Entity to read in another Entity which has a List<String> in Servlet?

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I set an Entity to read in another Entity which has a List<String> in Servlet?

    I have 2 entities - tutor and Subject.

    At the controller, I have problem with the code where I need to set the tutor to get the list of subjects which are in String.

    Here's the snippet of code in the servlet:

    tutor m4 = new tutor();

    String[] subj = request.getParameterValues("subject");

    List<string>subjects = new ArrayList<string>(Arrays.asList(subj));

    for(String subject : subjects){ //next() returned showed values
    System.out.println(subject);//no explicit return value
    // m4.setSubject(subject); //setSubject() returned null
    //m4.setSubjlist(subjects);
    m4.setSubjs((Subject) subjects);

    I have tried various ways to set the subject at the class without avail.

    The error message // shows that the tutor is not reading in the values



    Here's how I did the setter at tutor class



    public class tutor{
    private String tutorName;
    tutor subj;
    public tutor setSubject(String subject) {
    return this.subj;
    }


    My question is how do I write the setter in tutor Class in order for the code to work in the Servlet.

    Can this be done using pure Java at all? I heard there is this so called dependency injection but I am not sure how to make use of it in my case. Hope someone can also shed some light on this. Tks.

    I am using Java EE7 and jdbc, 1.8 Java SE and Tomcat.

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: How do I set an Entity to read in another Entity which has a List<String> in Servlet?

    Not certain what you're trying to do but here is how setters and getters work.

    public class Demo {
        private String value;
     
        public void setValue(String val) {
            value = val;
        }
     
        public String getValue() {
            return value;
        }
    }

    Regards,
    Jim

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I set an Entity to read in another Entity which has a List<String> in Servlet?

    Quote Originally Posted by jim829 View Post
    Not certain what you're trying to do but here is how setters and getters work.

    public class Demo {
        private String value;
     
        public void setValue(String val) {
            value = val;
        }
     
        public String getValue() {
            return value;
        }
    }





    Regards,
    Jim
    I think you have never done servlet or many to many using pure Java and JDBC so it will be difficult for you to understand about my problem.

    Your above code is only good for normal single table where I have no problem in doing the getter and setter.

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: How do I set an Entity to read in another Entity which has a List<String> in Servlet?

    I actually have done some servlet programming in conjunction with JSP but not very much. And I have not used any JDBC so you are correct there.

    Regards,
    Jim

Similar Threads

  1. I/O warning : failed to load external entity "wineet.xml"
    By sjipke in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 6th, 2014, 12:05 PM
  2. Convert List<Set<String>> to string array
    By shatlav in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 4th, 2013, 08:16 AM
  3. Replies: 0
    Last Post: July 27th, 2011, 10:31 AM
  4. How To Retain Entity Reference Character While Reading XML
    By leon850515 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 2nd, 2011, 09:10 AM

Tags for this Thread