I am trying to create an HTML form which displays text from a text file, using a Java Servlet. I keep getting the following error: Error instantiating servlet class servlets.ReportServlet.

Here is the HTML page:

<html>
<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <form name = "managementReports" method = "get" action = 
"ReportServlet">
    <input type="submit">
    </form>
</body>
</html>

And here is my servlet:

package servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.annotation.WebServlet;
@WebServlet(urlPatterns = { "/ReportServlet" })
public class ReportServlet extends HttpServlet   
{
public void service(HttpServletRequest req, HttpServletResponse res) throws 
ServletException, IOException  
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();

String name = req.getParameter("filename");

BufferedReader br = new BufferedReader(new 
FileReader("c:/Consignment.txt"));

String str;
while( (str = br.readLine()) != null )
{
 pw.println(str + "<BR>");
}

br.close();
pw.close();
}
}

Here is my web.xml:

<?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_3_1.xsd"
 version="3.1">
<servlet>
    <servlet-name>CustomerServlet</servlet-name>
    <servlet-class>servlet.CustomerServlet</servlet-class>
</servlet>
<servlet>
    <servlet-name>ConsignmentServlet</servlet-name>
    <servlet-class>servlet.ConsignmentServlet</servlet-class>
</servlet>
<servlet>
    <servlet-name>ReportServlet</servlet-name>
    <servlet-class>servlet.ReportServlet</servlet-class>
</servlet>
session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
</web-app>

Any suggestions would be appreciated

Dani AI

Generated

A few focused debugging points and a small example to resolve the "Error instantiating servlet class servlets.ReportServlet."

As observed, mixing annotation-based and XML-based declarations can hide deployment problems; echoed that. The usual root causes in this thread are (1) a package/name mismatch between the servlet class and the name used in web.xml, (2) a malformed web.xml (invalid tags or bad XML), or (3) a duplicate/conflicting declaration (both annotation and web.xml). Server logs will show the exact root exception (ClassNotFoundException, NoClassDefFoundError, or an XML parse error) and are the best first source of truth. 's pointer to examples is useful once the root cause is identified.

Concrete fixes and safer file access:

  • Make the servlet-class value in web.xml match the servlet package exactly, and validate web.xml with an XML validator.
  • Either remove the @WebServlet annotation or the web.xml servlet entry (not both), so only one configuration source is used.
  • Avoid absolute OS paths. Package the text file under WEB-INF and read it via the servlet context.

Example (read a file from WEB-INF safely, using doGet and try-with-resources):

package servlets;

import java.io.*;
import java.nio.charset.StandardCharsets;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;

@WebServlet("/ReportServlet")
public class ReportServlet extends HttpServlet {
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    resp.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = resp.getWriter();
         InputStream in = getServletContext().getResourceAsStream("/WEB-INF/Consignment.txt")) {
      if (in == null) { out.println("<p>File not found</p>"); return; }
      try (BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
        String line;
        while ((line = br.readLine()) != null) out.println(line + "<br/>");
      }
    }
  }
}

Quick checklist: correct package names, validate web.xml, confirm WEB-INF/classes layout, check server logs for the full stack trace, and prefer context resource access over hard-coded disk paths.

Recommended Answers

All 4 Replies

Member Avatar for Member #120589

Any suggestions would be appreciated

Post to the correct forum. This is the community forum. Doh!

I'm not sure if it's the root cause of your problem, but I don't think mixing xml based and annotation based configuration of your web application is a good idea.
I wouldn't be at all surprised if the servlet engine ignores the annotations if an xml based configuration is found.

I go with jwenting!!

XML based and annotation based configuration of your web application is not a good idea!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.