Maven Jetty Plugin - Servlets Aren't Working - java

I have the following servlet coded in /src/main/java/examples/web/SimpleServlet.java:
package examples.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SimpleServlet extends HttpServlet {
public void goGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
PrintWriter writer = res.getWriter();
writer.println("SimpleServlet Executed!");
writer.flush();
writer.close();
}
}
And the following web.xml defined in src/main/webapp/WEB-INF/web.xml:
<!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>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>SimpleServlet</servlet-name>
<servlet-class>examples.web.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>/simpleservlet</url-pattern>
</servlet-mapping>
</web-app>
And the following pom.xml defined in /pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>examples.web</groupId>
<artifactId>simple-webapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>simple-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>simple-webapp</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
When I run mvn jetty:run I get output indicating that everything is working correctly. When I load localhost:8080/simple-webapp I am served src/main/webapp/index.jsp, so I know Jetty is at least partially working.
And now for the question: Although I am able to get to index.jsp, I cannot get to /simple-webapp/SimpleServlet -- I just get a 404 error from Jetty. Because of my configuration in web.xml, I was expecting to see SimpleServlet Executed! from SimpleServlet. What am I doing wrong?
Here is the relevant output from mvn jetty:run:
[INFO] Configuring Jetty for project: simple-webapp Maven Webapp
[INFO] Webapp source directory = C:\Users\Ken\Workspace\Eclipse\Maven\simple-webapp\src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = C:\Users\Ken\Workspace\Eclipse\Maven\simple-webapp\target\classes
2011-11-23 16:59:56.220:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
[INFO] Context path = /simple-webapp
[INFO] Tmp directory = determined at runtime
[INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] web.xml file = C:\Users\Ken\Workspace\Eclipse\Maven\simple-webapp\src\main\webapp\WEB-INF\web.xml
[INFO] Webapp directory = C:\Users\Ken\Workspace\Eclipse\Maven\simple-webapp\src\main\webapp

The URLs are case-sensitive. The defined URL pattern does not match the value that you are requesting:
<url-pattern>/simpleservlet</url-pattern>
Try http://localhost:8080/simple-webapp/simpleservlet (lowercase servlet name).

Related

How to enable JSP in a Jetty server in a JAR file?

