my dog learned polymorphism*
The moose likes Servlets and the fly likes servlet not running but examples does Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet not running but examples does" Watch "servlet not running but examples does" New topic
Author

servlet not running but examples does

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
Hello all
im trying to run simple example of servlet , very simple
but somehow its not working when but when I run the tomcat servlets example its working just fine here is what I have
what im doing wrong, by the way im running on unix sun solaris both with apache and tomcat and mod_jk as the connector.

my apache httpd.conf config is :

#Load mod_jk
LoadModule jk_module libexec/mod_jk.so

# Configure mod_jk
JkWorkersFile /bld/rnde/RunEnv/apache/tomcat/conf/workers.properties
JkLogFile /bld/rnde/RunEnv/apache/tomcat/logs/mod_jk.log
JkLogLevel debug
JkMount /*.jsp testWorker
JkMount /* testWorker

my tomcat directories for the new servlet under tomcat dir is :

../tomcat/webapps/myApp
../tomcat/webapps/myApp/WEB-INF/web.xml
../tomcat/webapps/myApp/WEB-INF/classes
../tomcat/webapps/myApp/WEB-INF/classes/TestingServlet.class


my web.xml looks like :

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Testing</servlet-name>
<servlet-class>TestingServlet</servlet-class>
</servlet>
</web-app>

and the jave file :
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class TestingServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Servlet Testing</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("Welcome to the Servlet Testing Center");
out.println("</BODY>");
out.println("</HTML>");
}
}
Balasubramani Dharmalingam
Ranch Hand

Joined: Dec 06, 2004
Posts: 116
You should give the url-pattern for your TestingServlet in web.xml, then you can access the servlet by using that url-pattern.


Balasubramani SD,<br />SCJP 1.4,SCWCD 1.4,SCJP 5.0<br /><a href="http://sd.balasubramani.googlepages.com" target="_blank" rel="nofollow">www.sd.balasubramani.googlepages.com</a>
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Balasubramani means setting the <servlet-mapping> tag to match url pattern to your servlet. For example:


[My Blog]
All roads lead to JavaRanch
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Meir Yan,
Welcome to JavaRanch.


Javaranch tip:

If you are going to post more than a line or two of your code, wrap that
code in a set of UBB Code tags.
Doing so will help to preserve your code's indenting, making it easier to read.
If it is easier to read, more people will actaully read it and you will
stand a better chance of getting help with your question.



As Balasubramani has stated, you will want to create a servlet-mapping entry for your servlet so the container will know how to match up your browser request with your servlet.

Also, I would advise putting all of your classes in packages.
You may be able to get the servlet working from the default package but you will run into trouble later when trying to add beans and JSPs if all of your Java classes are not packaged.

-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
I still can't see my servlet even that I added the servlet mapping.
I changed in the apache httpd.conf to take all the servlets under the myApp
so its looks like :
JkMount /myApp/* testWorker
im trying to browse as
http://sun:9999/Testing
http://sun:9999/myApp/Testing
http://sun:9999/myApp/servlet/Testing
but with no lock �

the web.xml now looks like this :

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Testing</servlet-name>
<servlet-class>TestingServlet</servlet-class>
<servlet-mapping>
<servlet-name>Testing</servlet-name>
<url-pattern>/Testing</url-pattern>
</servlet-mapping>
</servlet>
</web-app>



the jk_mod log says :

[Tue Jun 27 13:19:34 2006] [jk_ajp_common.c (537)]: ajp_unmarshal_response: Header[1] [Content-Language] = [en]
[Tue Jun 27 13:19:34 2006] [jk_ajp_common.c (851)]: received from ajp13 #731
[Tue Jun 27 13:19:34 2006] [jk_ajp_common.c (851)]: received from ajp13 #2
[Tue Jun 27 13:19:34 2006] [jk_ajp_common.c (1716)]: Into jk_endpoint_t: one, recycling connection
[Tue Jun 27 13:19:44 2006] [jk_uri_worker_map.c (445)]: Into jk_uri_worker_map_t::map_uri_to_worker
[Tue Jun 27 13:19:44 2006] [jk_uri_worker_map.c (459)]: Attempting to map URI '/myApp/servlet/Testing'
[Tue Jun 27 13:19:44 2006] [jk_uri_worker_map.c (483)]: jk_uri_worker_map_t::map_uri_to_worker, Found a context match testWor
ker -> /myApp/

that means what ? that tomcat did found it ? but why it does not show it?

in the localhost_log.2006-06-27.txt log I see :

2006-06-27 12:49:40 StandardManager[/myApp]: Seeding random number generator class java.security.SecureRandom
2006-06-27 12:49:40 StandardManager[/myApp]: Seeding of random number generator has been completed
2006-06-27 12:49:40 StandardWrapper[/myApp efault]: Loading container servlet default
2006-06-27 12:49:40 StandardWrapper[/myApp:invoker]: Loading container servlet invoker
2006-06-27 13:09:41 WebappLoader[/myApp]: Deploying class repositories to work directory /bld/rnde/RunEnv/apache/tomcat/work/S
tandalone/localhost/myApp
2006-06-27 13:09:41 WebappLoader[/myApp]: Deploy class files /WEB-INF/classes to /bld/rnde/RunEnv/apache/tomcat/webapps/myApp/
WEB-INF/classes
2006-06-27 13:09:41 StandardManager[/myApp]: Seeding random number generator class java.security.SecureRandom
2006-06-27 13:09:41 StandardManager[/myApp]: Seeding of random number generator has been completed
2006-06-27 13:09:41 StandardWrapper[/myApp efault]: Loading container servlet default
2006-06-27 13:09:41 StandardWrapper[/myApp:invoker]: Loading container servlet invoker
2006-06-27 13:19:26 WebappLoader[/myApp]: Deploying class repositories to work directory /bld/rnde/RunEnv/apache/tomcat/work/S
tandalone/localhost/myApp
2006-06-27 13:19:26 WebappLoader[/myApp]: Deploy class files /WEB-INF/classes to /bld/rnde/RunEnv/apache/tomcat/webapps/myApp/
WEB-INF/classes
2006-06-27 13:19:26 StandardManager[/myApp]: Seeding random number generator class java.security.SecureRandom
2006-06-27 13:19:26 StandardManager[/myApp]: Seeding of random number generator has been completed
2006-06-27 13:19:26 StandardWrapper[/myApp efault]: Loading container servlet default
2006-06-27 13:19:26 StandardWrapper[/myApp:invoker]: Loading container servlet invoker


what im doing wrong here ? I don�t understand .
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Before getting entangled in connector issues, I would make sure that the app is deployed properly and the servlets can be hit with a browser using Tomcat as a standalone.

Also, did you see my earlier advice about packaging that servlet?

If you would like an example of a simple hello world servlet app, go to
http://simple.souther.us and download SimpleServlet.war to your {tomcat install}/webapps directory. This will give you working example of servlet that is properly packaged and configured. If you can't hit this one, the problem is most likely in your connector code.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Your web.xml is not correct.
Should be
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
Thanks allot guys my web.xml was wrong ...
i have one more question when i write simple servlet
and i will like to test my servlet output in simple java app when i do
System.out.println("Somthing ") i will see the result
when i run java myclass
but in servlets when i build something like this :

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

public class PrimitiveServlet implements Servlet {
public void init(ServletConfig config)throws ServletException{
System.out.println("Init Servlet");
}
public void service(ServletRequest request,ServletResponse response)throws ServletException,IOException{
System.out.println("Service Servlet");
}
public void destroy(){
System.out.println("destroy Servlet");
}
public String getServletInfo(){
return null;
}
public ServletConfig getServletConfig(){
return null;
}
}
when i rune the servlet where can i see the output of the servlet ? that is the System.out.println() calls
thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Look under {tomcat install}/logs
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
Thanks again
one more basic question..
is There any matter of caching when compiling servlets?
do i need to restart the server every time i recompile the servlet?
im getting weird results, im keep getting the old servlet result even if
i did refresh the browser. is there any kind of caching parameter i need to be familure with ?
thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

It depends on the server.

With Tomcat, you will need to restart the application whenever you recompile your java classes. This can be done from the manager app that ships with Tomcat. You could also just restart Tomcat but that usually takes longer.

If you set the reloadable attribute in your Context to true, the app will reload itself every time you recompile a class that has already been loaded.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet not running but examples does
 
Similar Threads
Tomcat 404 error
Cannot run servlets in Tomcat
requested servlet is not available
Executing my first servlet program
Cannot run servlets in Tomcat