I'm using Eclipse to create some basic jsp files with the help of Tomcat, and I was able to run and render a jsp file (on Mozilla Firefox), but then I read that one also should have a "deployment descriptor" file (web.xml), in order to "tell the application container how the web app should be configured".
I don't remember creating one, and I checked to see if Eclipse created a default one for me but couldn't find in anywhere in my project files...
Do I have to provide it, or is it just optional?
if you don't use deployment descriptor you have to use annotations in your classes which are supported in servlet 3.0 and higher
No it's not optional it is required to have a deployment descriptor in your deployed project. Since the deployment descriptor tells the application container how the web app should be configured.
Related
I was going through java tutorials and found that after creating the servlet they have added the servlet.jar file using build path opt. But in my case servlet is still working without that external jar file.
So what is the need of that external jar file?
It's still working fine in your case because
Just check below
Right click on your project then go to Properties -> Targeted Runtime -> Apache tomcat
If you have selected that checkbox you no need to add servlet jar and if it's not then you need to add servlet jar in lib folder
So in your case you must have selected targeted runtime that's why even though u have not added servlet jar file it's working fine
You don't need to add because you may have added Apache Tomcat as server run time in for your web project and Apache tomcat has servlet.jar in its lib folder so it will be there
you can check it by going in Apache tomcat folder > lib
You need Servlet-api.jar to compile servlets in eclipse but while deploying servlet container ( like tomcat ) will have it built in. Infact it is bad practise to include it inside your WEB-INF/LIB folder.If you configure your eclipse to use libraries from tomcat(or other servlet containers) then you might not need it manually.
Servlet-api.jar is by default provided by the container environment. So you should not put it in your WEB-INF/LIB folder when you are deploying your application. However, it is required for compiling your Servlets. In case if you are using Eclipse as your IDE, place it in your classpath using Add External JAR files option.
Ensure that you're using at least Eclipse IDE for Java EE developers (with the EE). It contains development tools to create dynamic web projects and easily integrate servletcontainers (those tools are part of Web Tools Platform, WTP). You also need to ensure that you already have a servletcontainer installed which implements at least the same Servlet API version as the servletcontainer in the production environment, for example Apache Tomcat, Oracle GlassFish, JBoss AS/WildFly, etc.
You should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar, jsp-api.jar, el-api.jar, j2ee.jar, javaee.jar, etc. It would only lead to future portability, compatibility, classpath and maintainability troubles, because your webapp would not work when it's deployed to a servletcontainer of a different make/version than where those libraries are originally obtained from.
These links below will give you more information about this
How do I import the javax.servlet API in my Eclipse project?
Understanding who provides servlet-api.jar, is it web-container or part of Java EE download
http://wiki.metawerx.net/wiki/JARFilesYouShouldNeverIncludeInYourWebapp
I have a maven web application which has the file src/main/resources/appContext-ejb.xml file which has the ${version} placeholder in it.
I want that placeholder to be replaced by the version of the maven project, so I 've configured the maven resources plugin to enable filtering of the file.
After compiling with eclipse I can see the file under the target/classes directory with the correct (replaced value).
Also when packaging the war archive the file is being replaced without problems.
My problem is that I want to use the JBoss Tools eclipse plugin to start my web application, so I create a jboss server, and add my web application to it. When I start jboss (from within eclipse using jboss tools) and try to use my application the piece of code which needs that file with the replaced value fails telling that ${version} was not replaced.
Does JBoss Tools plugin use src/main/resources instead of the contents of target/classes?
How could I configure or make Jboss Tools use the already replaced file in target/classes?
I 've searched the web for this and I 've found a lot of information on how to enable filtering to web resources so that jboss tools takes changes on the fly, but I believe that that's not helpful for me because this file should not be used as a web resource because it shouldn't be served as web content.
How does IntelliJ automatically deploy the webapp in tomcat?
Just wondering as after the webapp has been deployed and run, there are no traces of the web app in the tomcat folder. (Not even the servlet classes' files).
Does it create those file and delete straight after execution?
Thanks in advance.
IntelliJ is deploying the exploded WAR which exists in your compilation target directory (for my project, it's target - it could be out for yours, depends on if you're using Maven or some other dependency management application), in the process explained at this Wiki link.
So, the artifact always exists in your target or out directory. If it didn't exist, then Tomcat would pitch a fit.
Is there a plugin for Eclipse GWT or any other method to automatically deploy and run GWT app on Tomcat (or any other Serlvet container)? For the moment the only method I know is copying the compiled classes into WEB-INF directory but this is an arduous work. Additionally you have to configure Apache Tomcat manually. I'd like to have something like in Eclipse Dynamic Web Project where you can run your servlets directly by spawning tomcat process.
Thanks
You could write an Ant task to deploy and run your GWT app. It could copy the files and then tell your servlet container to reload the latest files.
Is this what you are looking for: "How do I use my own server in hosted mode instead of GWT's built-in Jetty instance?"?
PS: You only need to copy the contents of the war folder once.
PPS: I'm assuming here you want to be able to easily deploy your app to Tomcat during development, since you are bringing up spawning Tomcat from Eclipse - meaning it's not a production server.
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.