• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Save parameters from JSP into ArrayList and print them all

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

A a simple task to do but i have no idea how to do it :(



I have a index.jsp file (actually it is a form) which has two components described
by the code below.

<form action="index.jsp" method="get">
<input placeholder="Enter name" name="name" type="text"/>
<input type="submit" value="Add Employee"/>
</form>

<form action="index.jsp" method="get">
<input name="kill" type="hidden" value="1"/>
<input type="submit" value="Kill 'em all!"/>
</form>

I have to forms in my JSP page

When a type a name-- placeholder="Enter name" this name appears right the way here in that JSP
when i type the next name , the second one appears here again along with the first one.

So when I types names i need to store them somewhere into ArrayList , and after which new name i wont them all to be printed
for Example:
I typed Jack
a have Jack
I typed John
a have Jack, John
I typed Tina
a have Jak,John,Tina
and so on

If push - <input type="submit" value="Kill 'em all!"/>

The list should be cleared a my JSP page after i push that button shows no names

this is an example link how should it works: http://rodiongork.atwebpages.com/employees/index.php

Please help me to solve this simple task.
Thanks a lot!
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know how to create a servlet? Try creating a servlet that does nothing but receive the parameters from your form and print them out to the log

It will look something like this:


We're not even trying to display the names on your page yet (that's going to require JavaScript), we just want to make sure you are sending the form parameters to the servlet correctly.

[edit] I see that the example you linked to does not use JavaScript, it's reloading the page each time. So we'll skip that and keep this simple.
btw, that demo is in php. What are you doing this in Java? Is this a homework assignment?
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is kind of tutorial for me and my goal to do this task using JSP/Servlet technology. The link example was done by somebody using php but is not my case ( i need JSP/Servlet way to do the same what link example does) I know how to create JSP/Servlet in a dynamic web Application using Eclipse IDE
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this my let's say empty jsp file for this task

Also a have already a servlet to i am trying to address by this --- form action="/Forms/Controller"
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this my servlet :



 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see that you tried to use code tags but didn't get them quite right, so I fixed it. Just highlight the code and click the "Code" button above.
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vladimir Vancea wrote:this my servlet :



Danger, Will Robinson! Never use class level variables in a servlet. It breaks thread-safety. Unless they are final and static (like a logger) declare all variables as local to the get/post method that you are using. And the constructor is unneeded. Delete that.

So fix that, and then explain to us exactly what happens when you run this.
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin can not see any Code button above Would you tell me more precisely . Thanks
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The image is a little small, but when you are composing a new message, you'll see that toolbar. See the Code button? That's the one I was referring to.
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Kevin. Today is my first day here on this site. And to subject: Can not see any changes to the code i provided here . To check what ? Anyway i replaced my code from eclipse project with the code i posted here in a hope that there is difference (with your changes) but still the same result(nothing new)
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me say Welcome to the Ranch!

I didn't change your code, I just added the code tags so it displays correctly.

So let's backup. Does your code compile? What happens when you run it?
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well!

Let me described What I achived at point treying to do this task.

