aspose file tools*
The moose likes Servlets and the fly likes How to persist data without HttpSession in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to persist data without HttpSession in Servlet" Watch "How to persist data without HttpSession in Servlet" New topic
Author

How to persist data without HttpSession in Servlet

Kat Rollo
Ranch Hand

Joined: Jun 30, 2014
Posts: 46

Hi guys,

I have a basic HTML calculator that asks the user to input 2 integers and select from a dropdown their desired math operation:


The servlet will process the calculation and counts how many times a certain operation has been used:

The counter for Multiplication has incremented accordingly.

HTML:


Servlet:


Question:
However, when I press "Go Back" and process a new computation, all counters reset to 0. Is there a way without using HttpSession to persist the counter values?
(It has not been taught to us, so we are not allowed to use it yet.)

For example, going back and having below input...
First Operand: 4
Operator: +
Second Operand: 5

...will result to...
Addition: 1
Subtraction: 0
Multiplication: 1 (counter from previous computation maintained)
Division: 0

Your kind assistance is much appreciated.


Help first. Criticize second.
Paul Ngom
Ranch Hand

Joined: May 08, 2014
Posts: 312
    
    1

Is there a way without using HttpSession to persist the counter values?

You can add 4 hidden type elements inside the form of the servlet(to store addCtr,subCtr,mulCtr,divCtr) that you pass to index page which in turn will return them to your servlet. But then you will have to turn your index.html to index.jsp.


I am always surprised at the added knowledge i can get from others when i give my opinion on a topic.
Kat Rollo
Ranch Hand

Joined: Jun 30, 2014
Posts: 46

I changed the servlet as follows:


I also renamed index.html to index.jsp via the Project Explorer in Eclipse.
It works the same, but it is still not keeping the counter values.

I suspect I am not using the hidden type correctly, would someone kindly post a code snippet/template I can follow on how it should be done?
Paul Ngom
Ranch Hand

Joined: May 08, 2014
Posts: 312
    
    1
You aren't saving the values i mentioned earlier.
Rather than:

it should be:

Correct the other values. And you should also have hidden elements in your index.jsp to store these values. You can just reproduce the same hidden elements.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Look at the HTML that you send to the browser. The browser is full of useful tools that you need to learn how to use.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kat Rollo
Ranch Hand

Joined: Jun 30, 2014
Posts: 46

So far, this is what I have:

JSP:


Servlet:


The data is still not being kept.
Apologies but this is basically my first exercise with post and get.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Have you looked at the HTML yet? What is there for the values? Are they correct?
Kat Rollo
Ranch Hand

Joined: Jun 30, 2014
Posts: 46

I'm trying, but please understand it's hard to "learn how to use" without really seeing how to use it first.
Examples are the best teachers for better learning.
Paul Ngom
Ranch Hand

Joined: May 08, 2014
Posts: 312
    
    1
I wonder if your jsp does compile. Here replace this

with

or if you want to use scriptlets:

So i hope you can correct the other input elements in index.jsp. When that is done, you need to review your code in the servlet to fetch these values.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

You can use the browser (have no idea which one you are using -- recommend Chrome as its debugging tools are extensive) to "view source" which will show you the HTML that your servlet is sending to the browser. If the values in the hidden inputs aren't set correctly, then you know that you need to work on getting them right. If they are set correctly, then you know that the problem comes later than that.

Learning to debug is an essential part of coding. It's not something you put off to learn "later".
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Paul Ngom wrote:or if you want to use scriptlets:

You don't want to use scriptlets.

2001 called and wants its technology back.
Paul Ngom
Ranch Hand

Joined: May 08, 2014
Posts: 312
    
    1

You don't want to use scriptlets.

2001 called and wants its technology back.

Lol! Looks like you hate to see people still using scriptlets.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Are you new here?

I've been ragging on people not to use obsolete, out-dated technology in JSPs for over a decade. There is no valid reason for JSPs written after 2001 to contain scriptlets.
Paul Ngom
Ranch Hand

Joined: May 08, 2014
Posts: 312
    
    1

Are you new here?

I joined the Ranch on May 2014 and i have been reading your posts. Thanks a lot.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Paul Ngom wrote:I joined the Ranch on May 2014

