Where's javax.servlet? - java

I have jdk1.6.0_13 installed, but when I try to find a javax.servlet package, or press Ctrl+Space in Eclipse after Servlet I cannot get anything. Where can I download this package, and why isn't it included in standard distribution for developers?

javax.servlet is a package that's part of Java EE (Java Enterprise Edition). You've got the JDK for Java SE (Java Standard Edition).
You could use the Java EE SDK for example.
Alternatively simple servlet containers such as Apache Tomcat also come with this API (look for servlet-api.jar).

A bit more detail to Joachim Sauer's answer:
On Ubuntu at least, the metapackage tomcat6 depends on metapackage tomcat6-common (and others), which depends on metapackage libtomcat6-java, which depends on package libservlet2.5-java (and others). It contains, among others, the files /usr/share/java/servlet-api-2.5.jar and /usr/share/java/jsp-api-2.1.jar, which are the servlet and JSP libraries you need. So if you've installed Tomcat 6 through apt-get or the Ubuntu Software Centre, you already have the libraries; all that's left is to get Tomcat to use them in your project.
Place libraries /usr/share/java/servlet-api-2.5.jar and /usr/share/java/jsp-api-2.1.jar on the class path like this:
For all projects, by configuring Eclipse by selecting Window ->
Preferences -> Java -> Installed JREs, then selecting the JRE you're
using, pressing Edit, then pressing Add External JARs, and then by
selecting the files from the locations given above.
For just one
project, by right-clicking on the project in the Project Explorer
pane, then selecting Properties -> Java Build Path, and then pressing
Add External JARs, and then by selecting the files from the locations
given above.
Further note 1: These are the correct versions of those libraries for use with Tomcat 6; for the other Tomcat versions, see the table on page http://tomcat.apache.org/whichversion.html, though I would suppose each Tomcat version includes the versions of these libraries that are appropriate for it.
Further note 2: Package libservlet2.5-java's description (dpkg-query -s libservlet2.5-java) says: 'Apache Tomcat implements the Java Servlet and the JavaServer Pages (JSP) specifications from Sun Microsystems, and provides a "pure Java" HTTP web server environment for Java code to run. This package contains the Java Servlet and JSP library.'

Have you instaled the J2EE? If you installed just de standard (J2SE) it won´t find.

The normal procedure with Eclipse and Java EE webapplications is to install a servlet container (Tomcat, Jetty, etc) or application server (Glassfish (which is bundled in the "Sun Java EE" download), JBoss AS, WebSphere, Weblogic, etc) and integrate it in Eclipse using a (builtin) plugin in the Servers view.
During the creation wizard of a new Dynamic Web Project, you can then pick the integrated server from the list. If you happen to have an existing Dynamic Web Project without a server or want to change the associated one, then you need to modify it in the Targeted Rutimes section of the project's properties.
Either way, Eclipse will automatically place the necessary server-specific libraries in the project's classpath (buildpath).
You should absolutely in no way extract and copy server-specific libraries into /WEB-INF/lib or even worse the JRE/lib yourself, to "fix" the compilation errors in Eclipse. It would make your webapplication tied to a specific server and thus completely unportable.

If you've got the Java EE JDK with Glassfish, it's in glassfish3/glassfish/modules/javax.servlet-api.jar.

those classes are usually part of servlet.jar
http://www.java2s.com/Code/Jar/wsit/Downloadservletjar.htm

Related

Convert a .war file into a right version

