?
Solved

Basics of using J2EE?

Posted on 2005-04-13
36
Medium Priority
?
338 Views
Last Modified: 2013-11-24
Hello,

Could anyone advise? I am using win98, and installed JE221.3. I added to this to the classpath, then installed tomcat.

I have this example code, which I compiled without error.

But I am not sure how to run the code using tomcat?

Thanks

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

public class PrintLine extends HttpServlet{

   public void doGet(HttpServletRequest req, HttpServletResponse res)
         throws ServletException, IOException{

      res.setContentType("text/html");
      PrintWriter out = res.getWriter();
      out.println("<HTML>");
      out.println("<HEAD><TITLE>Example Script</TITLE></HEAD>");
      out.println("<BODY>");
      out.println("<BIG>Hello World</BIG>");
      out.println("</BODY></HTML>");
   }
}
0
Comment
Question by:AntoniRyszard656
  • 18
  • 16
  • 2
36 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 280 total points
ID: 13770512
add:

package mycode ;

to the top of the file, compile it, copy the .class file to tomcat/webapps/ROOT/WEB-INF/classes/mycode

and add the following lines to web.xml:

  <servlet>
    <servlet-name>firstServlet</servlet-name>
    <servlet-class>mycode.PrintLine</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>firstServlet</servlet-name>
    <url-pattern>/First</url-pattern>
  </servlet-mapping>

then go to:

http://localhost:8080/First

to see the output :-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13770514
(you have to do the first bit, as Tomcat requires Servlets are in packages)

Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13770517
>> and installed JE221.3

If you have Tomcat, you don't need the J2EE library...

Just the "servlet.jar" in tomcat/common/lib
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:AntoniRyszard656
ID: 13770540
Thanks

I'll just try this.
0
 

Author Comment

by:AntoniRyszard656
ID: 13770723
Should the web.xml look:

<?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>
  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>

  <servlet>
    <servlet-name>firstServlet</servlet-name>
    <servlet-class>mycode.PrintLine</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>firstServlet</servlet-name>
    <url-pattern>/First</url-pattern>
  </servlet-mapping>
</web-app>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13770733
That looks fine :-)

Are you getting errors?

Can you post them here if so?

You may need to restart tomcat (that's always a good first step after you have changed something) ;-)

Tim
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13770747
Have you changed the pott at which it is listening? What URL are you using to access the Servlet?
0
 

Author Comment

by:AntoniRyszard656
ID: 13770754
The errors are:

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: Wrapper cannot find servlet class mycode.PrintLine or a class it depends on
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:891)
      at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:668)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2422)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:163)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:199)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:700)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Thread.java:534)


root cause

java.lang.ClassNotFoundException: mycode.PrintLine
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1443)
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1289)
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:885)
      at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:668)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2422)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:163)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:199)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:700)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Thread.java:534)



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

Apache Tomcat/4.1.30
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13770765
You did change your source to:

---------------------------------------------------------------
package mycode ;

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

public class PrintLine extends HttpServlet{

   public void doGet(HttpServletRequest req, HttpServletResponse res)
         throws ServletException, IOException{

      res.setContentType("text/html");
      PrintWriter out = res.getWriter();
      out.println("<HTML>");
      out.println("<HEAD><TITLE>Example Script</TITLE></HEAD>");
      out.println("<BODY>");
      out.println("<BIG>Hello World</BIG>");
      out.println("</BODY></HTML>");
   }
}
---------------------------------------------------------------

and copy the class to

    ROOT/WEB-INF/classes/mycode

like I said didn't you?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13770767
>> and copy the class to

(after compiling it obviously)
0
 

Author Comment

by:AntoniRyszard656
ID: 13770772
The above error is shown when the I am not on broadband.

But when I am using broadband the error changes to:

HTTP Status 503 - Servlet firstServlet is currently unavailable

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

type Status report

message Servlet firstServlet is currently unavailable

description The requested service (Servlet firstServlet is currently unavailable) is not currently available.


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

