Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP Scripting Elements

 
Sudhir Srinivasan
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

I'm new to JSP and facing an issue with the following program.

Program files:
contains 3 jsp files -

a) index - links to the form page
b) Form page - takes input, from the user, into the text box and on hitting the submit button navigates to the
c) ReadForm page - which displays the values input on the form page (and to also display the calculated result of the input values,
which does not happen)


Issue faced:

The (String) values input - principal, interestrate, tenure - on the Form page is converted to int on the ReadForm page to facilitate calculation & display of the simple interest amount. While the expression tags relating to the instance variables



display the (converted to int) values, they do not do so for the method



invoked (which should calculate & return the simple interest amount). Instead, just displays the principal value again!.....full code shown below under ReadForm.jsp

Could any of the forum expert(s) give a solution as i'm unable to resolve this.

thanks,
Sudhir

index.jsp



Form.jsp



ReadForm.jsp
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhir Srinivasan wrote:I'm new to JSP and facing an issue with the following program.

If you are new to JSP, you are learning bad habits right off the bat. In modern JSP, there should be no Java code or scriptlets in the JSP. That is a bad practice that has been discredited for almost 10 years now.

You should be focusing on learning how to write JSPs using the JSTL and EL.

Also, I ususally recommend that JSP novices read the following articles:
  • The Secret Life of JSPs
  • The Front Man

  •  
    Paul Clapham
    Sheriff
    Pie
    Posts: 20771
    30
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And your problem is a basic Java problem. Nothing to do with JSP at all.


    If your Interestrate parameter is a percentage number, say 5 for example, then dividing it by 100 yields zero. I'm sure you can then see why the result of that calculation is Principal.

    Ironically if you had written this same calculation in the EL, you would not have had that problem.
     
    Paul Clapham
    Sheriff
    Pie
    Posts: 20771
    30
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your JSP converted to JSP with EL:


    You don't need those 40 lines of scriptlet code.
     
    Sudhir Srinivasan
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:
    If you are new to JSP, you are learning bad habits right off the bat. In modern JSP, there should be no Java code or scriptlets in the JSP. That is a bad practice that has been discredited for almost 10 years now.

    You should be focusing on learning how to write JSPs using the JSTL and EL.


    Thank you for your reply and suggestions. It certainly helps in learning the right way!

    Bear Bibeault wrote:
    In modern JSP, there should be no Java code or scriptlets in the JSP. That is a bad practice that has been discredited for almost 10 years now.


    Could you please explain why scripting is discouraged on JSP pages (though i wonder why my course-ware has a small section on the same ).


    Bear Bibeault wrote:
    Also, I ususally recommend that JSP novices read the following articles:
  • The Secret Life of JSPs
  • The Front Man



  • Thank you for the links....will certainly read the same.

    regards,
    Sudhir
     
    Sudhir Srinivasan
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul Clapham wrote:And your problem is a basic Java problem. Nothing to do with JSP at all.


    If your Interestrate parameter is a percentage number, say 5 for example, then dividing it by 100 yields zero. I'm sure you can then see why the result of that calculation is Principal.


    Thank you for your response.

    :eek: I've declared Interestrate as int instead of double which naturally returns only the Principal value.

    Paul Clapham wrote:
    Ironically if you had written this same calculation in the EL, you would not have had that problem.


    Going forward, i shall certainly incorporate EL in JSP as suggested by Bear and you.
     
    Sudhir Srinivasan
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul Clapham wrote:Your JSP converted to JSP with EL:



    Thank you for the code snippet.

    As my course hasn't touched on EL yet, could you answer the following:

    a) Is param a predefined object reference or user-defined
    b) When sending the request, are the variables Principal, Interestrate, Tenure retrieved as int and double OR they are read as string and then converted to respective types.
    c) Given that java code within JSP is a big NO, the declaration of the variables (& their types) does not take place. How does the web container know that Principal is to be converted to int, Interestrate to double and so on......

    regards,
    Sudhir
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic