Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Servlet interface is expected here

Tags:

java

servlets

I am absolutely new to Java.

I am creating a Servlet and getting the error interface is expected here.

Can you please make me understand the problem(s)?

I am using IntelliJ 14.

My Servlet code is as follows:-

package ERPdetector;  

/** 
 * Created by Sinha on 12/15/14. 
 */  

import javax.servlet.http.*;  
import javax.servlet.*;  
import java.io.*;  

public class ErpServlet implements HttpServlet {  

    public void doGet(HttpServletRequest req,HttpServletResponse res)  
            throws ServletException,IOException  
    {  
        res.setContentType("text/html");  
        PrintWriter pw=res.getWriter();  

        String dropdown=req.getParameter("dropdown");  
        pw.println("You Requested for "+dropdown);  

        pw.close();  
    }  
}  

Thanks in Advance.

like image 320
Indzi Avatar asked Dec 15 '14 09:12

Indzi


People also ask

Is HTTP servlet an interface?

HttpServlet is an abstract class, not an interface - Servlet is an interface, but you rarely implement that directly.

What is HttpServletRequest and HttpServletResponse in Java?

The HttpServletRequest object can be used to retrieve incoming HTTP request headers and form data. The HttpServletResponse object can be used to set the HTTP response headers (e.g., content-type) and the response message body.

What is HTTP servlet request and response?

The servlet container of a web service is responsible for the creation of a ServletRequest object. The servlet container wraps all data that came from a client to a web server as a request. Also, the servlet container creates a corresponding ServletResponse object, that will be filled with data in a servlet.

How do I define the URL pattern which when requested from a browser will invoke my servlet class?

You can declare multiple servlets using the same class with different initialization parameters. The name for each servlet must be unique across the deployment descriptor. The <servlet-mapping> element specifies a URL pattern and the name of a declared servlet to use for requests whose URL matches the pattern.


1 Answers

HttpServlet is an abstract class, not an interface - Servlet is an interface, but you rarely implement that directly.

Just change this:

public class ErpServlet implements HttpServlet { 

to

public class ErpServlet extends HttpServlet { 

and you should be fine.

like image 181
Jon Skeet Avatar answered Oct 22 '22 00:10

Jon Skeet