Having problems starting Tomcat from Eclipse?
Join the DZone community and get the full member experience.Join For Free
A very common problem Java developers face while working on web projects that need to be deployed in Tomcat is the error: 'Failed to Start...'
When we debug the application during development time and use Tomcat with Eclipse EE to deploy an application, the common error pops up below...
EclipseLink JPA Deployed on Tomcat 6 using Eclipse WTP. Tomcat 6 is not a Java EE 5 compliant server by design as it is a servlet container, however the servlet container is able to run EJB 3.0/JPA applications in application-managed Java SE (stand alone) mode. I'm using Eclipse 2019-03 and Apache Tomcat version 8.5.45. I have added the server in the eclipse. After running the server, It is giving me an error like this below. After you create a maven project in eclipse for spring application development, you always need to use maven to clean, build, install, and deploy the spring application to a tomcat server to see the result. This article will tell you how to make maven deploy to tomcat in eclipse project. Create Maven Project in STS (Spring Tool Suite). Eclipse JST Server Adapters (Apache Tomcat, JOnAS, J2EE) This is quick way to install Eclipse JST Server Adapters and JST Server Adapters Extentions (Apache Tomcat, JOnAS, J2EE) This entry was created when Eclipse IDE for Java Developers was promoted.
Server Tomcat v.xx Server at localhost failed to start.
This wastes several hours of developers' time. However, it's very easy to fix once you know the solution — it will also save you time. This error can be resolved in the following three cases:
1. Clean project and server
2. Remove .snap file from this directory
3. Remove temp file from this directory
Another similar problem is shown below. This, too, is a very common and frequent problem.
This is how you can fix the Eclipse Error: Starting Apache Tomcat at localhost has encountered a problem!
The error looks something like this:
Most of the time, this means that Tomcat is already running in the background and Eclipse is trying to open it again on the same ports. This is obviously not possible, as the ports will be in use.
The issue typically arises from either the Eclipse or Tomcat process crash or being stuck.
This is how you can go about fixing the issue:
1. Go to server tab and double click on the Tomcat server, the configuration file for Tomcat will open and look like this:
2. Mark down the Port Number Tomcat is running on.
3. In my case, I see ports: 8013, 8014, and 8015
4. Go to command prompt and execute the following lines in succession (make sure to change ending port numbers to your own Tomcat ports)
Once you do so, you’ll get a result similar to this:
As you can see in the above screenshot, in my case, port 8013 is not running, but ports 8014 and 8015 are being used and they are running using PID (process id): 15484.
This PID number (15484 in my case) is something you need to mark down.
5. Now, all you need to do is to go to your Windows Task Manager, click on Details Tab (in Windows 8), and sort the processes by PID number.
6. Find the Tomcat PID(s) that you found earlier, highlight the task (typically, it’ll be the Java process) and click “End Task Button”
7. Now, you can go back to Eclipse and run the task on your server as you would typically do. It should work again.
Let me know if this helped you in any way in the comments below. Thanks.
Opinions expressed by DZone contributors are their own.
Popular on DZone
- Written by Nam Ha Minh
- Last Updated on 11 March 2020 Print Email
- JDK 8
- Servlet 3.1
- JSP 2.3
- Eclipse 4.6 (Neon)
- Apache Tomcat 8
1. Creating a Java Dynamic Web ProjectIn Eclipse IDE, click menu File > New > Dynamic Web Project to create a project for Java web application. Name the project as HelloWorldJavaEE and keep the default settings for:
- Target Runtime: Apache Tomcat v8.0
- Dynamic web module version (Servlet version): 3.1
- Configuration: Default Configuration for Apache Tomcat v8.0
2. Adding Dependencies for Serlvet and JSP in Maven’s Project FileNow, we need to add Maven as a build manager for our project. To do so, right click on the project and select Configure > Convert to Maven project. The Create New POM dialog appears, enter the following information:Note that the Group Id will be used as the main package for our Java code. Now you see the pom.xmlfile created in the project. Open it in XML mode and put the following code between the <description>and <build> elements:You can see these are two dependencies for Servlet API 3.1.0 and JSP API 2.3.1. Save the pom.xmlfile and Maven will download the dependent JAR files in few seconds, as you can see under the Libraries section of the project:
3. Creating a JSP PageRight click on the project and select New > JSP File. Enter
Tomcat Server Tutorialindex.jspas the file name in the New JSP File dialog:Click Finish and you see Eclipse created the index.jspfile under the WebContentdirectory. Update it with the following code:As you can see, this page simply uses HTML code to display the text “Hello JSP and Servlet!”.
4. Testing the JSP Page
Eclipse Tomcat Server Failed To StartNow, let’s test the JSP page to see if the server works normally. If you haven’t added Tomcat server to Eclipse, follow this tutorial to add Apache Tomcat to the IDE.To deploy the project on Tomcat, simply drag the project from the Project Explorer view to Servers view, which results in the following screenshot:Right click on the server and select Start. Tomcat will be in Started state after few seconds and you see the last line in the Console view is something like this:Now we can test the JSP page either by:- Right click on the project, select Run As > Run on Server and click Finish in the Run on Server dialog. This will open an internal web browser inside the IDE like this:If you see the page displays “Hello JSP and Servlet!”, that means the web application is up and running successfully. Congratulations!- Another way is opening an external browser e.g. Google Chrome and enter the following URL (as shown in the internal web browser):
5. Update the JSP Page (Adding Form)Next, let’s update the JSP page to work with a Java servlet. The idea is adding a web form and on submit, the form will be processed by the servlet. Add the following code between the<body> tags of the page:This HTML code adds a form with a text field and a submit button to the page. The actionattribute of the form specifies the URL handles this form’s submission. In this case, we specify a path which is relative to the application and it points to a Java servlet which we will create in the next section.Save the file and refresh the browser, you will see:Try to enter something in the text field and click Call Servlet, we get a 404 error page like this:Don’t worry. This is because we haven’t created any Java servlet to handle this request. We will do so in the next section.
6. Creating a Java ServletBefore creating a servlet, let’s create a Java package named net.codejava.javaee by right click on the project, select New > Package. Enter that package name in the New Java Package dialog.Now click on the newly created package name and select New > Servlet, then enter HelloServlet as class name in the Create Servlet dialog:Click Next. In the next screen, edit the URL mapping from /HelloServlet to /helloServlet as shown in the following screenshot:NOTE:this URL mapping must match the value specified for the actionproperty of the form in the JSP page so the servlet can process request from the form.In the next screen, uncheck the option doGet in order to generate only doPost() method to handle the form:Click Finish to let Eclipse generates code for the HelloServletclass, as shown below:The@WebServlet annotation placed before the class declaration specifies that this class is a Java servlet which is responsible to handle requests in the form of /helloServlet.The doPost() method is where we put the code to process HTTP POST request sent to this servlet. This method accepts two parameters HttpServletRequestrepresents the request and HttpServletResponserepresents the response. These request and response objects are created and injected by the Servlet container (Tomcat).Now, put the following code inside the method doPost():This code simply retrieves value of the field yourNamefrom the form and writes a String to the response.
7. Testing the ServletSave the HelloServlet class and restart the server. Refresh the home page and enter your name in the form, for example:Click the button Call Serlvet and you see this response:Congratulations! Seeing this page means that the servlet has fulfilled the request and gave a response.So far we have walked you through the process of developing a ‘Hello world’ Java web application based on JSP and Servlet technologies using Eclipse IDE with Maven as the build system and Tomcat as the server. We hope you found this tutorial helpful, and thanks for reading.You can also follow this video tutorial (something might be different than the text in this article):
Other Java Servlet Tutorials:
About the Author:Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.
|[Java JSP and Servlet Hello World Eclipse project]||13 kB|