tl;dr: How can I enable JSP support for this project (which you can also download as a zip file)?
I'm trying to create a simple "hello world" web application using Jetty, and I'm pretty happy with what I have so far. The important files are:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.happycoding</groupId>
<artifactId>app-engine-hello-world</artifactId>
<version>1</version>
<properties>
<!-- App Engine currently supports Java 11 -->
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty.version>9.4.31.v20200723</jetty.version>
<!-- Project-specific properties -->
<exec.mainClass>io.happycoding.ServerMain</exec.mainClass>
<googleCloudProjectId>YOUR_PROJECT_ID_HERE</googleCloudProjectId>
</properties>
<dependencies>
<!-- Java Servlets API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-annotations</artifactId>
<version>${jetty.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Copy static resources like html files into the output jar file. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-web-resources</id>
<phase>compile</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>
${project.build.directory}/classes/META-INF/resources
</outputDirectory>
<resources>
<resource><directory>./src/main/webapp</directory></resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- Package everything into a single executable jar file. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${exec.mainClass}</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<!-- App Engine plugin for deploying to the live site. -->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<projectId>${googleCloudProjectId}</projectId>
<version>1</version>
</configuration>
</plugin>
</plugins>
</build>
</project>
ServerMain.java
package io.happycoding;
import java.net.URL;
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
/**
* Starts up the server, including a DefaultServlet that handles static files,
* and any servlet classes annotated with the #WebServlet annotation.
*/
public class ServerMain {
public static void main(String[] args) throws Exception {
// Create a server that listens on port 8080.
Server server = new Server(8080);
WebAppContext webAppContext = new WebAppContext();
server.setHandler(webAppContext);
// Load static content from inside the jar file.
URL webAppDir =
ServerMain.class.getClassLoader().getResource("META-INF/resources");
webAppContext.setResourceBase(webAppDir.toURI().toString());
// Enable annotations so the server sees classes annotated with #WebServlet.
webAppContext.setConfigurations(new Configuration[]{
new AnnotationConfiguration(),
new WebInfConfiguration(),
});
// Look for annotations in the classes directory (dev server) and in the
// jar file (live server)
webAppContext.setAttribute(
"org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
".*/target/classes/|.*\\.jar");
// Handle static resources, e.g. html files.
webAppContext.addServlet(DefaultServlet.class, "/");
// Start the server! 🚀
server.start();
System.out.println("Server started!");
// Keep the main thread alive while the server is running.
server.join();
}
}
index.jsp
<%# page import="java.util.Date" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Google Cloud Hello World</title>
</head>
<body>
<h1>Google Cloud Hello World</h1>
<p>This is a sample HTML file. Click here to see content served from a servlet.</p>
<p>Learn more at HappyCoding.io.</p>
<p>The current time is: <%= new Date().toString() %></p>
</body>
</html>
The server starts up and renders HTML perfectly. Servlets also work. But when I try to use JSP like above, I see the JSP code rendered in the HTML instead of being parsed as Java.
I've tried googling, but every tutorial I've found works by creating a separate WAR file. My server is launched from a JAR file, and I'm trying to keep the code as simple as possible, so I'm trying to avoid using a separate WAR file if possible.
Is there a small change I can make to my pom.xml file and my ServerMain.java file to enable JSP?
To enable JSP support in the embedded Jetty server you need to do two things, modify your project pom.xml file to include the necessary dependencies, and configure the JettyJspServlet and related Jetty stuff in your ServerMain class.
First, include the following dependencies on your project pom.xml. They will provide support for JSP an JSTL:
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>apache-jsp</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>apache-jstl</artifactId>
<version>${jetty.version}</version>
<type>pom</type>
</dependency>
Then, use this modified version of the ServerMain class:
package io.happycoding;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import org.apache.tomcat.util.scan.StandardJarScanner;
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.apache.jsp.JettyJasperInitializer;
import org.eclipse.jetty.jsp.JettyJspServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
/**
* Starts up the server, including a DefaultServlet that handles static files,
* and any servlet classes annotated with the #WebServlet annotation.
*/
public class ServerMain {
public static void main(String[] args) throws Exception {
// Create a server that listens on port 8080.
Server server = new Server(8080);
WebAppContext webAppContext = new WebAppContext();
server.setHandler(webAppContext);
// Load static content from inside the jar file.
URL webAppDir =
ServerMain.class.getClassLoader().getResource("META-INF/resources");
webAppContext.setResourceBase(webAppDir.toURI().toString());
// Enable annotations so the server sees classes annotated with #WebServlet.
webAppContext.setConfigurations(new Configuration[]{
new AnnotationConfiguration(),
new WebInfConfiguration(),
});
// Look for annotations in the classes directory (dev server) and in the
// jar file (live server)
webAppContext.setAttribute(
"org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
".*/target/classes/|.*\\.jar");
// Handle static resources, e.g. html files.
webAppContext.addServlet(DefaultServlet.class, "/");
// Configure JSP support.
enableEmbeddedJspSupport(webAppContext);
// Start the server! 🚀
server.start();
System.out.println("Server started!");
// Keep the main thread alive while the server is running.
server.join();
}
/**
* Setup JSP Support for ServletContextHandlers.
* <p>
* NOTE: This is not required or appropriate if using a WebAppContext.
* </p>
*
* #param servletContextHandler the ServletContextHandler to configure
* #throws IOException if unable to configure
*/
private static void enableEmbeddedJspSupport(ServletContextHandler servletContextHandler) throws IOException
{
// Establish Scratch directory for the servlet context (used by JSP compilation)
File tempDir = new File(System.getProperty("java.io.tmpdir"));
File scratchDir = new File(tempDir.toString(), "embedded-jetty-jsp");
if (!scratchDir.exists())
{
if (!scratchDir.mkdirs())
{
throw new IOException("Unable to create scratch directory: " + scratchDir);
}
}
servletContextHandler.setAttribute("javax.servlet.context.tempdir", scratchDir);
// Set Classloader of Context to be sane (needed for JSTL)
// JSP requires a non-System classloader, this simply wraps the
// embedded System classloader in a way that makes it suitable
// for JSP to use
ClassLoader jspClassLoader = new URLClassLoader(new URL[0], ServerMain.class.getClassLoader());
servletContextHandler.setClassLoader(jspClassLoader);
// Manually call JettyJasperInitializer on context startup
servletContextHandler.addBean(new JspStarter(servletContextHandler));
// Create / Register JSP Servlet (must be named "jsp" per spec)
ServletHolder holderJsp = new ServletHolder("jsp", JettyJspServlet.class);
holderJsp.setInitOrder(0);
holderJsp.setInitParameter("logVerbosityLevel", "DEBUG");
holderJsp.setInitParameter("fork", "false");
holderJsp.setInitParameter("xpoweredBy", "false");
holderJsp.setInitParameter("compilerTargetVM", "1.8");
holderJsp.setInitParameter("compilerSourceVM", "1.8");
holderJsp.setInitParameter("keepgenerated", "true");
servletContextHandler.addServlet(holderJsp, "*.jsp");
}
/**
* JspStarter for embedded ServletContextHandlers
*
* This is added as a bean that is a jetty LifeCycle on the ServletContextHandler.
* This bean's doStart method will be called as the ServletContextHandler starts,
* and will call the ServletContainerInitializer for the jsp engine.
*
*/
public static class JspStarter extends AbstractLifeCycle implements ServletContextHandler.ServletContainerInitializerCaller
{
JettyJasperInitializer sci;
ServletContextHandler context;
public JspStarter (ServletContextHandler context)
{
this.sci = new JettyJasperInitializer();
this.context = context;
this.context.setAttribute("org.apache.tomcat.JarScanner", new StandardJarScanner());
}
#Override
protected void doStart() throws Exception
{
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(context.getClassLoader());
try
{
sci.onStartup(null, context.getServletContext());
super.doStart();
}
finally
{
Thread.currentThread().setContextClassLoader(old);
}
}
}
}
As you can see, the principal difference with the previous code is the inclusion of the method call to a new method enableEmbeddedJspSupport while configuring the embedded Jetty server in main.
The enableEmbeddedJspSupport method and the companion class JspStarter are an adapted version of the code you can find in the Main class in this Github repository.

Deploy servlet with IntelliJ IDEA to local Tomcat server

Trying to deploy simple servlet to Tomcat server. After select Run Tomcat... I'm redirected to http://localhost:8080/hi_war_exploded/ with webpage with one word - $END$. Logfiles reports no error.
I was expecting to see hw folder with my application in tc9\webapps, but found nothing.
What does the $END$ means? Where is my application on TomCat server? How to put my servlet to TomCat server?
Servlet:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class hw 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.
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>hw</servlet-name>
<servlet-class>hw</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hw</servlet-name>
<url-pattern>/hw</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Hello</groupId>
<artifactId>hw</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
IntelliJ IDEA doesn't copy the webapp files into TOMCAT\webapps.
It modifies Tomcat configuration in CATALINA_BASE and deploys the artifact directly from its output directory to avoid copying the files which can take a lot of extra time, especially for the large projects.
hi_war_exploded is the context configured in Tomcat Run/Debug configuration, Deployment tab.
In the root of this context you have the default index.jsp page generated by IntelliJ IDEA on project creation.
When you open http://localhost:8080/hi_war_exploded/ URL, Tomcat serves index.jsp from the Web Resource root of your application.
$END$ is a part of the the new JSP file template. When you create a new JSP file in a project, cursor is placed at this location.
When the project wizard generates the Web Application project and places index.jsp file from the template, it doesn't expand the $END$ macro, so it appears in the JSP file. It's actually a known bug in IntelliJ IDEA.
Your servlet is available at http://localhost:8080/hi_war_exploded/hw URL.
To make it available at http://localhost:8080/hw URL instead you need to change the Application context to / as shown on this screenshot:

Java - IntelliJ Community Maven Web App

I'm trying out Java coming over from .NET and want to make a simple page that does the following:
Implements RESTful API
Connects to a MySQL Database
Implements Role-based Security
I tried out Eclipse but wasn't met with much success, plus the code completion was very slow compared to VS2017. So I'm trying out IntelliJ Community. I've built a Maven project, but my URL is not running, also it looks like I'm not getting any code completion in my .java files. It did not build the app directories automatically.
My POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.johnnytest.webapp</groupId>
<artifactId>jerseyexample</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>jerseyexample Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>snapshot-repository.java.net</id>
<name>Java.net Snapshot Repository for Maven</name>
<url>https://maven.java.net/content/repositories/snapshots/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.26</version>
</dependency>
</dependencies>
<build>
<finalName>jerseyexample</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/</url>
<!--<username>joe</username>-->
<!--<password>joe</password>-->
</configuration>
</plugin>
</plugins>
</build>
</project>
My web.xml:
<!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>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.johnnytest.app</param-value>
<load-on-startup>1</load-on-startup>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
My Java file:
package com.johnnytest.app;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
#Path("/test")
public class MainApp {
#GET
#Path("/{param}")
public Response getMessage(#PathParam("param") String message) {
String output = "Jersey says " + message;
return Response.status(200).entity(output).build();
}
}
Also it's running Tomcat 7, and I've installed 9 on my system. Is there any way to get it to run on my existing Tomcat install? When I go to any URL on localhost:8080 I just get a blank page in Safari. Running on OSX Sierra.
Launch at Command line
if you didn't do it already, download Apache Maven 3.5.0 and add the /bin directory to the system PATH variable.
Enter the directory where the pom.xml file is and run the following:
mvn clean package
mvn tomcat7:deploy
mvn tomcat7:start
The first command will build the code and produce a Java WAR archive in the subdirectory target/.
The second command will deploy the WAR archive to Tomcat application server.
The third command will start Tomcat and expose the server as per configuration (localhost:8080).
Don't worry about the command tomcat7, even if you're using Tomcat 9 it should be fine.
Launch within IntellJ
Maven management in IntelliJ is not really optimal, but on the right side you should see a Maven Projects pane (if you don't, select "Tools -> View tool buttons" menu). Click and expand.
Under Lifecycle you will find the standard Maven goals, so double clicking on clean and then package will produce the WAR file in a similar manner as explained in the previous section.
Under Plugins you will find the Maven goals ruled by the Tomcat plugin. Expand and double click tomcat7:deploy and tomcat7:start.
A quick parallel with ASP.NET
If you come from .NET, just think of Tomcat as the Java version of IISExpress that comes bundled with VisualStudio. It is a application server where you deploy the application code and which emulates a webserver service for development.
There are many parallels and also many discrepancies. In general, an application lifecycle in .NET is much more contained, you rarely need anything else beyond VisualStudio and MSBuild, I think. In Java, many things have been created and evolved through the decades thanks to the community: we have different build systems (Maven, Ant, Gradle), the best tooling is command line and there are multiple ways to achieve the same goal.
For example, you are using the Jersey REST library with an underlying app server below (Tomcat), but you could also deploy to a NIO server (Grizzly). If you use extended frameworks like Spring, you generally don't even worry about these things because switching from one way to the other is just a matter of importing a module in your pom.xml instead of another.
Hope this helped you and gave you some directions.

ServletException: No servlet class has been specified

I have what should be an extremely simple "Hello World" servlet, but I cannot get it to work. I am using Eclipse, Tomcat 8, Java 7, and Servlet 3.1.
I have looked at many tutorials and questions, but they have not completely helped. Most tutorials I have seen talk about creating servlets by extending HttpServlet. I got those to work. Now I'd like to try the cleaner annotation approach.
I've been referring to this tutorial, but it isn't quite complete and seems to have some incorrect or incomplete examples:
Packaging and Deploying RESTful Web Services
Why is com.testing.service.MyApplication not being loaded?
Any help on getting this thing to run would be immensely appreciated!
Here are my files:
MyApplication.java
package com.testing.service;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("app")
public class MyApplication extends Application {
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(HelloWorldResource.class);
return s;
}
}
HelloWorldResource.java
package com.testing.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/helloworld")
public class HelloWorldResource {
#SuppressWarnings("unused")
private static final long serialVersionUID = 1L;
#GET
#Produces("text/plain")
public String sayHello() {
return "Hello World!";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>service</display-name>
<servlet>
<servlet-name>com.testing.service.MyApplication</servlet-name>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.testing.service.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testing</groupId>
<artifactId>service</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>Rest Test</name>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Whenever I run Tomcat, it displays the following error:
INFO: Marking servlet com.testing.service.MyApplication as unavailable
Feb 05, 2015 3:28:55 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /service threw load() exception
javax.servlet.ServletException: No servlet class has been specified for servlet com.testing.service.MyApplication
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1041)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4944)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5230)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
I found the answer in the comments of a related question. The answer was provided by Alvin Thompson. Unfortunately, I don't have enough reputation to up-vote your answer.
If you're using a standard Tomcat install (or some other servlet
container), you need to include a REST implementation since Tomcat
doesn't come with one. If you're using Maven, add this to the
dependencies section:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>2.13</version>
</dependency>
...
</dependencies>
Then just add an application config class to your project. If you
don't have any special configuration needs aside from setting the
context path for the rest services, the class can be empty. Once this
class is added, you don't need to configure anything in web.xml (or
have one at all).
Originally posted here:
How to set up JAX-RS Application using annotations only (no web.xml)?

