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

Member Avatar for diafol

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.