0

I have a .jsp file that should tell the user the result of their exam.

This is the WebController.java class that works out what a person gave as their answer and then adds either 0 or 1 depending on if their answer is wrong or right.

@RequestMapping(value = "/results", method = RequestMethod.POST)
    public String results(Model model, HttpSession session, HttpServletRequest request,
            HttpServletResponse response){

            String radio = request.getParameter("radios1");

            request.setAttribute("total", total);
            model.addAttribute("total", total);

            if (radio.equals("int")){
                total = total + 0;
            }
            else if (radio.equals("Enum")){
                total = total + 0;
            }
            else if (radio.equals("integer")){
                total = total + 0;
            }
            else if (radio.equals("Integer")){
                total = total + 1;
            }

            return "results";
}

I would like to print the "total" variable onto the jsp page so it displays in the browser. So the idea is the user will instantly be able to see what they got in their test.

Also can this work the same way in reverse? e.g. in the code above can the java class get the parameter "radios1" which is located inside a form in a jsp class?

<p>Good day <%= session.getAttribute("uname") %> </p>
<p>For question 1 you chose <%= session.getAttribute("q1") %> </p>
<p>For question 2 you chose <%= session.getAttribute("q2") %> </p>
<p>For question 3 you chose <%= session.getAttribute("q3") %> </p>
<p>For question 4 you chose <%= session.getAttribute("q4") %> </p>

<section>

    <p>Total score: ${requestScope.total}</p>
    <p>Total score: ${total}</p>
</section>

3 Answers3

2

add the total to the model and display on the html as

model.addAttribute("total",total);

and display on html as

${total}

Neelam Mehta
  • 186
  • 5
  • i have done as said but no luck yet. my code is edited in the question above, can you see any glaring errors?? –  Nov 11 '14 at 11:10
  • what is output or you are getting an exception – Neelam Mehta Nov 11 '14 at 11:12
  • netbeans is being difficult at the moment and not giving me anything in the console. I can only go with what the browser is telling me. (which obviously isn't anything other than working or not working). –  Nov 11 '14 at 11:18
1

Set the total in request, response or session as an Attribute and later get it in JSP using JSTL or EL (Expression Language)

For example

Servlet

request.setAttribute("total",total);

JSP:

${requestScope.total}

There are several implicit objects in JSP EL. See Expression Language under the "Implicit Objects" heading.

Read more...

How to access a request attribute set by a servlet in JSP?

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • i have done as said but no luck yet. my code is edited in the question above, can you see any glaring errors?? –  Nov 11 '14 at 11:09
0

As you are using Spring MVC, you should simply put in Model what you want to pass to the view :

model.addAttribute("total", total);
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • The upper case `a` was not at the right place, sorry. It not really easy from a smartphone :-( – Serge Ballesta Nov 11 '14 at 11:15
  • no worries, I have seen that enough times to know it should be lowercase! edited my question code and the browser is still not playing ball –  Nov 11 '14 at 11:19
  • does it matter that my variable name at the top of the java class static int total = 0;, is the same as the name for this variable model.addAttribute("total", total);? could it be getting confused..? –  Nov 11 '14 at 11:44
  • @RedBaron Looks strange to use a static var here... Could be better in session. And did you control what you really get in `radio` ? – Serge Ballesta Nov 11 '14 at 12:05
  • i am using a static var here due to the advice here. http://stackoverflow.com/questions/17126541/adding-a-counter-to-a-java-program-with-different-methods and radio is just from this string. String radio = request.getParameter("radios1"); which is referring to these in the .jsp files Int
    sorry for lots of code in comment. do you understand what I am doing?
    –  Nov 11 '14 at 12:10
  • @RedBaron Referenced example was stand alone app. The static variable would be shared among **all** sessions and you do not want that. And log or print the value of `radio` : I think it is not what you expect... – Serge Ballesta Nov 11 '14 at 13:05
  • netbeans is too difficult to log in. or at least the one on my computer is. just will not display anything in the output console whether that is logs or system.outs. and eclipse is broken. do you have any idea what radios might be returning or what i think it's returning and what it actually is? –  Nov 11 '14 at 15:07
  • right, worked out an easier way to do. what exactly do i want to print out? –  Nov 11 '14 at 15:33
  • @RedBaron : It is easy to have misspelled something (I remember my first answer). You should use the debugger (Netbeans is quite easy for that) with a breakpoint on `String radio = request.getParameter("radios1");` to see exactly what happens. – Serge Ballesta Nov 11 '14 at 15:48
  • ah right i see. i get "no current context (stack frame)" in the value column. which, after a quick google, says it cannot evaluate variable because it is not in memory. what is the solution for this? –  Nov 11 '14 at 15:55
  • Normally you should simply go single step (should be F8 if I correctly remember). – Serge Ballesta Nov 11 '14 at 15:59
  • think this is too complicated for me. i just need a solution where you can pass a parameter from a jsp to a java class –  Nov 11 '14 at 16:03
  • @RedBaron It should work. I suspect you get `Int` and compares to `int` or something like that. At least put a `System.out.println("radio : " + radio);` after the `getParameter` to be sure of the value. – Serge Ballesta Nov 11 '14 at 16:07
  • but it does not print out anything that is in the WebController.java class. for some reason system.out.println is only working when put inside scriplets in jsps. which i dont really get –  Nov 11 '14 at 16:12
  • It will not show up in the client browser, but you should find it in the output window in Netbeans. – Serge Ballesta Nov 11 '14 at 16:35
  • no that's what i mean. the system.outs in the jsp show up in the output window in netbeans but the ones in the java classes do not for some reason. or at least which output window? i currently have Java DB database process, Glassfish server 4.1, HelloSpring (run). the jsps system.outs appear in the glassfish window. –  Nov 11 '14 at 16:50
  • i cant for the life of me work out why it is not printing anything? even if i had got it wrong e.g. returning int instead of Int, it still should be printing the variable total should it not/ –  Nov 12 '14 at 16:09
  • If you want to do serious programming, you must learn how you IDE works and where messages go. It is **your** system, and I cannot help you in this part. Also, you should try to use the debug logs from spring : it gives you details on how it processes an URL and what controller is called. – Serge Ballesta Nov 12 '14 at 16:14
  • i dont think that is the case. I have done all you have said and just asked a simple question as to why sysos wont show in the java files but will in the jsps –  Nov 12 '14 at 17:05
  • I do not know ... I use netbeans too, and if I write to System.out, I can see that in the logs. But I always use slf4j for that purpose : it is dedicated for logging. – Serge Ballesta Nov 12 '14 at 17:10
  • ok, it's really odd though. maybe the java class is not getting called in sequence. becuz the logs show up when i put sysos in scriplets within the jsp. that all works fine. just when i wack a syso at the bottom of my controller class it does not call it –  Nov 12 '14 at 17:17