Exception Deploying EJB Jar to Glassfish

This is my first foray into using JMS. I have a successfully created/deployed a war file that contains a servlet that I can use to upload files. When a file is uploaded it sends a message to a JMS queue. Next I wrote a listener to retrieve the uploaded messages from the queue, but when I try to deploy it, I get this error:
SEVERE: Invalid ejb jar [file-listener-ejb-1.0.jar]: it contains zero ejb.
Note:
1. A valid ejb jar requires at least one session, entity (1.x/2.x style), or message- driven bean.
2. EJB3+ entity beans (#Entity) are POJOs and please package them as library jar.
3. If the jar file contains valid EJBs which are annotated with EJB component level annotations (#Stateless, #Stateful, #MessageDriven, #Singleton), please check server.log to see whether the annotations were processed properly.
at com.sun.enterprise.deployment.util.EjbBundleValidator.accept(EjbBundleValidator.java:76)
...<snip>...
It's a very simple project with one class, built using Maven. The class looks like this:
package my.package;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
#MessageDriven(mappedName = "jms/FileUploadedQueue", activationConfig = {
#ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
#ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") })
public class FileListener implements MessageListener
{
private static Logger log = LoggerFactory.getLogger(FileListener.class);
public FileListener()
{
// empty constructor
}
public void onMessage(Message message)
{
try
{
log.info("Received message: " + ((TextMessage)message).getText());
}
catch (JMSException ex)
{
String error = "Received error code '"
+ ex.getErrorCode()
+ "' retrieving message from queue jms/FileUploadedQueue.";
Exception linkedEx = ex.getLinkedException();
if (linkedEx != null)
{
log.error(error += "Linked exception: ", linkedEx);
}
else
{
log.error(error, linkedEx);
}
}
}
}
My pom.xml looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.package</groupId>
<artifactId>uploaded-file-listener</artifactId>
<version>1.0</version>
<packaging>ejb</packaging>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<bootclasspath>${settings.localRepository}/javax/javaee-endorsed-api/6.0/javaee-endorsed-api-6.0.jar${path.separator}${sun.boot.class.path}</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>
This builds a jar file which when I try to deploy to my Glassfish 3.1 server (via the admin console) results in the above error.
Since I have the #MessageDriven annotation on my class, I'm not sure what I'm doing wrong. Unfortunately, the server.log file does not contain any more details about the error.
Should I be packaging the jar in an ear and deploying that?
----------EDIT----------
I created an ear which includes the ejb jar, and I get the same error when I deploy the ear to Glassfish. So, I think it must be something to do with the annotation. However, I've looked at multiple examples/tutorials and I can't see what's wrong.
Any insights/suggestions would be most welcome!!
----------EDIT TWO----------
Contents of MANIFEST.MF files:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: <name>
Build-Jdk: 1.6.0_24
Contents of application.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE application PUBLIC
"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN"
"http://java.sun.com/dtd/application_1_3.dtd">
<application>
<display-name>FileListener-ear</display-name>
<module>
<ejb>file-listener-ejb-1.0.jar</ejb>
</module>
</application>
----------EDIT THREE----------
Contents of ejb-jar file:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar 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/ejb-jar_3_1.xsd"
version="3.1">
<display-name>FileListener</display-name>
<enterprise-beans>
<message-driven>
<ejb-name>FileListener</ejb-name>
<ejb-class>my.package.FileListener</ejb-class>
<transaction-type>Container</transaction-type>
</message-driven>
</enterprise-beans>
</ejb-jar>
Does your jar file contain an ejb-jar.xml file? If it was missing, then it could explain why the whole thing explodes upon deploy
Deploying on GF 4.1 our EAR that consists of an EJB and WAR and several JAR projects suddenly needs an application.xml.
Many deploys before it was not necessary.
Did you try to check the compatibility check box while deployment and see if deploys fine. We also got such error preventing the deployment and was resolved by checking the compatibility check box while deployment.

Categories

Resources