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>

I have been searching google for days but I am still struggling with this

Re: How to read from a text file into an html page using Java Servlet? 80 80

Just so others know, there are replies at https://www.daniweb.com/community-center/threads/509357/how-to-read-from-a-text-file-into-an-html-page-using-java-servlet

So this must be so the post is in the right forum but I fear you are ignoring prior discussions on the subject.

Re: How to read from a text file into an html page using Java Servlet? 80 80

I have marked that question as resolved and put it onto the right forum. I followed the link you sent me but I am still getting nowhere with it.

Re: How to read from a text file into an html page using Java Servlet? 80 80

@R I hear you clearly but to work this you either use priors or if you want to start from scratch, break the goal into manageable chunks.

In the 62 lines above, install what some call debug statements so you can see where your code breaks down.

Added with edit.

Just a thought, did not test it out. I worry about reading files in Java. I've lost count of how many times the file path is incorrect or permissions didn't allow the file to be read.

Re: How to read from a text file into an html page using Java Servlet? 80 80

@rproffitt I had a go at that and this is what comes up:

java.lang.ClassNotFoundException: servlets.ReportServlet
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:520)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:501)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1050)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:779)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:789)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Re: How to read from a text file into an html page using Java Servlet? 80 80

There are tutorials about that error such as https://dzone.com/articles/how-resolve-2

Sorry if I can't solve this for you as I don't have your server to replicate the issue. It does sound as if it's well discussed over the years.

Re: How to read from a text file into an html page using Java Servlet? 80 80

I will keep researching, thanks anyway

Re: How to read from a text file into an html page using Java Servlet? 80 80

I am feeling a lot of deja vu here...

You should not use both annotation based and xml based web application configuration in the same web application.
Choose one or the other and stick with it.

Be a part of the DaniWeb community

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