How to create a servlet in an existing package of a project - java

I have a Client-Server (Client and server are seperate projects in eclipse) system setup in Eclipse(Mars) I now need to create a servlet in a new package on the client project.
when I am creating a class and trying to extend HttpServlet to it I am getting an error saying javax.servlet cannot be resolved.
When I am trying File->New->Servlet I am being able to create a new Project altogether but unable to create a servlet in the existing project.
How should I resolve the issue?

You need do set up the "Java Build Path" of your project.
In eclipse, right-click on you project, "Build Path / Configure Build Path"
and there, in "Libraries" tab, you need to add a server runtime, that should contain servlet-api.jar

Servlets are the Java platform technology of choice for extending and enhancing Web servers. Servlets provide a component-based, platform-independent method for building Web-based applications
See :
Java Servlet Technology
Java Servlets Tutorial
So when you try to create a servlet, it should be a separate project....its a web project.

Related

Cannot add application insights bare api to java web application

I added all the jar files of application insight sdk for java in java web project and included the code for sending custom telemetry in servlet but when i try to run the servlet it shows classnotfound error for TelemetryConfiguration and TelemetryClient.
Per my experience, I think you can try to check the jar files of Application Insight SDK for Java whether be added the Libraries of Java Build Path, please right click the prject name and select the Properties in the menu.
If using Maven project, you can see the jar files in the Maven Dependencies.
If using the normal Dynamic Web Project, you can see them in the Web App Libraries or a User Library with custom name.
Then you need to try to manually build the project, and run the servlet again.
It seems that you added only the web library without the core library, which contains the TelemetryClient class.
You can read the Application Insights Java SDK article which describes in details how to add the Java SDK to your web project, whether you're using Maven or Gradle as your build system, or manually downloading and referencing the libraries.

Can't find classes from imported java project in dynamic web project

I'm working on a Dynamic web project (REST service) which imports a java project I've made earlier. Now I'm trying by adding the project to the build path to use classes from the java project. While writing this it gives no Errors but on runtime the service gives the folowing error:
SEVERE: Servlet.service() for servlet [Jersey REST Service] in context with path [/pathname] threw exception [org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Controller/Generator] with root cause
java.lang.ClassNotFoundException: Controller.Generator
Can any one tell me what is the problem from the error and given context?
Otherwise i could upload the project for further debugging.
The project runs on Tomcat 7.0
Thanks in advance
Your issue is not compile time, rather runtime while class loading. Some quick points to debug...
If you are generating a WAR/ EAR from your web project, make sure your java project is added a .jar utility inside WEB-INF/lib in the installed application(folders may vary based on the web/ application server you are using).
If you are using an IDE for deployment(e.g. Eclipse), make sure your java projects are properly 'Project Referenced' by visiting the 'Property' tab of your web project. You can also use this java project as a shared library and made corresponding configurations from your server admin console and based on the server you are using, it may vary.
Hope this helps.
I'm suposing you are using ANT to compile your project (An eclipse ANT projet). Right-Click your project, select 'Deployment Assembly' and then Add option to select the project you want to make a reference.

Eclipse Web Service Client Project Type?

I created a basic Java project in Eclipse then added a Web Service Client using the File->New "Web Service Client" wizard.
This generated my stubs correctly and I am able to connect to the web service, however, I get the following error/warning when I run:
WARNING: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
I then tried to create the client using a Dynamic Web Project (instead of a basic Java project) and the error went away.
This is obviously due to the fact that the Dynamic Web Project includes libraries that are not included in a basic Java project.
My question is, why do I need to make a Dynamic Web Project (it seems like overkill) just to make a web service client? Is there some simpler project type you would recommend using?
Thank you.
Since it is just a warning, you could leave it as is. To make it go away, you could look up the jars that contain those classes and add them to your project.
I think we normally use a JavaEE project type, and that may bring in some extra library files. I wouldn't normally think you need a dynamic web project to make the error go away.
The only difference in the project type is the Libraries included automatically for you when you start them. You can start any project as a Java project and then in the Project Properties, Build Path, Libraries tab add additional libraries to the project.
Both of the missing classes above are in the j2ee.jar file, so if you can add that jar to your build path of the project it should go away.

