how to add the servlet api to my pom.xml - java

How do I add the servlets API to my project's pom.xml
mvnrepository.com has lots of servlet api and similarly named projects, that I don't know which is the right one. Or are all of them ok?

I believe most web/app servers come bundled with a version of the servlet api, so you won't want to bundle the api in your .war file. You will need to find out which version is included with your server, then you can use
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api-version}</version>
<scope>provided</scope>
</dependency>
replacing servlet-api-version with your version. You will want to specify the "provided" scope so the api.jar isn't included in your war file.

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

For servlet-api 3.1.0, here is the declaration :
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

We use
<dependency>
<groupId>javax</groupId>
<artifactId>j2ee</artifactId>
<version>1.4</version>
<scope>provided</scope>
</dependency>
but if you only need the servlet api you might want to use
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>?</version>
<scope>provided</scope>
</dependency>

It depends on which version of the servlet API you are using.
The javax.servlet artifact will provide jars for all of the servlet API versions.

Scope provided can be used when you dont want to put jar file inside the WEB-INF/lib folder instead you are supplying it at runtime either by container or JDK.

Jakarta EE
In recent years, Oracle transferred the Java EE technologies to the Eclipse Foundation. There the technologies have been renamed to Jakarta EE. So Java Servlet is now known as Jakarta Servlet.
This name change was done to respect Oracle’s trademarks. Do a Web search to find many articles and videos discussing this transition.
This name change includes changing the package naming of the classes from javax.* to jakarta.*. This is a breaking change, though updating your project may be as simple as merely changing your import statements. But check that any libraries you depend on have versions available using the new naming as well.
Servlet 5
This transition has brought new versions of the Servlet specification. Version 5 of the spec is the same as Servlet 4 but with the new naming.
For the current version, edit your POM file to use the following Maven dependency. Check for updates in a Maven repository of your choice in the version numbering.
You can deploy web apps built with Servlet 5 to web containers such as Tomcat 10.0.x, Jetty 11.0.x, Glassfish 6, and several more.
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
Servlet 6
Jakarta Servlet 6 specification is currently in development, and will contain significant changes. The spec will be finalized later this year 2022.
See the overview page, the product page, project links page, and repository coordinates page.
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>

Related

Which Jakarta-light-web-server is to implement jax-ws and JMS specification?

I have an application in which the following modules are:
1- Jax-ws
2- JMS and its implementation by Activemq with the following dependency:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>2.4.0</version>
</dependency>
3- the version of JDK is 1.8
4- Its application server is Wildfly
I want to change and upgrade my application to JDK 17(LTS), due to this migration the javax package is replaced with jakarta package.
My question is:
1- What except Wildfly is which supports Jakarta-ws and jakarta-jms? because Wildfly is so heavy for my solution.
2- Before for JMS configuration, the following dependency were:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.2</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>2.4.0</version>
</dependency>
after migration to JDK-17, which dependency should to add to implement jakarta-jms by activemq
Check out OpenLiberty, it is fully open source and supports Java 17 and Jakarta EE 8 and 9. Moreover its fully composable, so you can pick and choose what features (APIs) you need in the server.
WildFly can be easily reduced to your usage since you can easily disable what you don't need. You may want to take a look at Galleon as a way to provision your server tailored to your need.

Bouncycastle dependency conflicts

I have a web application created using spring boot. I have added jasper report, iText and bouncycastle maven dependency. Jasper and iText both contain bouncycastle libraries and now because of this the web application is not working correctly.
Error is: java.security.NoSuchProviderException: JCE cannot authenticate the provider BC. Note that I already added this code: Security.addProvider(new BouncyCastleProvider());
This perfectly works using spring boot embedded tomcat but not when exporting to a war file running on a wildfly server.
Here is how I declare the pom.
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.58</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.4.0</version>
<exclusions>
<exclusion>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk14</artifactId>
</exclusion>
<exclusion>
<groupId>org.bouncycastle</groupId>
<artifactId>bcmail-jdk14</artifactId>
</exclusion>
</exclusions>
</dependency>
Upon creating war file, this are the list of libraries included:
bcmail-jdk14-138
bcprov-jdk14-138
bcpkix-jdk15on is not being included even I specify it as provided
To quote directly from the Maven docs
provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
To paraphrase, it uses the .jar marked as "provided" to compile (and test) your software, but when you package it up, it will not be included in the .war: you are expecting the runtime system to provide a (presumably different) version of those classes.
Try removing changing the scope of that dependency to "compile" to see if that resolves your problem.

