0

I am trying to get the values from a form in order to create an object with those values. How would I do this? Also currently when I hit submit I get the http 404 error. I know I'm doing this wrong, I just don't know how to fix it!

I have created the form with the following code:

<html lang="en">
    <head>
        <title>Example</title>
    </head>
    <body>
        <h1>Welcome to Loggy!</h1>
        
        <form id="logForm" action="LogsServlet" method="GET">
        <h3>What have you been up to today?</h3>
        <br>
        <label id="logTitleLabel" for="logTitle">Title :</label>
        <br>
        <input type="text" id="logTitle" name="logTitle">
        <br>
        <label id="logDescriptionLabel" for="LogDescription">Description</label>
        <br>
        <input type="text" id="logDescription" name="logDescription">
        <label id="logContentLabel" for="logContent">Content :</label>
        <br>
        <input type="text" id="logContent" name="logContent">
        <button type="submit" id="submitLog">Submit Log</button>
        
        </form>
    </body>
</html>

Here are the abstract Log Class and TextLog Class:

import java.sql.Timestamp;
import java.util.Date;
import java.util.UUID;

public abstract class Log {
    private UUID id;
    private String title;
    private String description;
    private String content;
    private Timestamp createTimestamp;
    
    
    //Constructor 
    Log(String title,String description, String content){
        this.setTitle(title);
        this.description=description;
        this.content=content;
    };
    

    public void create() {
        //call UUID method
        id();
        //create new timeStamp
        Date date= new Date();
        Timestamp createTimestamp = new Timestamp(date.getTime());
        this.createTimestamp=createTimestamp;
    }
    
    public UUID id() {
        UUID uuid = UUID.randomUUID();
        id = uuid;  
        return id;
    }

    
    //GETTERS AND SETTERS
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
       public UUID getId() {
            return id;
        }

        public void setId(UUID id) {
            this.id = id;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public Timestamp getCreateTimestamp() {
            return createTimestamp;
        }

        public void setCreateTimestamp(Timestamp createTimestamp) {
            this.createTimestamp = createTimestamp;
        }

  
}

public class TextLog extends Log {

    public TextLog(String title,String description, String content) {
        super(title,description, content);
        
    }

}

I am trying to get the values from the form and then create a Log object of type TextLog with the variables submitted in the form. Here is my Servlet:

import java.io.IOException;
import java.io.PrintWriter;

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



/**
 * Servlet implementation class LogsServlet
 */
//@WebServlet(description = "Loggy Logs", urlPatterns = { "/LogsServlet" })
public class LogsServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogsServlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //set content type
       response.setContentType("text/html");
       //get print writer
       PrintWriter writer = response.getWriter();
       //generate content
       
       request.getParameter("logTitle");
       request.getParameter("logDescription");
       request.getParameter("logContent");
       
      
    }
}

This is the first time I have worked with this, and I am quite lost of how to set it! I will also have to display the object in a list after and add it to a database if that makes a difference.

Any advice would be appreciated!

1 Answers1

0

This is a common issue on Tomcat 10 because the change from Java EE to Jakarta EE, so you can change your project, using Jakarta EE 9

<dependency>
        <groupId>jakarta.platform</groupId>
        <artifactId>jakarta.jakartaee-api</artifactId>
        <version>9.0.0</version>
        <scope>provided</scope>
    </dependency>

and importing this packages

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

If you don´t want do that, you can downgrade to Tomcat 9. Don´t forget uncomment

//@WebServlet(description = "Loggy Logs", urlPatterns = { "/LogsServlet" })

For related documentation follow this link

Tomcat 10.0.4 doesn't load servlets (@WebServlet classes) with 404 error

CaptainPyscho
  • 338
  • 1
  • 7
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 12 '22 at 10:39