Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Very dummy question on servlet

Eduardo FreitasJun 27 2024

First of all, let me tell that I never did any program of Java, but now I need to a very simple test on a Weblogic 14C that was installed to use Java 11

So, as I'm not a Java programer at all I asked ChatGPT to create one for me, and the code generated was

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/current-time")
public class CurrentTimeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

   // Get current server time  
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
   Date now = new Date();  
   String currentTime = sdf.format(now);  
     
   // Write HTML response  
   out.println("\<html>");  
   out.println("\<head>\<title>Current Server Time\</title>\</head>");  
   out.println("\<body>");  
   out.println("\<h1>Current Server Time:\</h1>");  
   out.println("\<p>" + currentTime + "\</p>");  
   out.println("\</body>");  
   out.println("\</html>");  
     
   out.close();  

}

}

The instruction to generate the .war was:

javac -cp $WEBLOGIC_HOME/server/lib/weblogic.jar CurrentTimeServlet.java

jar cvf CurrentTimeApp.war CurrentTimeServlet.class

and deploy to weblogic, which I did, however when I call http://myserver:port/CurrentTimeApp/current-time I'm getting error 404 page not found

Here is the big thing: I know that by default people use web.xml and weblogic.xml as configuration file, however according to ChatGpt with the new Java the web.xml is not necessary anymore (even still can be used for compatibility purpose),

What I want to do is make the program run WITHOUT creating the web.xml … seems the parte of the code where it says @WebServlet("/current-time") , should make the web.xml uncessary

What Am I missing?

Comments

Post Details

Added on Jun 27 2024
1 comment
169 views