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$
Comments
Comments are not available for this entry.