Issue
package com.luv3code.testdb;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestDBServlet
*/
@WebServlet("/TestDbServlet")
public class TestDBServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String user="springstudent";
String pass="springstudent";
String jdbcUrl= "jdbc:mysql://localhost:3306/web_customer_tracker?useSSL&serverTimezome=UTC";
String driver= "com.mysql.cj.jdbc.Driver";
try {
PrintWriter out=response.getWriter();
out.println("Connect to Database: " +jdbcUrl);
Class.forName(driver);
Connection myConn= DriverManager.getConnection(jdbcUrl, user, pass);
out.println("suceess");
}catch(Exception exc) {
exc.printStackTrace();
throw new ServletException(exc);
}
response.getWriter().append("Served at: ").append(request.getContextPath());
}
}
I create a connection springstudent: with user spring student. Besides create a database with table on my workbench.
I would like to connect my eclipse project on my database. So I write the code and add a library.
My steps:
Download apache tomcat
Create web-dynamic project. Create a servlet Add library my-sql-8.0.12
Java compiler, Java built path, Project Facest have the same version
I try to run doing both of the above settings: Server Right Click priorities : workspace metadata Server Right Click priorities : local
Delete and add the server(Window->Show Servers...) Check project facets->runtime apache clicked Clean project Clean server
Delete and import project again.
console:
Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: Server version name: Apache Tomcat/9.0.35 Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: Server built: May 5 2020 20:36:20 UTC Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: Server version number: 9.0.35.0 Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: OS Name:
Windows 10 Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: OS Version: 10.0 Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: Architecture: amd64 Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: Java Home: C:\Program Files\Java\jdk1.8.0_251\jre Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Version: 1.8.0_251-b08 Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: JVM Vendor: Oracle Corporation Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_BASE:
C:\Users\User\eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1 Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: CATALINA_HOME: C:\Program Files\Apache Software Foundation\Tomcat 9.0 Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.base=C:\Users\User\eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1 Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 9.0 Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dwtp.deploy=C:\Users\User\eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 9.0\endorsed Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.startup.VersionLoggerListener log INFO: Command line argument: -Dfile.encoding=Cp1253 Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: Loaded Apache Tomcat Native library [1.2.24] using APR version [1.7.0]. Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true] Ιουν 14, 2020 1:57:58 ΠΜ org.apache.catalina.core.AprLifecycleListener initializeSSL INFO: OpenSSL successfully initialized [OpenSSL 1.1.1g 21 Apr 2020] Ιουν 14, 2020 1:57:59 ΠΜ org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-nio-8081"] Ιουν 14, 2020 1:58:00 ΠΜ org.apache.catalina.startup.Catalina load INFO: Server initialization in [2,379] milliseconds Ιουν 14, 2020 1:58:00 ΠΜ org.apache.catalina.core.StandardService startInternal INFO: Starting service [Catalina] Ιουν 14, 2020 1:58:00 ΠΜ org.apache.catalina.core.StandardEngine startInternal INFO: Starting Servlet engine: [Apache Tomcat/9.0.35] Ιουν 14, 2020 1:58:02 ΠΜ org.apache.jasper.servlet.TldScanner scanJars INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. Ιουν 14, 2020 1:58:03 ΠΜ org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["http-nio-8081"] Ιουν 14, 2020 1:58:03 ΠΜ org.apache.catalina.startup.Catalina start INFO: Server startup in [2,449] millisecondsHTTP Status 404 – Not Found
Type Status Report
Message The requested resource [/web-customer-tracker/] is not available
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
I have two PC, in the first one run the program if change @WebServlet("/") I cannot undresand why. In this it doesn't run.
I also reinstall eclipse and tomcat. Eclipse 2019 Apache 9 java version "1.8.0_251" Java(TM) SE Runtime Environment (build 1.8.0_251-b08) Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
Could someone help me?
Solution
Simply right-click on your servlet and choose Run As > Run on Server
.
Answered By - Arvind Kumar Avinash