• 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

please help with running this code

 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone,

could some one please help me with showing me what i have done wrong in this web app. i have tried so many times to get this to work but failed and i have turned to you guys out there who can help me. following are the codes for my web app. it consists of one servlet, one servletcontext listener class, one helper class and the web.xml class. its actually code given in the hfsj book. there is no html involved. the code is as follows.

the MyServletContextListener class is as follows;


package com.example;

import com.example.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class MyServletContextListener implements ServletContextListener{

public void contextInitialized(ServletContextEvent event){

ServletContext sc = event.getServletContext();
String doBreed = sc.getInitParameter("breed");
Dog d = new Dog(doBreed);
sc.setAttribute("dog",doBreed);

}

public void contextDestroyed(ServletContextEvent event){

}

}

the servlet class is as follows;

package com.example;

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

public class ListenerTester extends HttpServlet{

public void goGet(HttpServletRequest request, HttpServletResponse response)throws IOException,ServletException{

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("test context attributes set by listener<br>");
out.println("<br>");

String dog = (String) (getServletContext().getAttribute("dog"));
out.println("Dog's breed is " + dog);

}

}

the helper class is as follows;

package com.example;


public class Dog{

private String breed;

public Dog(String breed){

this.breed = breed;
}

public String getBreed(){

return breed;
}

}
and finally the web.xml file is as follows;
<?xml version="1.0" encoding= "ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
verion="2.4">

<servlet>
<servlet-name>ListenerTester</servlet-name>
<servlet-class>com.example.ListenerTester</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ListenerTester</servlet-name>
<url-pattern>/ListenTest.do</url-pattern>
</servlet-mapping>

<context-param>
<param-name>breed</param-name>
<param-value>Great Dane</param-value>
</context-param>

<listener>
<listener-class>com.example.MyServletContextListener</listener-class>
</listener>
</web-app>

when i put them in the appropriate folders in the development enviroment i get an error report. but what should happen is i should get the paramaeter value which is great dane and set it as a servletcontext attribute and call it from the servlet class and hence to prove that it was initialized before any servlet class was initialized. please help.

thank you
Dinuka Arseculeratne
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the type of error that occurs ?
 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i type http://localhost/listenerTest/ListenTest.do in my browser it gives the error message saying
HTTP Status 404 - /listenerTest/ListenTest.do

type Status report

message /listenerTest/ListenTest.do

description The requested resource (/listenerTest/ListenTest.do) is not available.
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Specify the port number. localhost:8080 or what ever your port number is.
 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my port number is set to the default port which is 80. it cannot be the problem because i have already run some other web apps which did work before. is there anything else i have done wrong in the code perhaps?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You wrote goGet instead doGet
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.goGet() instead of doGet()
[ September 21, 2006: Message edited by: Venkat Perumalla ]
 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you everyone for your help. things like this always happen to me. i bury myself in the code to see what has gone wrong just to find out i have misspelled a method name. ... thank you again eveyone for your reponses and taking the time to go through the code. i appreciate it

Dinuka Arseculeratne
 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In these situation it's better to paste the code to an IDE like netbeans and check for syntax errors.
 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i too had problems with the code

but i never could make out what was the problem

finally one of the ranchers sent me the code.
reply
    Bookmark Topic Watch Topic
  • New Topic