I have to compile a Java EE project with JBoss 7. When I run with JBoss 7, Netbeans creates a .war file. Due to problems with my environnment, I have to deploy this .war file with Tomcat6 on a virtual machine.
The problem is that the .war created is not compatible with tomcat6.
I tried to install tomcat7 on the virtual machine, but I have other problems like "NoClassDefFoundError".
So I'm looking for a way to convert the .war in the right version. How should I do please ?
There is no standard way to achieve this goal ("convert a .war file"), as Tomcat6 (out of the box) isn't a JEE certified application server - just a web container:
Apache Tomcat, a Servlet and Java Server Pages container
Thus, certain libraries that are part of a true JEE-application server are not bundled and present in the /lib folder of a plain Tomcat installation, for instance implementations for JTA, JPA, CDI, JAX-RS, JAX-WS...
However, you could try and download an enhanced Tomcat, namely TomEE, as it is - in it's latest release - JEE6 certified (and has several variants):
Apache TomEE, pronounced "Tommy", is an all-Apache Java EE 6 Web Profile certified stack where Apache Tomcat is top dog.
This might be a good solution to your migration approach as JBoss7 is also compliant to JEE6 level.
After you have downloaded TomEE you could then try to deploy your application in there. Nevertheless, you should have provided the full stacktrace or at least the Class (incl. fully qualified package) that is not found, as there might be certain third-party libraries in JBoss 7 that are not present in TomEE or other application servers.
Hope it helps.
War Files itself don't have a Version.
The Java Classes them selfs have a Java-Version (e.g. if they using Java 7 or Java 8).
If your Tomcat runs on Java 6 and you compiled with Java 7 you'd get a UnsupportedClassVersionError. If thats the case, you have to either update the Java-Version of Tomcat or compile your Code with Java 6 (and remove Java7 Features).
Other than that, it might be possible, that you're using Features from JBoss 7 that aren't available in Tomcat 6 (e.g you referring to Servlet 3.0 in your App, but Tomcat doesn't support that version). To make it work in this case, you'd have to remove all the Features, that Tomcat doesn't support.

Downloading and adding javax.enterprise.context.RequestScoped to my project

I've got a project running 1.7. I'm getting issues using javax.enterprise.context.RequestScoped. I'm confused as to why it isn't included as I have other javax.* libraries built into Netbeans by default.
Where can I download this library and how can I add it to my project?
By default, only the packages that are part of the Java Standard Edition are available. This library belongs to Java EE. Just go to project->properties->libraries->add library->Java EE Web 7 Api Library->add. That´s it.
It's part of the Java EE Specification. You should start your project within a container. In Netbeans use an Enterprise Application project type.
Try this:
You can import this jar file into your project.
Use cdi-api.jar
I found mine in glassfish folder
.\glassfish-4.1\glassfish\modules

classpath question, head first servlets book

I'm studying via Head First's Servlets and JSPs, and doing the assignment on the third chapter.
On page 81, there's this javac call:
> %javac -classpath /Users/bert/Applications2/tomcat/common/lib/
servlet-api.jar:classes:. -d classes src/com/example/web/BeerSelect.java
I don't get what it's trying to do. The book says that everything on the path before common should be suited for my specific system, but I don't have a common/lib path (not that I know of). I'm working on ubuntu 9 and I have created the following directory structures for deployment and developing of the web app that the chapter talks about, neither of them has a common/lib/ path on them. Where can I find this on my machine?
Which edition of the book are you using? The /common/lib is true for until with Tomcat 5.5 (Servlet 2.4). Since Tomcat 6.x (Servlet 2.5) and on the Servlet API libraries are available in /lib folder.
If you trace the rest of the path, you'll see that common/lib belongs to Tomcat, the engine on which you'll deploy and run your servlet when it's compiled and packaged.
I'm assuming that you've downloaded and installed Tomcat.
Since you said the book called for "common/lib", that tells me that the examples are based on Tomcat 5.5. Tomcat 6.x, the latest version, uses "lib" instead of "common/lib".
The point is that you need the servlet API JAR in order to compile successfully.
If you installed a packaged version of tomcat, it should be:
/usr/share/tomcat5.5/common/lib/servlet-api.jar for Tomcat 5.5
/usr/share/tomcat6/lib/servlet-api.jar for Tomcat 6
But my recommendation would be to get a binary distribution of Tomcat from the official website (get the version of the book if you want to stick to the book) and to uncompress it in your home folder (I put this kind of stuff in ~/opt)

Enabling full documentation for Java EE in eclipse

I'm new to Eclipse and am using it currently to play with Java EE.
When using Ctrl+Space for types/functions from the regular Java libraries I get a full description (i.e. general description of the type, what are the arguments of the method for, etc.). However I don't get the same for Java EE types.
For example, when using Ctrl+Space on methods of the HttpSession class I get only names like "arg0" or "obj" and no description.
How can I remedy this?
I'd suggest downloading the javadocs and adding them in eclipse. (right click project > properties > java build path > libraries > expand the library > set Javadoc location)
I think that's why Java EE defines a specification only, the implementation must then be done by the provider of your application server. javax.servlet.http.HttpSession e.g. is bundled in servlet.jar.
It you are using Tomcat as your application server, you could attach the java sources to your servlet.jar and then you can browse the source and read it's javadoc.
The source for the latest tomcat is here ->
http://www.apache.org/dist/tomcat/tomcat-6/v6.0.26/src/apache-tomcat-6.0.26-src.zip
I wanted to add what might be very pertinent information for projects using maven and eclipse / wtp. What I did to get the Javadocs to attach nicely was the following 2 steps.
1) I am using glassfish so using the glassfish plugin site I installed the java ee 6 (or 7) documentation. You can easily view this documentation with Help->Java EE 6 docs. I copied the URL from here.
2) Opened my projects build path and went to the libraries tab. Opened the Maven Dependencies and found the java ee 6 api entry (this should be part of the maven project). Opened this up and was able to add a location for the javadocs for the api. I pasted the file:/// url I had copied.
Then I verified this worked by using the ctrl + shift plus mouse pointer hover over some of the java ee 6 classes and methods.
I hope this information will be useful to other who searched and got here but didn't find the solution they were looking for (or for those who now use Maven!).

Missing javax.servlet.http.Cookie class

I haven't been using Java extensively hence forget a lot fundamental things. Basically, I am working with a web-dynamic project (using Eclipse IDE with jdk 1.6), and during the build, I get errors that Cookie class and other web related classes cannot be found. What am I missing?
What Jars files do I need to add (and how)?
thanks
Think you need servlet-api.jar
Try here for any jar files you need http://www.java2s.com/Code/Jar/CatalogJar.htm
Sounds like you are missing servlet-api.jar
You can download it from the Maven repository.
Servlets are only part of the Enterprise Edition, not of the Standard Edition of Java. So you need the Java Enterprise Edition, AKA Java EE.
http://java.sun.com/javaee/
As others have said, you're missing the server api jar.
Having said that, downloading just the servlet api jar and not the server that goes with it means that you can't actually test your code.
In Eclipse, once you're installed a servlet container, such as Sun's Glassfish, Apache Tomcat, or Jetty, you can create a server instance of it so that Eclipse will compile against your server's servlet implementation. Creating a server instance also means that Eclipse's Run on Server command works.
IBM has a slightly outdated guide on Developing Web applications with Tomcat and Eclipse. The entire "Install the Web tools" section can be skipped if you have the Eclipse EE version, as Web Tools are already installed.

Categories

Resources