Forum Stats

  • 3,853,749 Users
  • 2,264,264 Discussions
  • 7,905,440 Comments

Discussions

how do I set an entity to read in a list<string> from another entity?

User_DTTUY
User_DTTUY Member Posts: 5 Blue Ribbon
edited Dec 12, 2018 2:26PM in Java 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<br/>System.out.println(subject);//no explicit return value<br/>// m4.setSubject(subject); //setSubject() returned null<br/>//m4.setSubjlist(subjects);<br/>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{<br/>private String tutorName;<br/>tutor subj;<br/>public tutor setSubject(String subject) {<br/>  return this.subj;<br/>  }

My question is how do I write the setter in tutor Class in order for the code to work in the Servlet. I am using Java EE7 and jdbc, 1.8 Java SE and Tomcat.

Answers

  • eudriscabrera-JavaNet
    eudriscabrera-JavaNet Member Posts: 214 Bronze Badge
    edited Dec 12, 2018 2:26PM

    Try with the following code:

    public class tutor{

            private String tutorName;

            tutor subj;

           

            public String getTutorName() {

                return tutorName;

            }

            public void setTutorName(String tutorName) {

                this.tutorName = tutorName;

            }

            public tutor getSubj() {

                return subj;

            }

            public void setSubj(tutor subj) {

                this.subj = subj;

            }           

    }

    Don't return the value from the set method, create a get method to return the value;