I am using Eclipse IDE for Java Developers Helios. I have mainly done desktop applications before but now I would like to learn about Servlets. I have installed Jetty on my computer. And I wrote a simple Servlet in Java using Eclipse. But how do I compile it and export it to a war file in Eclipse? I have found some tutorials doing it with Ant, but I would like to do it native in Eclipse if possible.
Here is my Servlet:
package org.jonas;
// some imports from java.io, java.servlet and java.servlet.http
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
out.println(
"<html><body>" +
"<h1>" + name + "</h1>" +
"</body></html>");
}
}
How can I compile it and export it as a war file in Eclipse? Without Ant or Maven. So I can deploy it in Jetty.
To create war file, you need to use jar tool of JDK. You need to use -c switch of jar, to create the war file. Go inside the project directory of your project (outside the WEB-INF), then write the following command: jar -cvf projectname.
A WAR file can be created with the jar command, included in the Java runtime environment, or a ZIP utility program such as WinZip . To create a WAR file, you first need to create the file structure as directories in the filesystem and place all files in the correct location as described earlier.
Eclipse does not put anything anywhere itself. When you build the project using maven, according to maven folder structure it will build and put the war file in its target directory where the project is present in file system (mostly inside your workspace), which you can see on your eclipse package/project explorer.
Edit: As @nos has inferred, the OP was using "Eclipse IDE for Java Developers" and not "Eclipse IDE for Java EE Developers". The below is only relevant for the latter.
Assuming you created this as a Dynamic Web project in Eclipse, just
right-click on the
project name, > Export > WAR file
and fill in the details it asks for.
If you didnt create this as Dynamic Web Project, you can convert your static web project into one first
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With