Announcement Announcement Module
Collapse
No announcement yet.
How to pass objects between methods in Servlet Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to pass objects between methods in Servlet

    Hi

    Here is my servlet myApp. For incoming GET request, myAppForm will be displayed for user to enter student name. Once student name is entered, POST method is called ,ProcessMyAppRequest. In ProcessMyAppRequest, student name is extracted from object StudentObject (POJO). I also have instance AppEventlListener listen for some kind of event. If the event is happening, I will return student's age in myAppFormSuccess. MyAppFormSuccess needs to access StudentObject.age for display result.

    My questions are:

    1. Am I right that if some event happens from AppEventListener in doPost, assign age to student and return to doPost where MyAppFormSuccess will be able to access StudentObject.age?

    2. In the update function of AppEventListener below, how do I assign age to same StudentObject from doPost. Is SesssionAttributes ("StudentObject") a solution?

    Thanks a lot for your help

    @Controller
    @RequestMapping("/myApp")
    public class MyAppController {

    @RequestMapping(method = RequestMethod.GET)
    public String showMyAppRequest(Map<String, StudentObject> model) {
    StudentObject studentObject = new StudentObject();
    model.put("studentObject", studentObject);

    return "myAppForm";
    }


    // Process the user input from myAppForm .
    @RequestMapping(method = RequestMethod.POST)
    public String processMyAppRequest(@Valid StudentObject studentObject,
    BindingResult result, Map<String, StudentObject> model) {

    if (result.hasErrors()) {
    return "myAppForm";
    }

    model.put("studentObject", studentObject);

    new AppEventListener("Some Event");

    return "myAppFormSuccess";
    }

    @SessionAttributes("studentObject")
    private class AppEventListener extends GenericEventListener {
    public AppEventListener (String msg) {
    super (msg);
    MessageObserver obMessage = MessageObserver();

    synchronized(this) {
    obMessage.addListener(msg);
    }
    }

    public void update (final Observable listener, final Object msg) {
    if (msg == "Some Event") //Event is happening
    /* QUESTION

    How do I access studentObject and assign StudentObject.age so when I return to doPost, myAppFormSuccess will display this age.

    }
    }

    ------------ StudentObject ---------

    public class StudentObject {

    String student = null;
    int age;

    public setStudentName (String student){
    student = student;
    }

    public getStudentName(){
    return student;
    }

    public setStudentAge (int age){
    age = age;
    }

    public int getStudentAge(){
    return age;
    }
    }
Working...
X