angelcool@fedora-laptop$date
Sat Nov 23 05:57:16 PM PST 2024
angelcool@fedora-laptop$
angelcool@fedora-laptop$
angelcool@fedora-laptop$java --version
openjdk 17.0.13 2024-10-15
OpenJDK Runtime Environment (Red_Hat-17.0.13.0.11-1) (build 17.0.13+11)
OpenJDK 64-Bit Server VM (Red_Hat-17.0.13.0.11-1) (build 17.0.13+11, mixed mode, sharing)
angelcool@fedora-laptop$jar --version
jar 17.0.13
angelcool@fedora-laptop$
angelcool@fedora-laptop$pwd
/home/angelcool/java-practice/HelloWorldServlet
angelcool@fedora-laptop$tree
.
├── deploy
│ └── helloworld.war
├── Notes.txt
├── src
│ ├── HelloWorld.class
│ └── HelloWorld.java
└── web
└── WEB-INF
├── classes
│ └── com
│ └── coolwriter
│ └── helloworld
│ └── HelloWorld.class
└── web.xml
9 directories, 6 files
angelcool@fedora-laptop$
angelcool@fedora-laptop$cat Notes.txt
# Compile java class, this will create HelloWorld.class
angelcool@fedora-laptop$javac --class-path /usr/share/tomcat/lib/tomcat-servlet-api.jar src/HelloWorld.java
# Copy compiled .class file to WEB-INF
angelcool@fedora-laptop$cp src/HelloWorld.class web/WEB-INF/classes/com/coolwriter/helloworld/
# Create WAR file
angelcool@fedora-laptop$jar cvf deploy/helloworld.war -C web .
# Move WAR file to Tomcat webapps directory
angelcool@fedora-laptop$sudo cp deploy/helloworld.war /usr/share/tomcat/webapps/
# Reload servlet in /manager page ( or restart Tomcat I think )
# Access servlet: http://localhost:8080/helloworld/Hello
angelcool@fedora-laptop$
angelcool@fedora-laptop$
angelcool@fedora-laptop$cat web/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web- ... ">
<display-name>Hello World!</display-name>
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>com.coolwriter.helloworld.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>
angelcool@fedora-laptop$
angelcool@fedora-laptop$
angelcool@fedora-laptop$cat src/HelloWorld.java
package com.coolwriter.helloworld;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class HelloWorld extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String msg = this.getGreeting();
out.println(msg);
}
private String getGreeting()
{
String msg = null;
int rand = (int) (Math.random() * (6)) +1;
switch (rand) {
case 1:
msg = "Hello There!";
break;
case 2:
msg = "Hello Friend!";
break;
case 3:
msg = "Hello Stranger!";
break;
case 4:
msg = "Hello! My name is Angel!";
break;
default:
msg = "Hello World!";
}
return msg;
}
}
angelcool@fedora-laptop$
angelcool@fedora-laptop$
[ view entry ] ( 82 views ) | print article
angelcool@fedora-laptop$date
Sat Nov 23 12:00:14 PM PST 2024
angelcool@fedora-laptop$
angelcool@fedora-laptop$
angelcool@fedora-laptop$cat /etc/redhat-release
Fedora release 39 (Thirty Nine)
angelcool@fedora-laptop$
angelcool@fedora-laptop$
angelcool@fedora-laptop$dnf info tomcat
Last metadata expiration check: 0:42:59 ago on Sat 23 Nov 2024 11:18:54 AM PST.
Installed Packages
Name : tomcat
Epoch : 1
Version : 9.0.96
Release : 1.fc39
Architecture : noarch
Size : 323 k
Source : tomcat-9.0.96-1.fc39.src.rpm
Repository : @System
From repo : updates
Summary : Apache Servlet/JSP Engine, RI for Servlet 4.0/JSP 2.3 API
URL : http://tomcat.apache.org/
License : ASL 2.0
Description : Tomcat is the servlet container that is used in the official Reference
: Implementation for the Java Servlet and JavaServer Pages technologies.
: The Java Servlet and JavaServer Pages specifications are developed by
: Sun under the Java Community Process.
:
: Tomcat is developed in an open and participatory environment and
: released under the Apache Software License version 2.0. Tomcat is intended
: to be a collaboration of the best-of-breed developers from around the world.
angelcool@fedora-laptop$
angelcool@fedora-laptop$sudo dnf install tomcat
...
angelcool@fedora-laptop$sudo dnf install tomcat-admin-webapps
...
angelcool@fedora-laptop$sudo dnf install tomcat-docs-webapp
...
angelcool@fedora-laptop$sudo systemctl start tomcat.service
...
Enable admin user, uncomment the following in /etc/tomcat/tomcat-users.xml
<role rolename="admin"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user name="admin" password="admin" roles="admin,manager,admin-gui,admin-script,manager-gui,manager-script,manager-jmx,manager-status" />
Restart server and go to: http://localhost:8080/manager/html
For reference:
angelcool@fedora-laptop$
angelcool@fedora-laptop$tree /usr/share/tomcat/webapps/
/usr/share/tomcat/webapps/
├── host-manager
│ ├── css
│ │ └── manager.css
│ ├── images
│ │ ├── asf-logo.svg
│ │ └── tomcat.svg
│ ├── index.jsp
│ ├── META-INF
│ │ └── context.xml
│ └── WEB-INF
│ ├── jsp
│ │ ├── 401.jsp
│ │ ├── 403.jsp
│ │ └── 404.jsp
│ ├── manager.xml
│ └── web.xml
└── manager
├── css
│ └── manager.css
├── images
│ ├── asf-logo.svg
│ └── tomcat.svg
├── index.jsp
├── META-INF
│ └── context.xml
├── status.xsd
├── WEB-INF
│ ├── jsp
│ │ ├── 401.jsp
│ │ ├── 403.jsp
│ │ ├── 404.jsp
│ │ ├── connectorCerts.jsp
│ │ ├── connectorCiphers.jsp
│ │ ├── connectorTrustedCerts.jsp
│ │ ├── sessionDetail.jsp
│ │ └── sessionsList.jsp
│ └── web.xml
└── xform.xsl
13 directories, 26 files
angelcool@fedora-laptop$
[ view entry ] ( 79 views ) | print article
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |