Deploying Servlet on Tomcat 8.0.30 - java

I am trying to deploy a compiled Servlet class onto Apache Tomcat server 8.0.30.
But i get the following exception :
javax.servlet.ServletException: Error instantiating servlet class HelloWorld
java.lang.ClassNotFoundException: HelloWorld
My tomcat webapps/ROOT/ folder did not contain the classes folder so i created one and copied HelloWorld.class into it. I added the following lines inweb.xml :
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
Can anyone tell me what am I doing wrong?
#wero : This is the content of my HelloWorld.java :
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class HelloWorld extends HttpServlet {
private String message;
public void init() throws ServletException
{
// Do required initialization
message = "Hello World";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html");
// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy()
{
// do nothing.
}
}

You need to put the files into the correct places. Java class files need to be placed into WEB-INF/classes:
webapps/ROOT/WEB-INF/web.xml
webapps/ROOT/WEB-INF/classes/HelloWorld.class
Then start Tomcat and look if there are errors in the Tomcat log.

When tomcat had been started there were not class file in your app. Tomcat is loading classes during deployment on startup or if you manually deploy it at runtime. After that the context should be reloaded. You can't just copy your classes to the tomcat webapps folder without loading them.
Read Tomcat docs how to deploy your application.
Deployment is the term used for the process of installing a web
application (either a 3rd party WAR or your own custom web
application) into the Tomcat server.
Web application deployment may be accomplished in a number of ways
within the Tomcat server:
Statically (the web application is setup before Tomcat is started)
Dynamically (by directly manipulating already deployed web applications (relying on auto-deployment feature) or remotely by using
the Tomcat Manager web application)
The Tomcat Manager is a web application that can be used interactively
(via HTML GUI) or programmatically (via URL-based API) to deploy and
manage web applications.
There are a number of ways to perform deployment that rely on the
Manager web application. Apache Tomcat provides tasks for Apache Ant
build tool. Apache Tomcat Maven Plugin project provides integration
with Apache Maven. There is also a tool called the Client Deployer,
which can be used from a command line and provides additional
functionality such as compiling and validating web applications as
well as packaging web application into web application resource (WAR)
files.

Got it. The classes folder I created was "C"lasses where tomcat expects "c"lasses.

Related

servlet application does not work, although tomcat works correctly

I have installed Apache Tomcat on ubuntu under tomcat directory. I set CATALINA_HOME environment variable for a single session via export. Then I started tomcat from the command line via $CATALINA_HOME/bin/startup.sh. And everything worked well. I saw tomcat official page and assume that the server is installed correctly. Then I created a webb application with a single servlet under
$CATALINA_HOME/webaps/apress/
directory.
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>hello, world</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> hello world</h1>");
out.println("</body>");
out.println("</html>");
}
}
Then I compiled the file
javac -cp HelloWorldServlet.java -d ./classes -cp $CATALINA_HOME/bin/servlet-api.jar
Compillation was successful and the class file was put into the classes subdirectory of the root directory of the application.
Then I edited web.xml file.
<?xml version ="1.0" encoding ="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns.xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:/java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Chapter 2</display-name>
<description> Apress demo</description>
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/hello.html</url-pattern>
</servlet-mapping>
</web-app>
Then I restartd Tomcat. But when I try to access via
http://localhost:8080/apress/hello.html
I get Http status 404 message. What I have done wrong?
I would suggest you first try building your application in an IDE such as Netbeans or Eclipse, then study the configuration files that are automatically generated.
Look in your tomcat directory there is a logs directory. Stop tomcat. Delete all the log files and restart tomcat and hit that URL. Check the logs (mainly access log and catalina.out). Look for errors in there first.
You may also want to consider using #WebServlet instead of editing the web.xml, modern versions of tomcat can identify classes annotated with this and automatically register them as servlets.
#WebServlet(name="HelloWorld", urlPatterns={"/hello.html"})
public class HelloWorldServlet extends HttpServlet {
}
Also, how are you deploying this servlet, as a WAR file?
Two possible things. According to what you wrote, the directory you created the application was:
$CATALINA_HOME/webaps/apress/
It's supposed to be
$CATALINA_HOME/webapps/apress/
But i guess it is just a typeO.
the second thing is that you should copy the compiled file
HelloWorldServlet.class
to
apress/WEB-INF/classes
directory, or directly compile the java file into this directory

