tags:

views:

94

answers:

3

Hi guys!

I have to write a website with JSP and Servlets which displays a questionnaire and gets the result of the user.

I've already written the question object, a questionnaire (random list of questions) and the questions library.

Now my Servlet generates an instance of the questionnaire and displays the questions. My problem is I don't know how to get the results. I can get the answers but AFAIK every time I load a Servlet it would generate a new questionnaire and the answers would be useless. So how can I pass the answers to the correct questionnaire. Or how can I pass the questionnaire to my new Servlet?

Thanks a lot.

Edit: Here is the code for the questionnaire:

public abstract Question getNextQuestion();

public abstract void setResult(ArrayList<Boolean> answers);

public abstract int[] getResult();

Basically I create a questionnaire, and get via getNextQuestion() the next question until there are no more. then the user will hit the submit button and i'll have to read the answers and pass it to the questionnaire (setResult() for the actual question). When there is no more question I'll use retResult() for the result.

Hope this helps. I can't give u any JSP code since I have no JSP written yet

+1  A: 

Any data which must survive several requests should go into the session (see request.getSession()).

Aaron Digulla
+1  A: 

Since you are creating a questionnaire per user when he enters the site you can have the servlet generate tehe questionnaire and record it into the session under a name like "lastGeneratedQuestionnaire".

When the answers are submitted that servlet would retrieve the questionnaire from the session by that name. If one is not there I would redirect the user back to the page that generates a new questionnaire.

Also, the page that generates a new questionnaire should override the object in the session each time it generates a random questionnaire.

The short of it is...use the HttpSession to store the object between requests.

Gennadiy
A: 

Normally you will have a form object (on the JSP page) that will be associated with your questionaire.

<html:form action="/servlets/MyQuestionaireServlet method="post">
    <h3>Question 1: What is your name</h3>
    <html:text name="question1" />
    <input type="submit" name="submitBtn" value="Submit"/>
</form>

What you need to do in order to send the data to the servlet so that it can be stored/recorded is you need to provide a submit button inside of the form. This will send all of the data in the form to the servlet.

Good point, without struts you can still perform a standard post method on the form and retrieve the values in the servlet listed under action. You do not need struts to perform an action on a form.

amischiefr
The OP didn't said he was using Struts.
BalusC
no i'm not using stuts. it's actually the first time i'm using jsp ;)
Dimitri