wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HFSJ book: Cookie example chapter 6 plz Help :( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "HFSJ book: Cookie example chapter 6 plz Help :(" Watch "HFSJ book: Cookie example chapter 6 plz Help :(" New topic
Author

HFSJ book: Cookie example chapter 6 plz Help :(

Jamed
Ranch Hand

Joined: Apr 17, 2005
Posts: 35
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
Olivier Cleyman
Greenhorn

Joined: May 29, 2005
Posts: 14
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

Joined: Apr 17, 2005
Posts: 35
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
Leigh Parsons
Greenhorn

Joined: May 30, 2005
Posts: 8
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.


Leigh Parsons<br />SCJP 1.4, SCWCD (preparing)
Olivier Cleyman
Greenhorn

Joined: May 29, 2005
Posts: 14
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

Joined: Apr 17, 2005
Posts: 35
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
H Singh
Ranch Hand

Joined: Apr 03, 2005
Posts: 34
Jamed

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

HS
Olivier Cleyman
Greenhorn

Joined: May 29, 2005
Posts: 14
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

Joined: Apr 03, 2005
Posts: 34
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

Joined: May 30, 2005
Posts: 8
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

Joined: May 30, 2005
Posts: 8
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

Joined: Apr 03, 2005
Posts: 34
I changed cookie to kookie bcoz it won't let me post anything in the forum if i use cookie.
Leigh Parsons
Greenhorn

Joined: May 30, 2005
Posts: 8
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

Joined: May 30, 2005
Posts: 8
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

Joined: Apr 17, 2005
Posts: 35
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
 
jQuery in Action, 2nd edition
 
subject: HFSJ book: Cookie example chapter 6 plz Help :(