I'm a newbie for Java Development so sorry if questions seem dumb ;-)
For a webservices server behind a load balancer I need a default java application to show only a hostname. I asked AI however I get 404 messages on weblogic 12.2.1.4.
I expected a hostname when running the servlet.
AI came with this
Here's an example of a simple servlet in Java that retrieves the hostname of the server:
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.InetAddress;
@WebServlet("/hostname")
public class HostnameServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String hostname = InetAddress.getLocalHost().getHostName();
response.getWriter().write("Server Hostname: " + hostname);
}
}
This servlet defines a doGet method that handles GET requests. When a GET request is made to the "/hostname" URL pattern, the method retrieves the server's hostname using InetAddress.getLocalHost().getHostName() and writes it as the response.
To use this servlet, you need to have a Java web application project set up with a servlet container such as Apache Tomcat. The above code assumes you are using Java Servlet 3.0 annotations for servlet configuration.
After deploying the application to the servlet container, you can access the server hostname by making a GET request to the "/hostname" endpoint of your application. For example, if your application is running on http://localhost:8080, you can access the server hostname by visiting http://localhost:8080/hostname.
To create a WAR (Web ARchive) file for your servlet to deploy it on Apache Tomcat, you need to follow these steps:
Create a directory structure for your web application. Assuming your application is named "HostnameApp", create the following directories:
HostnameApp
└── WEB-INF
├── classes
└── web.xml
Place your servlet class file HostnameServlet.java in the classes directory. Your directory structure should now look like this:
HostnameApp
└── WEB-INF
├── classes
│ └── HostnameServlet.class
└── web.xml
Create a web.xml file in the WEB-INF directory with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>HostnameServlet</servlet-name>
<servlet-class>HostnameServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HostnameServlet</servlet-name>
<url-pattern>/hostname</url-pattern>
</servlet-mapping>
</web-app>
I compiled using
/usr/java/jdk1.8.0_361/bin/javac -cp /u01/app/oracle/product/12.2.1.4.0/mw_1/wlshome/oracle_common/modules/javax.servlet.javax.servlet-api.jar -d WEB-INF/classes src/HostnameServlet.java
And created a war file using /usr/java/jdk1.8.0_361/bin/jar -cvf HostnameApp.war HostnameApp/
This will create a HostnameApp.war file containing your web application.
Tried it however on weblogic 12.2.1.4 is see this in the console
And in Tomcat
What am I missing here?