Solved

Using cookies in JSP

Posted on 2008-06-23
17
2,409 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
  • 6
  • 4
  • 2
  • +3
17 Comments
 
LVL 92

Expert Comment

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

0
 
LVL 3

Expert Comment

by:shaz_
Comment Utility
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
Comment Utility
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
 
LVL 26

Expert Comment

by:mrcoffee365
Comment Utility
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_
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
what are u using as a container at the moment?
you can grab the jar from there

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:Sankozi
Comment Utility
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
Comment Utility
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 26

Expert Comment

by:mrcoffee365
Comment Utility
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
Comment Utility
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 26

Expert Comment

by:mrcoffee365
Comment Utility
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
Comment Utility
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 26

Expert Comment

by:mrcoffee365
Comment Utility
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
Comment Utility
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
Comment Utility
Yep, that seems to have done it.  Thanks sree, that's what I was looking for from the outset.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now