Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

JSP Scripting Elements

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67497
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

  •  
    Marshal
    Posts: 26377
    81
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 26377
    81
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    keep an eye out for scorpions and black widows. But the tiny ads are safe.
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic