Answer the question
In order to leave comments, you need to log in
How to run a servlet in Tomect without an IDE?
Good evening. Started learning Java servlets Downloaded tomcat in webapps folder created helloworld
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>");
}
}
<?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>
Answer the question
In order to leave comments, you need to log in
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
package sample;
ваш код
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 questionAsk a Question
731 491 924 answers to any question