1.I have a free host(openShift) and i know how to deploy a war file there!
2. I completyly new to JSP/Servlet but i already followed couple of tutorial and and at least a have an idea what is servlet/jsp file. I did several example in eclipse IDE using tomkat( at leat hello world and other stuff )
3. I am trying to solve a task on site symilar to projectEuler.com or somthing like this. Acctuale the task itself is located here(http://www.codeabbey.com/index/task_view/employees-web-app) Of course this task could be solved in many ways, but my goal is to solve it using JSP/Servlet, thus i am doing all the task on this site using Java language.
4. My index.jsp file works a litlle (just opens and allows me enter a name and when i enter that name it display on the page, but when enter next one it just replaced the previous one with last i entered
5. Kill button just exists but do nothing, because i did not solve first task yet( to display all the names i have entered an later kill" them " by push the kill button
6. Pretty much that it all at this stage( not many )
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't worry about the kill button at this point. One thing at a time.

Okay then, let's talk about this code:


First, we don't need the enumeration of parameter names. We know the names; they will be the "name" attribute of the input elements on the jsp page, so in your case, it will just me a single parameter called "name". We can get that like so:

Let's do one thing at a time.

So for now, reduce your doGet method to this: (you can comment out your existing code for now.

That should display the name and message in your output log, catalina.log. Are you using an IDE? If so, there is probably a window where log messages are displayed.

I'm going to lunch. Be back later. Get this much working.

 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two options here :
String newName = request.getParameter("name"); when I push Add button
and could be String newName = request.getParameter("kill"); when I push kill button

So i need somehow to check what i have (name or kill) and after I found what is this ( i do ArrayList - add and i have name and when i have kill i need clear that ArrayList)
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but do one thing at a time. Get the name listing working, then we'll add a check for the kill button later.

Code small pieces at a time, save, and compile. I use NetBeans which compiles on every save. I hit ctrl-shift-S a lot.
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i completely agree with this approach, thanks for good advise.
Now my doGet Method looks like this:
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But unfortunately no result
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This i my new doGet Method , but unfortunately nothing new



 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you writing to the response output in the servlet? You are forwarding to the a JSP page; that's what will do any writing.

If you want to log debug statements, don't write to the response, either write to System.out, or better yet, use a logger like Log4J.
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear Bibeaut could you write 4-5 lines of code for better understanding. Thanks
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By writing to the response and forwarding to a JSP, you are mixing up the response output.

In your servlet, for logging to the console, use something like:rather than trying to use the response.

It'd better to learn how to use a real logger, but System.out will do for now.
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




doGet method looks now like this but does not change anything.


I would like to ask you one more time regarding the connection between the JSP file and Servlet
1. So, I have a JSP file from which i wont take query parameter "name" and store somewhere in ArrayList
2. In this case ArrayList is located in Servlet, so every time when i entered a name in my form -> JSP file i want to store it in the ArrayList and immediately print or show back that ArrayList in the same JSP file
3. As I understand,logically i should store the name an retrieve all the content of ArrayList back into JSP file and show it
4. I think the main problem that i miss functionality to retrieve all content of ArrayList into JSP file
5. Above there is my JSP file can you take look at this and check if this JSP fie get back ArrayList every time when i entered a new name.

Thanks a lot !
 
Marshal
Posts: 28175
95
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

Vladimir Vancea wrote:I would like to ask you one more time regarding the connection between the JSP file and Servlet
1. So, I have a JSP file from which i wont take query parameter "name" and store somewhere in ArrayList



No. There's some HTML in a browser which sends a query with parameters. That HTML may or may not have been generated by JSP but at this point that's irrelevant.

2. In this case ArrayList is located in Servlet, so every time when i entered a name in my form -> JSP file i want to store it in the ArrayList and immediately print or show back that ArrayList in the same JSP file



You have a servlet, and you have decided to use an ArrayList to store the name. I'm not sure why you are doing that -- parameters are more like a Map than a List, and if you're only looking for a name there's only going to be one thing so any kind of collection is sort of pointless. Also, it looks like you made the ArrayList be a class-level variable, which is a bad thing to do in a servlet. It should have been a local variable in that method. (Except that you don't need it at all.)

And if you plan to use a JSP to output the HTML generated by this request (a perfectly reasonable thing to do), there's no reason that you have to use the same JSP which generated the HTML which started off this cycle. You could write a new JSP to display the name sent from the original request.

3. As I understand,logically i should store the name an retrieve all the content of ArrayList back into JSP file and show it



No. You don't store things in a JSP file. Your servlet forwards to a JSP, and if you want to send data along with that then the usual thing to do is to put that data into request scope before forwarding.

4. I think the main problem that i miss functionality to retrieve all content of ArrayList into JSP file



Yes. You would commonly put the data into request scope to pass it to the JSP. However in your example you are trying to deal with the request parameters. These are already accessible to the JSP so there's no need for any of that servlet code you have there.
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I already do not know what to write and which way to start in order to find a way to solve this task. I think the best way again to reference to a link : http://rodiongork.atwebpages.com/employees/index.php
1. By this link you can see that task is done in php. My goal is to learn Java/JSP/Servlet that why decided to do this task in JSP/Servlet. I know task i very easy but taking into account that i am completely new to the filed this task still is big challenge for me.
2. So the main point is , as to try this link You entered a name Ex. John and press button " Add Employee" you got immediately John plus all the names which entered before John - from here i got idea to store these names somewhere ( I choose ArrayList<String> whatever, probably there is other way). So adding names the list grow up and every time you see this list after pushing the button " Add Employee"( as can see accessing the same link)
3. And finally pushing "Kill them all" button the List is just cleared.
4. Having these conditions i start to think to store the names i entered and retrieve every time the list back.
5. Probably i started in wrong direction.
6. Any help would be really appreciated.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion, starting with a PHP example is a mistake. PHP is not like Servlets and JSP, and it's likely hurting more than helping.

Also, be sure you really understand how JSP works. Read this article to be sure.

I also have the same question as Paul: why the insistence on storing a single value in a List? A List is for, well, a list of values; not a single value.
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This link is to show how it must work. Just forget PHP :) if it is not List than just give me a good advise what must be instead. Actually what i am trying to do is to make functional page ( you already knows what it does) but using JSP/Servlet technology. Thanks a lot
 
Paul Clapham
Marshal
Posts: 28175
95
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
Could you tell us a bit about your background? What I mean is, what resources about servlets and JSP have you studied so far?
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul i have basics knowledges about JSP/Servlet. Task is not so complicated , does not require deep level knowledges. If you have a idea how to solve it , please :)
 
Paul Clapham
Marshal
Posts: 28175
95
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
So you have not studied any basic guides to servlets or JSP, I interpret that to mean. You're right that what you are trying to do doesn't require deep knowledge. It only requires basic knowledge -- and you don't even have that. For example you say

i got idea to store these names somewhere



Yes. That's exactly correct. Putting them in a list is correct too, but then you have no idea where to put that list so that it's preserved from one request to the next. And yet that's a very basic concept in servlets. So in my opinion you're wasting your time trying to learn servlets this way. In my opinion you should get yourself a proper textbook on servlets and work your way through that.
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul totally agree with you. In this case just give me a link or reference to a book in which i can find: quote "but then you have no idea where to put that list so that it's preserved from one request to the next. And yet that's a very basic concept in servlets"
. I wasted already a lot of energy to find this topic but did not get anything. If this basic concept is so obvious like to show where is New York city on the map, just show me:) Thanks in advance !
 
