@
@
@atambalasi2019-11-24 16:32:25
Java
@atambalasi, 2019-11-24 16:32:25

How to run a servlet in Tomect without an IDE?

Good evening. Started learning Java servlets Downloaded tomcat in webapps folder created helloworld
5dda85050d5e1680879886.png
folder Created classes/HelloWorld.java class in web-inf folder and compiled via cmd javac -cp ../../lib/servlet-api.jar HelloWorld.java
here is the content file

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Hello world!
 *
 */
public class HelloWorld extends HttpServlet
{
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter printWriter = resp.getWriter();

        printWriter.print("<h1> Apple </h1>");
    }
    
}


The contents of the web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <servlet>
        <servlet-name>mainServlet</servlet-name>
        <servlet-class>HelloWorld</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>mainServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>


I run localhost:8080/helloworld the html index starts I write localhost:8080/helloworld/hello not found

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergey, 2019-11-24
kuzmin @sergueik

atambalasi thanks
for the puzzle...
the structure should be quite a bit different

/opt/tomcat/webapps/helloworld/WEB-INF/classes/sample/HelloWorld.class
/opt/tomcat/webapps/helloworld/WEB-INF/web.xml
/opt/tomcat/webapps/helloworld/WEB-INF/sample/HelloWorld.java

(I copied your code and added
package sample;
ваш код

(and in web.xml naturally - DI is DI )
and checked
javac -cp /opt/tomcat/lib/servlet-api.jar sample/HelloWorld.java ; 
mkdir -p classes/sample/ ; 
mv sample/HelloWorld.class classes/sample/
/opt/tomcat/bin/catalina.sh stop;
/opt/tomcat/bin/catalina.sh start

catalina.out
org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/tomcat/webapps/helloworld] has finished in [51] ms

<h1> Apple </h1>r

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question