• 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

HFSJ book: Cookie example chapter 6 plz Help :(

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hallo everyone,
Right now I am doing the �simple custom cookie example� at Chapter 6 page 250 of the HFSJ book.

The example has 2 servlets: one to SET the cookie and the other one to GET the cookie.

1.In the XML do I need to include the 2 servlets? If yes, how can I include both of them in the servlet tag? And which one is gunna be in the servlet-mapping tag?

2.How is the form.html gunna look like?(the JSP page is exactly like the one in the book page 250)
this is how it looks like right now
****************************************
<html><body>
<h1 align="center">Hello Test Cookie Page</h1>
<form method="POST"
action="checkcookie.do">
Enter your name<p>

<input type="text" name="username" size="20"><br>
<center>
<input type="submit">
</center>
</form></body></html>
*********************************************
thanks in advance
Moh
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Moh,

1) The response to your first question is YES.
Each time you want to use a servlet, you need to register the servlet in your DD. No exception to this rule so yes, you need to declar both servlets in the WEB-INF\web.xml of you app.

You do that just the way it was explained previously on the book.
Your web.xml should be like this (assuming your classes are in the default package) :





2) Actually I do not see this JSP code in my own book! Anyway, the page should be like you wrote it, indeed.


Hope it helps...
Olivier
[ May 29, 2005: Message edited by: Olivier Cleyman ]
 
Jamed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your help Olivier

but there is a jsp on page 250 (lower part of the page)
and this is what's exactly written

JSP to render the view from this servlet

<html><body>
<a href="checkcookie.do">click here</a>
</body></html>

now my question is how should my form.html look like?

since we're including 2 servlets how can we tell the html that we're using 2 servlets in the action tag

i would greatly appricate it if you can help me with the form.html

thanks again Olivier

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

Your form should look something like this:

******************

<html><body>

<form method="POST" action="cookietest.do">

Name: <input type="text" name="username"/><br>

<input type="submit"/>

</form>

</body></html>

*******************

You are submitting this form to the CookieTest Servlet, which sets the cookie, and redirects you to the cookieresult.jsp response page.
The response page has a link that points to the second servlet (CheckCookie).

Hope this helps.

Regards,
Leigh.
 
Olivier Cleyman
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Moh,

I understand your concern now.
The thing is you mixed two JSP.

In the book there is only one JSP d�tailed. The one that gives you the kink between de CookieTest Servlet and the CheckCookie Servlet.
The Initial JSP containing the form is not in the book.

It should be like you or Leigh said.

If you do not want to write a JSP just change your doPost() method in the CookieTest Servlet into a doGet() method and acces the CookieTest directly with the url:



good luck...
[ May 30, 2005: Message edited by: Olivier Cleyman ]
 
Jamed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys it's still not working with me

this is the error i am getting, followed by my files
******************************************
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Error allocating a servlet instance
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
java.lang.Thread.run(Unknown Source)


root cause

java.lang.NoClassDefFoundError: com/example/web/CookieTest (wrong name: CookieTest)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1629)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:850)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1299)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1181)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
java.lang.Thread.run(Unknown Source)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.9
************************************************
please help
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jamed

I also got the same 'NoClassDefFoundError'. Could'nt figure out what went wrong...

HS
 
Olivier Cleyman
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Are your sure your are using the same package definition in the DD and in the classes?
Did you put your compiled classes in the right place under the WEB-INF\classes?
 
H Singh
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Olivier please take a look: '

1. Modified web.xml located at : \webapps\ROOT\WEB-INF


<servlet>
<servlet-name>testkookie</servlet-name>
<servlet-class>com.example.kookieTest</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>testkookie</servlet-name>
<url-pattern>/servlet/kookieTest.do</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>testkookie1</servlet-name>
<servlet-class>com.example.Checkkookie</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>testkookie1</servlet-name>
<url-pattern>/servlet/Checkkookie.do</url-pattern>
</servlet-mapping>


2. class files at : \webapps\ROOT\WEB-INF\classes\com\example

source code below:
package com.example;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Checkkookie extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

kookie[] kookies = request.getkookies();

for(int i=0;i<kookies.length;i++){
kookie kookie = kookies[i];
if(kookie.getName().equals("username")){
String userName = kookie.getValue();
out.println("Hello" + userName);
break;
}
}
}
}

and

package com.example;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class kookieTest extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException {

response.setContentType("text/html");

String name = request.getParameter("username");

kookie kookie = new kookie("username",name);

kookie.setMaxAge(30*60);

response.addkookie(kookie);

RequestDispatcher view = request.getRequestDispatcher("kookieresult.jsp");
view.forward(request,response);
}
}

3. form.html and cookieresult.jsp ( \Tomcat5\webapps\ROOT )

<html><body>
<h1 align="center">Hello Test kookie Page</h1>
<form method="POST" action="/servlet/kookieTest.do">
Enter your name<p>

<input type="text" name="username" size="20"><br>
<center>
<input type="submit">
</center>
</form></body></html>

and

<html><body>
<a href="/servlet/Checkkookie.do">click here</a>
</body></html>

[ May 30, 2005: Message edited by: H Singh ]
[ May 30, 2005: Message edited by: H Singh ]
 
Leigh Parsons
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi H,

Your problem is most likely due to miss-spelling Cookie.

Calling new Kookie(....) will give you a 'NoClassDefFoundError'. (Unless of course you've declared your own Kookie class).

Change all Kookie references to Cookie, that should solve your problem.

Cheers,

Leigh.
 
Leigh Parsons
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Moh,

It's hard to tell what is causing your problem without seeing your code. Possibly the CookieTest class has been created in the wrong package (or you've put the class location in the DD <servlet-class> tag incorrectly).

Also check that your CookieTest.class file exists where it should.

If you email me all your files, I'll have a quick look and let you know what is stuffing up.

Use "leigh_c_parsons@hotmail.com", as I'm going on two weeks leave tomorrow :-)

Cheers,

Leigh.
 
H Singh
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed cookie to kookie bcoz it won't let me post anything in the forum if i use cookie.
 
Leigh Parsons
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,

Sorry - my mistake.

I copied your code as you had it above, removed the "/servlet" portion from the defs in the <url-pattern> tags - ie.
was: <url-pattern>/servlet/cookieTest.do</url-pattern>
changed to: <url-pattern>/cookieTest.do</url-pattern>

and it works fine - give that a go.

Cheers,

Leigh.
 
Leigh Parsons
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PS - Sorry - also change your links in the form and jsp to just reference the servlet.
ie. change from: action="/servlet/cookieTest.do"
to: action="cookieTest.do".

Hope this helps.

Leigh.
 
Jamed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Leigh

You were right. The error was in Package declaration in my java classes. I didn't even put a package statment

thanks again Leigh for your help

HS,

Check all the things pointed out by Leigh. if it doesn't work let me know. and i'll try to help my email is mjamed2000@hotmail.com

thanks
reply
    Bookmark Topic Watch Topic
  • New Topic