Replace glassfish javax.servlet.jsp in jetty

The default jsp dependencies changed from glasfish to apache in jetty version 9.2.
http://www.eclipse.org/jetty/documentation/current/configuring-jsp.html
I have tried to replace all jsp dependencies in a project to apache but I have not succeeded to replace javax.servlet.jsp to a working apache dependency:
groupId: org.glassfish.web
artifactId: javax.servlet.jsp
version: 2.3.2
So are there any alternative dependency which I should use instead or is org.glassfish.web:javax.servlet still the best dependency to use when packaging a runnable war?
The exception thrown when removing the javax.servlet.jsp dependency is:
java.lang.ClassNotFoundException: org.apache.jasper.servlet.JspServlet
You only need the api libraries on the build time classpath. These should be marked with scope 'provided' which indicates that they should not be bundled in any generated artifact (WAR) as they will be provided by the Servlet container at runtime.
Thus in your pom you should have the following:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
In the absence of any other container specific behaviour, the above change makes your application portable across all containers compliant with the relevant version of the Servlet specification.

What are the Maven coordinates for the JCache API jar?

I have an executable JAR (not a web app being deployed to a standard container like Tomcat, etc.) that needs to connect to a DB and cache a lot of stuff.
If at all possible, I'd like to use the JCache API and inject (via Guice) the Hazelcast implementation. I will code 100% against the JCache API, so that if I ever change my mind and choose another implementation, the swap out should be (relatively) painless.
One problem: where can I get the latest stable JCache JAR?!? (What are the Maven coordinates?)
On Maven Central, all I can find are some dev JARs from 2005! So I ask: where is the JCache?
The jar are available here : javax.cache/cache-api
If you want to use Ehcache implementations, copy/past the dependencies below:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.1.1</version>
<scope>runtime</scope>
</dependency>
Find a complete example here.

Where is javax.annotation

Ok, so this is probably a NooB question (I'm more of a C++ guy), but I'm lost in the java woods and its frameworks forests...
I'm trying to look into eclipse RCP development. For that I'm following this well-known tutorial:
http://www.vogella.com/tutorials/EclipseRCP/article.html
At step 15 I need to add the following dependency packages to import in my bundle.
javax.annotation
javax.injection
The problem is that I cannot select these (they are not in the selection list)
I do have javax.el javax.servlet.* and javax.xml.*
Looking at
http://docs.oracle.com/javase/7/docs/api/overview-summary.html
suggests that this should be part of the standard java.
What obvious mistake am I missing?
The dependency including version:
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
See: http://mvnrepository.com/artifact/javax.annotation/javax.annotation-api
Or for the newer jakarta.annotation:
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>1.3.5</version>
</dependency>
See: https://mvnrepository.com/artifact/jakarta.annotation/jakarta.annotation-api
The Java Common Annotations Module java.xml.ws.annotation was deprecated in Java version 9 and was removed in java version 11. If this leads to a problem you could try to add javax.annotation.
The Javadocs for Java 8 can be found here:
http://docs.oracle.com/javase/8/docs/api/javax/annotation/package-summary.html
Your comment indicates this is for Guava, so you want the JSR305 library, which extends the javax package.
2020 Update: Note that this library breaks the Oracle licensing agreement and Guava has since moved to checkerframework's Nullable annotation.
Not sure if this is still relevant, but for Java 8, I had to add the two following Maven dependencies in order to get javax.annotation.concurrent.ThreadSafe to work:
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.0</version>
</dependency>
Javax annotations include in this dependency. This is latest version at the moment.
For maven
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
For gradle
compile('com.google.code.findbugs:jsr305:3.0.2')
In Java version >= 6, you should not need to add them explicitly.
They are part of the JDK. Just try to skip adding them, maybe
the list of instructions is outdated.
Before Java 6, you would have needed to add this jar, I think: jsr250-api-1.0.jar.
http://central.maven.org/maven2/javax/annotation/jsr250-api/1.0/
http://download.java.net/maven/2/javax/annotation/jsr250-api/1.0/

Categories