0

I recently had to use Netbeans for my class and out of all my classmates, I am the only one with a servlet problem. I will put the codes of my index.html, web.xml, and my servlet class below. In addition, HTTP Status 500 error would always appear and the instructions online haven't helped my servlet. I'm using Tomcats 9.0, Netbeans 11.2. I also have both JDK 14 and 8 installed. I installed 8 cause of the "error:option -Xbootclasspath not allowed target 14. I hope someone sees this and can help me, thank you!

index.html

<html>
<head>
    <title>title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <div>TODO write content</div>
    <a href="ServletTest">click</a>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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_3_1.xsd">
<servlet>
    <servlet-name>ServletTest</servlet-name>
    <servlet-class>com.anothertest.ServletTest</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ServletTest</servlet-name>
    <url-pattern>/ServletTest</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
</web-app>

ServletTest.java

package com.anothertest;

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;

public class ServletTest extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try ( PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet ServletTest</title>");            
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet ServletTest at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}
andrewJames
  • 19,570
  • 8
  • 19
  • 51
  • What error is displayed in the NetBeans console when you run the application? Or, if you are running the program outside of NetBeans (i.e. directly in Tomcat), what error is logged to the catalina.out log file in Tomcat? Edit your question and add that information. – andrewJames Aug 19 '20 at 19:04
  • @andrewjames, i was able to fix my error! I uninstalled my JAVA 14 and made netbeans.conf point to JAVA 8. So my servlets work now!! :D Not sure why JAVA 14 made it have errors with servlets. But thanks anyways! – Carlos Miranda Aug 20 '20 at 05:38

0 Answers0