Solved

Using cookies in JSP

Posted on 2008-06-23
17
2,520 Views
Last Modified: 2013-12-15
I've got a need to store user identity in a web application so that the user can "stay logged in" and not have to do so on each access to a site, and even if they don't elect to do that, to remember who the user is when browsing round the site once they have logged in.  I'm guessing the way to do this is with cookies so I've been trying to play with them, but my IDE (Eclipse) is throwing up an error, saying it doesn't recognise a type called Cookie (cannot be resolved to a type, it actually says).

Now I've found out that Cookie is defined in javax.servlet.http.* but if I prepend with this, I get the same problem with javax.servlet.  Now I guess this means I'm missing something, but what I'm missing and where to find it I don't know.

Further I've got no experience with Cookies at all so any *simple* pointers would be appreciated, including advice on whether my approach as detailed above is the right one to adopt here.

Thanks
B
0
Comment
Question by:Belazir
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +3
17 Comments
 
LVL 92

Expert Comment

by:objects
ID: 21844999
sounds like you haven't added the j2ee/servlet jar into your projects buildpath.

0
 
LVL 3

Expert Comment

by:shaz_
ID: 21845058
like objects just said u need the servlet-api.jar file in your buildpath.... u can find it under your ->apache-tomcat-x.x.xx\common\lib folder
0
 

Author Comment

by:Belazir
ID: 21853874
Thanks both.  Please note the "beginner" tag though - can you tell me step by step what I need to do?  Shaz, I don't have an apache tomcat filepath, that I can find - I'm only developing this to prototype level on my own PC, and I'm using Eclipse.  I've done a file search for the jar but no joy...
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 27

Expert Comment

by:mrcoffee365
ID: 21856587
You'll need to get a servlet engine, probably Tomcat, which is why shaz_ mentioned the tomcat filepath.

Without a servlet engine, and the jars that come with it, you won't be able to compile JSP pages in Eclipse, and of course you won't be able to run them to see if they work.

There are many JSP tutorials on the Web.  A simple one is here:
http://www.jsptut.com/
On the first page of the tutorial, it gives you some links for servlet engines.  Choose Tomcat -- it's the most commonly used one, and you'll be able to get more support with your problems.

Others are:
Sun: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro.html

It sounds as if you haven't used Eclipse before, either.  You'll need to add the Tomcat jars to your buildpath, just as "objects" said.  This tutorial on setting up Tomcat with Eclipse might help you:
http://www.ibm.com/developerworks/library/os-eclipse-tomcat/index.html
0
 
LVL 3

Expert Comment

by:shaz_
ID: 21856655
exactly wat mrcoffee365 says..  u need to download an engine... i would suggest tomcat... simple nuff for beginners... and follow the tutorials the mrcoffee has mentioned...
0
 
LVL 3

Expert Comment

by:Sankozi
ID: 21860157
Maybe you should try starting JSP project in Netbeans instead of Eclipse. It is also free and you don't have to configure and install anything. Engine is already built-in and default configuration allows you to run your first servlets without any problems.
0
 

Author Comment

by:Belazir
ID: 21870465
I'm short of time and installing and learning new packages is something I'd like to avoid if possible.  Is there any way of storing session state outside of Cookies then?  I presume access to the file system is pretty restricted for obvious reasons.

Sankozi, where do I find Netbeans, if I have to go down that route?

mrcoffee, I'm confused by this: "you won't be able to compile JSP pages in Eclipse".  The pages I have work perfectly well and I'm accessing databases using my JSP, so it's clearly not as sweeping as that.  Am I being naive to think I just need to get hold of the right JARs for Eclipse to reference?
0
 
LVL 92

Expert Comment

by:objects
ID: 21870535
what are u using as a container at the moment?
you can grab the jar from there

0
 
LVL 3

Expert Comment

by:Sankozi
ID: 21872473
Netbeans download links: http://download.netbeans.org/netbeans/6.1/final/
Install "Web & Java EE" or "All" version. During install agree to install Glassfish.