Apache Tomcat/4.1.30
0
 

Author Comment

by:AntoniRyszard656
ID: 13770780
Yes I made to changes you said.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13770801
>>  The above error is shown when the I am not on broadband.
>> But when I am using broadband the error changes to:

?!

Are you sure you're just running one version of Tomcat?

Do the examples work that came with it?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13770806
if you're using Tomcat 4, you MAY be able to do:

http://localhost:8080/servlet/mycode.PrintLine

or maybe

http://localhost:8080/servlet/mycode/PrintLine

I can't remember which one...
0
 

Author Comment

by:AntoniRyszard656
ID: 13770814
When tomcat is running, I can access the

http://localhost:8080/index.jsp page, and this jsp includes a link to the examples which run.
0
 

Author Comment

by:AntoniRyszard656
ID: 13770829
Could see any reason why the error message changes when I use tomcat with/ or without broadband running.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13770862
>>  Could see any reason why the error message changes when I use tomcat with/ or without broadband running.

Nope...  That has me baffled :-(

Try changing

    <url-pattern>/First</url-pattern>

to

    <url-pattern>First</url-pattern>

then see if the error changes again...?

Have you restarted tomcat?

What errors do you get in catalina.out as it starts up?
0
 

Author Comment

by:AntoniRyszard656
ID: 13770869
Both of these give the error below:

http://localhost:8080/servlet/mycode.PrintLine
http://localhost:8080/servlet/mycode/PrintLine


HTTP Status 404 - /servlet/mycode.PrintLine

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

type Status report

message /servlet/mycode.PrintLine

description The requested resource (/servlet/mycode.PrintLine) is not available.


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

Apache Tomcat/4.1.30
0
 

Author Comment

by:AntoniRyszard656
ID: 13770897
Could it be that broadband and tomcat are both using 8080
0
 

Author Comment

by:AntoniRyszard656
ID: 13770918
During the start-up Java says: There does not appear to be any errors.

13-Apr-2005 11:53:23 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on port 8080
Starting service Tomcat-Standalone
Apache Tomcat/4.1.30
13-Apr-2005 11:53:42 org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.util.LocalStrings', returnNull=
e
13-Apr-2005 11:53:42 org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.action.ActionResources', return
l=true
13-Apr-2005 11:53:46 org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.webapp.admin.ApplicationResources', re
nNull=true
- Unable to find config file.  Creating new servlet engine config file: /WEB-
/server-config.wsdd
13-Apr-2005 11:54:51 org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on port 8080
13-Apr-2005 11:54:53 org.apache.jk.common.ChannelSocket init
INFO: JK2: ajp13 listening on /0.0.0.0:8009
13-Apr-2005 11:54:54 org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/500  config=C:\Tomcat\bin\..\conf\jk2.properties
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13770942
>>  Could it be that broadband and tomcat are both using 8080

No...broadband shouldn't "use" any port...
0
 

Author Comment

by:AntoniRyszard656
ID: 13770952
There is a HelloWorld example in:

C:\Tomcat\webapps\examples\WEB-INF\classes\

Would you have any how to run this?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13770963
go to:

http://localhost:8080/examples

there should be the "Hello World" example in the "Servlets" category...
0
 

Author Comment

by:AntoniRyszard656
ID: 13770998
Thanks

When I ran this example, Hello World was displayed.

http://localhost:8080/examples/servlet/HelloWorldExample

So now we know tomcat will run servlets on my pc.

Do you know if there is a web.xml for these servlet examples?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13771020
yeah...inside

  webapps/examples/WEB-INF

Hope that sheds some light on it...I get the feeling I'm missing something important :-(

Tim
0
 

Author Comment

by:AntoniRyszard656
ID: 13771054
I'll have a look.
0
 

Author Comment

by:AntoniRyszard656
ID: 13771635
Hello,

I had a look over the webapps/examples/WEB-INF web.xml file, but could not really understand the differences from the version you suggested.

I wondered if you had a chance to look over the webapps/examples/WEB-INF web.xml
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13771665
it should be like I said...

right...can we start from scratch?

Can you post your current java file, how you are compiling it, where you are putting it, and your web.xml?
0
 

Author Comment

by:AntoniRyszard656
ID: 13772883
package mycode;
import java.io.*;
import javax.servlet.*;
The code is:

import javax.servlet.http.*;

public class PrintLine extends HttpServlet{

   public void doGet(HttpServletRequest req, HttpServletResponse res)
         throws ServletException, IOException{

      res.setContentType("text/html");
      PrintWriter out = res.getWriter();
      out.println("<HTML>");
      out.println("<HEAD><TITLE>Example Script</TITLE></HEAD>");
      out.println("<BODY>");
      out.println("<BIG>Hello World</BIG>");
      out.println("</BODY></HTML>");
   }
}

Once compiled I copied the .class file, but not the .Java file to:

C:\Tomcat\webapps\ROOT\WEB-INF\classes\PrintLine.class

And the web.xml file is positioned at:

C:\Tomcat\webapps\ROOT\WEB-INF\web.xml

And web.xml contains:

<?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>
  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>

  <servlet>
    <servlet-name>firstServlet</servlet-name>
    <servlet-class>mycode.PrintLine</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>firstServlet</servlet-name>
    <url-pattern>/First</url-pattern>
  </servlet-mapping>
</web-app>
0
 

Author Comment

by:AntoniRyszard656
ID: 13772917
The other thought I had was, since the web.xml is ready written in the:

webapps/examples/WEB-INF

Could I just add my PrintLine.java and PrintLine.class files with these examples, and run it from there.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13772955
>> Once compiled I copied the .class file, but not the .Java file to:
>> C:\Tomcat\webapps\ROOT\WEB-INF\classes\PrintLine.class

copy it to:

tomcat/webapps/ROOT/WEB-INF/classes/mycode/PrintLine.class

like I said in my first comment ;-)
0
 