Paul Clapham
Marshal
Posts: 28175
95
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
I didn't mean to suggest that preserving information was "obvious". It isn't obvious, it's just something that you learn near the beginning. Here's a link to the Oracle tutorial:

http://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, the keyword you are looking for here is "session attribute". You need to know the fundamentals of attributes and scopes in order to implement this.
Here is one page I found with a quick google that looks to give a reasonable summary of them: http://www.java-samples.com/showtutorial.php?tutorialid=1009
There are possibly better ones around.

As I understand your requirements:

You have a List of employees that you keep track of.
You have a JSP page that should display all of your current employees.
You have two actions a user can do on that page:
- add new employee
- remove all employees.
These actions are going to be implemented by your servlet.


Just so that I don't hand you the answer here:
Your List of employees should be an attribute - what scope do you think it should be in? (based on the link I posted earlier)?







 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stefan you understand corect all requirements. Thaks you guys for trying to help me. Let me first follow your link and after i'll try to answer to your last questions. I hope that i get done this task in the end:)
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stefan in my case i need Application Scope. That is right ?
 
Paul Clapham
Marshal
Posts: 28175
95
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
That depends on how long you want the list of names to stay available, and on whether you want more than one client to have access to the list. That wasn't clear from your original posts, so you would have to clarify that in your own mind.
 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was actually thinking session scope, because I very rarely use application scope.
Paul pointed out quite rightly that we don't actually know if the employee list should be just for one user/browser (and thus be in session scope) or for every user/browser getting access to the same list - and editing it - in which case it would be application.

It is a minimum of session scope, because you want the List to stay around over multiple requests.
Application scope will work as well and might actually be more applicable in this example.

Regardless, the next step is to move this List from being just a variable in your Servlet to be an attribute in your web application so your JSP can reference it.

So in the servlet to set the attribute:



You can retrieve it again using the getAttribute method of the appropriate object.
You should now be able to access it in the JSP as an EL expression using: ${employeeList}
That probably won't show you much if you print it out. ${employeeList[0]} will give you the first element in the list which might be a bit more useful for feedback.

My recommendation on the step after that would be to learn about JSTL and how you can loop over all the elements in the employee list from your JSP.

 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i am gonna use session scope. Let me put everything into JSP and Servlet and i will show my result here Thanks again guys i never solve this task without you. Actually my goal is to not real solve task but to learn JSP/Servlet a litle bit. I very beginer in Java (around 6 months) I start to practice a little bit of coding on this site http://www.codeabbey.com/. From 213 tasks i did 202 and this one number_199 is a last one from web-related (around 8 task in total from 213). Still working on this task for one week. I a have feeling that task #199 is very simple but my (JAva of 6 months) does not allow me to solve even this one. But i am progressing every day. After such good advises from you i am close to solve this. Now i have a clear direction to move forward.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vladimir Vancea wrote:Well i am gonna use session scope.


Why? What makes that the best choice?
 
Vladimir Vancea
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is Stefan Evans advise
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic