Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2577
  • Last Modified:

Using cookies in JSP

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
Belazir
Asked:
Belazir
  • 6
  • 4
  • 2
  • +3
1 Solution
 
objectsCommented:
sounds like you haven't added the j2ee/servlet jar into your projects buildpath.

0
 
shaz_Commented:
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
 
BelazirAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mrcoffee365Commented:
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
 
shaz_Commented:
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
 
SankoziCommented:
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
 
BelazirAuthor Commented:
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
 
objectsCommented:
what are u using as a container at the moment?
you can grab the jar from there

0
 
SankoziCommented:
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
 
BelazirAuthor Commented:
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
 
mrcoffee365Commented:
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
 
BelazirAuthor Commented:
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
 
mrcoffee365Commented:
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
 
sreehariharanCommented:
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
 
mrcoffee365Commented:
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
 
BelazirAuthor Commented:
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
 
BelazirAuthor Commented:
Yep, that seems to have done it.  Thanks sree, that's what I was looking for from the outset.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now