Author Comment

by:AntoniRyszard656
ID: 13773836
Thanks again the code is working.

Could I ask finally, would you have an example of the same code but using JSP?

And be-able to explain how to run this, I was told once the jsp are converted to servlets and wondered if the jsp code was written in a .java file.

Thanks
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13773916
create a file called "PrintLine.jsp"

and in it, put:

--------------------
<HTML>
    <HEAD><TITLE>Example Script</TITLE></HEAD>
    <BODY>
        <BIG>Hello World</BIG>
    </BODY>
</HTML>
--------------------

and save it in webapps/ROOT

then, go to http://localhost:8080/PrintLine.jsp

That is the identical match to your servlet...  Of course, if you want to use some java in the JSP, you can do:

--------------------
<HTML>
    <HEAD><TITLE>Example Script</TITLE></HEAD>
    <BODY>
        <BIG>Hello World</BIG><br>
<%
    out.println( "The current date is " + new java.util.Date().toString() ) ;
%>
    </BODY>
</HTML>
--------------------

which is a bit more interesting ;-)

Good luck!

Tim
0
 

Author Comment

by:AntoniRyszard656
ID: 13774044
Thanks I'll try this, I noticed jsp seems very much like php.

Could I ask finally, when I write other servlets and place the .class files in the mycode folder.

Would I need to add a separate piece of code like this to the web.xml

  <servlet>
    <servlet-name>firstServlet</servlet-name>
    <servlet-class>mycode.PrintLine</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>firstServlet</servlet-name>
    <url-pattern>/First</url-pattern>
  </servlet-mapping>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13776728
In the latest versions of Tomcat...yes, you would..
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13778759
>> Would I need to add a separate piece of code like this to the web.xml

In the previous versions when the invoker servlet was supported (mainly for testing purposes), you could've directly accessed it using /servlet/Second, so on and so forth. But I guess since version 5, they removed that facility - so every Servlet must have an entry in the web.xml.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

807 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