Yes, I know. That was a joke.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1038
    
  13

Bear Bibeault wrote:You don't want to use scriptlets.

2001 called and wants its technology back.




"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Varun Selva
Ranch Hand

Joined: Jun 17, 2012
Posts: 44

Hi

I think, In this case,
You should use instance variable rather than using local.
and you can reinitialize those variable when you do the new computation by calling reinit().


But this will help only for single client application.

If you want to do this for concurrent users, you should maintain any unique id.
Because as you told, can't use session.

then you can use the Hashmap<String,int[]> instead of those 4 variables.
key is userid
value array is your count variables.




I think It should give the solution for your prop..
any clarification. post me..
Enjoy with coding



Varun Selva
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Varun Selva wrote:
I think, In this case,
You should use instance variable rather than using local.

NO!

Never, ever, ever, use instance variables in a servlet. Never! It will make the servlet not thread-safe!







(Constants are ok.)





Varun Selva
Ranch Hand

Joined: Jun 17, 2012
Posts: 44

Bear Bibeault wrote:
Varun Selva wrote:
I think, In this case,
You should use instance variable rather than using local.

NO!

Never, ever, ever, use instance variables in a servlet. Never! It will make the servlet not thread-safe!


(Constants are ok.)




Read up to bottom.
That's why, I preferred to use Hashmap with userid key.
Kat Rollo
Ranch Hand

Joined: Jun 30, 2014
Posts: 46

@Varun Selva:
I scratched everything and restarted with my original code (first post). Your solution worked! I did not have the reinit() method though, I just moved the counter variables from doPost() as instance variables.

@Bear Bibeault:
While it is clear this is not good practice, may you kindly explain why it worked? I don't mean to be sarcastic, just plain curious.

(I'm pretty sure our professor will teach us security, browser debugging, and the proper way to do things in our future Java EE classes, just that, we are basically at the HelloWorld phase of servlets as of the moment.)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Building a Rube Goldberg faux pseudo-session isn't the answer. Especially for a novice, recommending such Byzantine approaches is not a good idea.

Carrying the data via conventional means makes much more sense -- especially for those just learning.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

It worked because you are building a faux session using a user id as a key. If I were your instructor and you passed this in you would fail. Just because you can get something to work doesn't make it good.
Kat Rollo
Ranch Hand

Joined: Jun 30, 2014
Posts: 46

Just because you can get something to work doesn't make it good.

Relax, Bear. I know that very well.

As I have mentioned numerous times, he has not taught it to us yet, but we will get there.
Just so you know, we sometimes get deductions for using techniques outside the lesson.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

That is one reason I would fail this assignment. You should be using conventional techniques, such as the hidden input approach, that you would end up using later in real applications. This map technique is not something that would ever be done in real code where an actual session can be used.
Kat Rollo
Ranch Hand

Joined: Jun 30, 2014
Posts: 46

The hidden input approach would involve JSP, and things I don't understand as of yet. True, it is the proper way, but I can't defend something I don't understand when I get questioned for it. I'd rather get a lower score for a known mistake rather than get a good score from something I didn't fully grasp.

I know folks are keen on the proper way to do things, I get that and again, we will get to the conventions as we go along our lessons. We all started at some point, just that, you can't expect everything to be taught during the first exercise.

I understand your intentions and it's appreciated, but please also understand such "student" situations. Thanks.
Varun Selva
Ranch Hand

Joined: Jun 17, 2012
Posts: 44

@Bear Bibeault
I also knew that this is not a gd solution. But reason is "Can not use Session"

This map technique is not something that would ever be done in real code where an actual session can be used.


So Give him a Very good solution for this specific problem Or improve my code. rather than telling leeks in my code.
Kat Rollo
Ranch Hand

Joined: Jun 30, 2014
Posts: 46

I am marking this Resolved before it becomes a possible source for e-drama.

Varun Selva, thank you for your help. I did ask for a way to persist without HttpSession and you did it. ;)

Bear Bibeault, thank you for your good intentions. I will not use instance variables in Servlets in real life, so you can sleep well on that.

Thank you for everyone's time, I hope to learn more in this community as I go along.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to persist data without HttpSession in Servlet