This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner JSP Counter  RSS feed

 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extreme beginner here. Trying a simple form with a text field and a submit button; the text field should start with zero and increment by 1 every time a user clicks it. I know this is basic stuff but I'm trying to learn this on my own and can't find the answers online. Any help would be greatly appreciated as it's the only way I'm going to learn.
My HTML


My JSP
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Betty,
I see two reasons why your code doesn't work as is:
1) You increment i, but don't appear to use it anywhere so changes are lost.
2) The HTML page has 0 hard coded so will always display 0.

It's not good practice to put Java code in JSPs anymore (and it hasn't been in over 10 years.) You want to use a technique called MVC (model view controller.)

Let's try to get this working with some good habits.
1) Does the book you are using talk about how to create a servlet? If not, which book is it? If so, can you create a servlet that gets the "count" attribute from the session? If there is a value already, it adds one and puts it back. If not, it creates one with the value 0.
2) Can you have the servlet redirect to counter.jsp
3) Can counter.jsp display your HTML format with the value from the session attribute as the count.

Please reply to this thread with which step you get stuck on.
 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I'll work on it using your suggestions.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are a novice to JSP, I suggest reading this article to make sure you understand what JSP is all about.

This article is a more advanced overview about structuring web apps, but you might want to read it over to see what sticks.

And as Jeanne points out, Java code in a JSP is an obsolete and outdated no-no that should no longer be used. The terms you want to key off of are the EL (Expression Language) and JSTL (JSP Standard Tag Library). Make sure that whatever material you are using covers those concepts.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!