Building a Java EE 6 project, with a swing client using Maven

I am trying to recreate an existing project with Maven (any IDE is ok, but mainly Netbeans), and I'm a bit confused about the best way to do this, so any help is greatly appreciated.
Currently I have an Enterprise application with the following components:
Web application (some jsps, servlets).
Ejb project
Client project (Swing application / applet)
Common project (Contains common files used by the applet and the Web app).
The problem is the packaging and dependencies, currently the Client (Applet) jar is packaged within the Web application, so that when the web app is deployed, the users can access the applet via their browser.
Is there any similar existing archetype for this, or does it require heavy customization ?
Oh, and I am using Glassfish 3.1
Thanks in advance.
So first of all, for me it looks like you need multi module maven project. In that case there will be no single archetype that will fulfill your needs.
When it comes to "the Client (Applet) jar is packaged within the Web application" you can use maven-dependency-plugin (http://maven.apache.org/plugins/maven-dependency-plugin/) and its goal:
dependency:copy - takes a list of artifacts defined in the plugin configuration section and copies them to a specified location, renaming them or stripping the version if desired. This goal can resolve the artifacts from remote repositories if they don't exist in local.

Java Web Project referencing another Java project

I have a Java Project, for which I'm now creating a Web interface, using a Dynamic Web Project from Eclipse. The Web project consists of a single servlet and two JSP's. Something like this:
/JavaApplication
/src
/lib
/resources
/WebApplication
/src
/Servlet.java
/WebContent
/WEB-INF
index.jsp
other.jsp
Now, I need to reference JavaApplication from WebApplication, in order to use its classes to process web requests. What's the best way to accomplish this ? My idea is to create a .jar of the JavaApplication, containing all the .class files, /resources, and /libs. In this way, I could include the .jar in the web application, and I could have a single .war file that contained the entire application.
What do you think? How is this problem typically solved ?
Note: I don't want to convert the Java Project into a Web project.
In Eclipse project properties, add the project to the Java EE Module Dependencies (Eclipse 3.5 or older)
or Deployment Assembly (Eclipse 3.6 or newer) entry in the project properties.
This way Eclipse will take care about doing the right thing to create a WAR out of this all (it will end in /WEB-INF/lib). No other configuration is necessary, even not some fiddling in Build Path.
Under Eclipse, you can declare Project References for a given project, the web application in your case. To do so, right click on your web application project, then go for Properties > Project References and select the JavaApplication project. This should allow you to call code from the JavaApplication project from the WebApplication without having to build a WAR. This is a solution for development.
For standard deployment (outside the IDE), you should indeed create a standard WAR. To do so, you'll have to package your JavaApplication as a JAR including the .class files and the files under /resources but not the libraries it depends on (JARs under /lib). These dependencies will actually end up in the WEB-INF/lib directory of the WAR, beside the JAR of your JavaApplication. These steps are typically automated with tools like Ant or Maven.
Connecting java app to web app for development :
right click on web project :
properties>project references> add the java project you want to refer
Now in properties tab of web project go to
properties>deployment assembly> add the project manually and run the app
Consider moving up to EAR level, if your web container supports that.
The tricky part with shared code is where should the common code be put. A copy pr web application? A copy in the web container? Overdoing the "share these classes" might end up in class loader problems.
If you are creating two separate web applications refactor common java code into a separate Eclipse project and refer to it from both WAR projects.
EDIT: Apparently I have misread the problem description, and thought you asked about an existing and a new web application sharing code.
If you have an Eclipse project with your application, and another with your web frontend, then you can let your application export the necessary resources which the "Export WAR" in Eclipse Java EE can wrap up in a jar file and put in WEB-INF/lib for you. You need to say this explicitly with a checkmark in Properties -> Java EE Module Dependencies for your web project. Expect you have to experiment a bit - this took me a while to learn.
Typically you would create an API interface using remote service beans from the Java application that expose the methods that you want to invoke in the web application. You would include a proxy of the API interface with your web application that calls the remote service bean in the Java application. Remember that you will need to register the remote bean in the web.xml file.

Categories

Resources