Weblogic SOAP service deployment errors reporting two ports in war when there is only one

I have a weblogic server with a cluster of two SOA servers.
I am deploying a servlet to this server with a SOAP servlet created with the JDeveloper wizard (Create Java Web Service from WSDL -> Java EE 1.5, with support for JAX-WS Annotations)
It is deployed as a standalone application.
During the development of the application I have changed the WSDL in various ways including changing the port name.
I have gone into the weblogic interface, deleted the previous instalisation and re-deployed through JDeveloper. I get the following error message:
[08:44:59 PM] weblogic.management.DeploymentException: Error encountered during prepare phase of deploying WebService module 'RJMTestBannerAdapter.war'. Two port in the callBannerService(WebserviceDescriptionBean) has the same name {http://ic.ac.uk/AIAMetaData/AIAComponents/ApplicationObjectLibrary/Banner/V1/APIJavaAdapter/BannerAPIJavaAdapterWsdl}callBanner.
It is complaining that there is two ports in the application with the same name. But there isn't.
I have inspected the .war file that JDeveloper produces
The WEB-INF/web.xml has a single entry for the service
The WEB-INF/weblogic.xml is also correct and just specifies the context-root.
I have determined that somewhere in the weblogic servers there must be something with the end point that is not being cleared away when I delete the application.
I have checked the weblogic console user interface, gone through the list of web applications and services and confirmed it is not there.
Does the weblogic server have a deployment plan file or something simular it is picking up while deploying? If so where would this deployment plan be?
I don't think it is important but here is my web.xml:
<?xml version = '1.0' encoding = 'UTF-8'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee">
<servlet>
<servlet-name>callBanner</servlet-name>
<servlet-class>ic.ac.uk.ic.rjmtest.banner.adapter.gen.main.Main_ptImpl12</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>callBanner</servlet-name>
<url-pattern>/callBanner</url-pattern>
</servlet-mapping>
</web-app>
Also this is the weblogic.xml that appears in the war file. (It is not in my project but JDeveloper must generate it)
<?xml version = '1.0' encoding = 'UTF-8'?>
<weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd" xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app">
<context-root>RJMTestBannerAdapter</context-root>
</weblogic-web-app>
I have also inspected the WSDL and there is only a single port.
I finally found the answer.
When I used JDeveloper to regenerate the classes for the service I must have changed the name somehow. This resulted in new java being built.
As I didn't delete the classes or deploy directory the old ones were still there in my war file. So I was trying to deploy two even though my .java files didn't reflect this.
Deleting the classes and deploy directories then recompiling resolved the issue.
This post might be useful for anyone else who hits this error!

Failure trying to deploy GWT on Apache Tomcat

I am working on a GWT application. The app. is working in dev mode.
I am working on a gwt project and trying to deploy it to Apache Tomcat. I have never used Apache Tomcat before and I am rather new to Java and GWT. My tomcat server seems to be up and running as I see the "If you see this you have succesfully installed Tomcat" on localhost:8080/
After I got Tomcat up and running I used Eclipse GWT Compile to compile my app. I have copied my .html file and .css file + the war folder to C:\apache-tomcat-8.0.15\webapps\MyAPP\
Opening the .html file (file:///C:/apache-tomcat-8.0.15/webapps/PurchaseOrder/PurchaseOrder.html) gives me my ui (login screen), but when making the first RPC call (loggin in) I am getting the error "FailureUnable to initiate the asynchronous service invocation (PurchaseOrderService_Proxy.checkUsernameAndPassword) -- check the network connection"
My thougts are that something is incorrect in my web.xml or my service class
PurchaseOrderService.java
package com.google.gwt.sample.purchaseorder.client;
import java.util.ArrayList;
import java.util.HashMap;
import com.google.gwt.sample.purchaseorder.client.model.Brands;
import com.google.gwt.sample.purchaseorder.client.model.Item;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
#RemoteServiceRelativePath("exampleservice")
public interface PurchaseOrderService extends RemoteService {
boolean checkUsernameAndPassword(String value, String value2);
ArrayList<Item> getPersonalInfo();
HashMap<String, ArrayList<Item>> getListOfPurchaseOrderSortedFromBrands();
String createExcelExportFile(HashMap<String, ArrayList<Item>> exportMap);
}
web.xml:
<!-- Servlets -->
<servlet>
<servlet-name>purchaseOrderServiceImpl</servlet-name>
<servlet-class>com.google.gwt.sample.purchaseorder.server.PurchaseOrderServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>purchaseOrderServiceImpl</servlet-name>
<url-pattern>/purchaseorder/exampleservice</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>PurchaseOrder.html</welcome-file>
</welcome-file-list>
</web-app>
Opening the .html file
(file:///C:/apache-tomcat-8.0.15/webapps/PurchaseOrder/PurchaseOrder.html)
gives me my ui (login screen), but when making the first RPC call
(loggin in) I am getting the error "FailureUnable to initiate the
asynchronous service invocation
(PurchaseOrderService_Proxy.checkUsernameAndPassword) -- check the
network connection"
This is not how you access the application running in Tomcat. Your tomcat is running # localhost:8080 so you have to access using http://localhost:8080/<app_context_root>/filename.html

Remote API JAVA url does not work GAE

I have deployed application into GAE. When i try the url as http://aabbbaaacccc.appspot.com/_ah/remote_api. I am getting 404 Error page. I have added in web.xml file. I have given correct app id. It deploys. After deployment successful, An dialog box appears and displays file not found along with notepad.
<servlet>
<display-name>Remote API Servlet</display-name>
<servlet-name>RemoteApiServlet</servlet-name>
<servlet-class>com.google.apphosting.utils.remoteapi.RemoteApiServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RemoteApiServlet</servlet-name>
<url-pattern>/remote_api</url-pattern>
</servlet-mapping>
I need to deploy my app into server and start a Remote api with an other application and share the entites from an other app.
I am struggling with this issue for past 2 days. Please help me.
U can look at the error dialog box in the following link.
http://i40.tinypic.com/bfgzki.png
Thanks.
Appengine should works fine.. i've listed the all details for basic project setup. please look and find what you missed.
The Servlet Class
App Engine Java applications use the Java Servlet API to interact with the web server.
In the directory src/guestbook/, make a file named GuestbookServlet.java with the following contents:
package guestbook;
import java.io.IOException;
import javax.servlet.http.*;
public class GuestbookServlet extends HttpServlet {
#Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
}
}
The web.xml File
When the web server receives a request, it determines which servlet class to call using a configuration file known as the "web application deployment descriptor." This file is named web.xml, and resides in the war/WEB-INF/ directory in the WAR. WEB-INF/ and web.xml are part of the servlet specification.
In the directory war/WEB-INF/, a file named web.xml has the following contents:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC
"-//Oracle Corporation//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<servlet>
<servlet-name>guestbook</servlet-name>
<servlet-class>guestbook.GuestbookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>guestbook</servlet-name>
<url-pattern>/guestbook</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
This web.xml file declares a servlet named guestbook, and maps it to the URL path /guestbook.
The appengine-web.xml File
App Engine needs one additional configuration file to figure out how to deploy and run the application. This file is named appengine-web.xml, and resides in WEB-INF/ alongside web.xml.
In the directory war/WEB-INF/, a file named appengine-web.xml has the following contents:
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application></application>
<version>1</version>
<threadsafe>true</threadsafe>
</appengine-web-app>
appengine-web.xml is specific to App Engine, and is not part of the servlet standard. You can find XML schema files describing the format of this file in the SDK, in the appengine-java-sdk/docs/ directory. See Configuring an App for more information about this file.
Running the Project
The App Engine SDK includes a web server application you can use to test your application.
select Debug As > Web Application.
Testing the Application
Start the server, then visit the server's URL in your browser. If you're using Eclipse and the Google Eclipse plugin, the server runs using port 8888 by default:
http://localhost:8888/guestbook
If you're using the dev_appserver command to start the server, the default port is 8080:
For details please see following tutorials:
Tutorial 1:
Tutorial 2:
Tutorial 3:

Tomcat 6 not recognizing <url-mapping>

I have what I think is the simplest possible
hello world example (see below). But when asking for
"http://localhost:8080/hello" thru firefox,
it gives me the
"The requested resource (/hello/) is not available"
error.
Environment: newly installed tomcat 6.0.32 on Windows 7.
Other information:
1. None of the "similar questions" provides any clues.
From experimentation, it appears that tomcat is not
doing the mapping from localhost:8080/hello to my servlet.
I set "<load-on-startup>"
which showed me that the servlet's init entry was being
called, but doGet() is never called.
The log files show no errors.
I have tried both starting tomcat with the hello
directory already in webapps, with hello.war in
webapps, and deploying using the manager application.
All act the same way.
Some possibilities I have considered:
According to the documentation, I should
not need to use a context.xml file, and my experiments
with a context.xml produced the same resource not found
error.
localhost:8080/hello should instead be
localhost:8080/.../hello, but if so, then what is
the ... supposed to be?
Trailing / (e.g. /hello versus /hello/). I changed
the url-pattern to "/hello/*", but it fails the same
way.
I assume the problem is something simple, but I cannot
see it.
[Added 8/8/2011]
The answers about using context.xml were correct; thanks.
In looking around, it appears that an alternate way
to achieve the same effect is to put this
into my web.xml file.
<context-param>
<param-name>ContextPath</param-name>
<param-value>/dts</param-value>
</context-param>
web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:/java.sun.com/dtd/web-app_2_3.dtd">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>test.HelloServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
HelloServlet.java:
package test;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class HelloServlet extends HttpServlet {
public void init()
{
System.out.println("\nHelloServlet.init");
}
public void doGet (HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
System.out.println("\nHelloServlet.doGet");
PrintWriter out = res.getWriter();
out.println("Hello, world!");
out.close();
}
}
With a Java Servlet Application (part of, but not the total sum of Java EE - Java Enterprise Edition), applications have servlets under what is called a "context path". This "context path" has to be specified in order to map any request to the application.
Apache Tomcat makes this context path pretty easy to configure, either via server.xml (not recommended) or individual context files (recommended). Both ways specify where to find your web application directory (an unpacked web application archive, or WAR file) and where to place it on the server at a context path.
As Vlad has already said, if you deploy your war file into Tomcat's webapps directory and have automatic installation on (I believe it is on by default), Tomcat will unpack the .war into a directory under that location and use the war's name as its context path. His example war file is named "helloapp.war", so, with the default settings, it would receive any request to http://localhost:8080/helloapp because its context path becomes helloapp.
Of course, once the request is sent to the context path, something needs to match against it. That's where the web.xml comes in to play. While it is possible to use the root as a matcher (every request to the context path gets handled by the same process), typically a pattern is used (such as *.do, *.action, etc), so that individual requests to the helloapp are easily distinguishable (it's easier to read and debug http://localhost:8080/helloapp/login.action and http://localhost:8080/helloapp/doSomethingElse.action than both being recognized via some parameters and the same path of http://localhost:8080/helloapp in my opinion)
So, the context path gets to your application, then your application has to do a lookup on the web.xml to see where to send the actual request. In your example, if your webapp was deployed at the context path of helloapp, to access it with the proper mapping, you would simply append /hello, so the request becomes http://localhost:8080/helloapp/hello
You are deploying your hello servlet in a webapp. Assuming the webapp is in a folder helloapp or in an archive helloapp.war in Tomcat's webapps directory then your sevlet would be accessible at http://localhost:8080/helloapp/hello
You will either need to rename the package to ROOT.war (or the ROOT directory) or modify the ROOT.xml context.xml file to point to the hello folder.
If you go to /hello/hello I bet you'll see your app. If you're using tomcat, use context.xml.
You may not need to use it for a webapp to work, but if you deploy under tomcat, things just work more coherently when you have a context.xml file.
In /yourtomcatinstall/webapps/hello/META-INF/ create a context.xml file with this information"
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/hello">
</Context>
And change the url mapping of your servlet in web.xml to / and/or /* you can have more than one url mapping for a servlet.

Categories