Run Netbeans, File->New Project ->Web -> Web Application (don't select any frameworks). You will have project created with simple jsp.
0
 

Author Comment

by:Belazir
ID: 21876489
objects - Eclipse runs its own localhost.  That's why I'm confused as to why I need a different environment when this one works well enough, so I'm pretty sure I just need the jar and to work out where to put it.
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 21879266
Running a Web server is not the same as running a servlet engine, which is what you need to run JSP pages.

If you have been creating JSP pages and they run under a Web server you have, then it must have a servlet engine associated with it, or it is one itself.  Since you cannot run JSP pages under Eclipse, then you have not been using a servlet engine in Eclipse.  

That's why you have been getting answers, and more questions, from us about where your servlet engine is.  Eclipse does not come with a servlet engine installed, which is why you have not been able to "compile" (which is generally the same as "run" for JSP) your JSP files under Eclipse.

You can either
1) put Tomcat or some other servlet engine under Eclipse.  This can be difficult, and is not recommended for newbies.
2) put the servlet engine jars -- not the entire running Tomcat --  into Eclipse's classpath, assuming you just want to use it for editing your JSP pages, so that Eclipse will not give you error messages about classes it can't find.  To do this, find the servlet jars in your servlet engine.
3) follow Sankozi's suggestion of NetBeans, which is an IDE which does come with a servlet engine as part of it.

Again -- going through some JSP tutorials might be helpful.

If you would like more help, we'll need to know what servlet engine you're using currently for the JSP pages you have successfully created.

If you don't know what the servlet engine is, start with the Web server.  We can help you work from there to find out what the servlet engine is, then help you find the .jar files from it for your compiles.


0
 

Author Comment

by:Belazir
ID: 21891966
I swear I am not running a separate Web server!

When I run the JSP page within Eclipse it comes up asking me what server I want to run it on and it says "J2EE preview at localhost", and then it pops up a new tab showing me the web page.  I can then also access the web page from within another browser such as IE.

I've no idea what servlet engine this is using but I am not going outside the Eclipse environment to do anything, and the only other web authoring software I have on this machine is VS2005, and I don't suppose that's anything to do with it.
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 21896923
It sounds as if you are using the JBoss included with your Eclipse.  Did you configure JBoss?  If not, then you probably need to fully configure it and include its jars in your project in order to import the classes you need.

JBoss can run JSP pages, which is probably why you have had some success.  At this point, it would probably help if you would post your JSP code (whatever minimal amount which produces the error).  Make sure your import statements for the JSP code are included.
0
 
LVL 3

Accepted Solution

by:
sreehariharan earned 500 total points
ID: 21907825
Hi Let me explain u the step by step process.

Its not compile coz its not finding the servlet jar file properly.so better download the jar file and include it in your project.

1) Download servlet.jar from the link http://www.java2s.com/Code/Jar/wsit/servlet.jar.htm

2) copy the servlet.jar and paste it in your Project.

3) right click on project folder --> Build Path --> Configure Build Path

4) Java Build Path, Open Libraries tab --> Add Jar
or Add External Jar (to select external to eclipse),  --> select servlet.jar and click on OK

5) Click on Ok

 
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 21909471
sreehariharan's suggestion might work, even though it will not be the servlet.jar that goes with your particular version of JBoss.   The best thing, of course, would be to use the servlet jar from the JBoss that you're running in Eclipse, but sreehariharan's suggestion might work.

Now that we believe you are using JBoss, this message about the odd location of javax.servlet.* classes might help you:
http://jira.jboss.org/jira/browse/JBAS-4780

If you're running JBoss 4.2.0, then look for the servlet-api.jar under JBoss's /server/default/lib/ and added it to your Eclipse project's build path.
0
 

Author Comment

by:Belazir
ID: 21944123
I can't find anything to do with JBoss on my machine either, and I don't remember configuring it.

I'll try sree's suggestion.
0
 

Author Comment

by:Belazir
ID: 21944238
Yep, that seems to have done it.  Thanks sree, that's what I was looking for from the outset.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question