I am a beginner to advance java and since am learning servlet am getting some weird error in my servlet. I am trying to get the method name and this is the code which i wrote down

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


public class NewServlet extends HttpServlet {

    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
    }
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           
        } finally { 
            out.close();
        }
    } 

   

   protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
   //     processRequest(request, response);
        response.setContentType("text/html");
        PrintWriter out= response.getWriter();
        out.println("<html><body>");
        out.println("your method was "+ request.getMethod());
        out.println("</body></html>");
    }

   

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       // processRequest(request, response);
        response.setContentType("text/html");
        PrintWriter out= response.getWriter();
        out.println("<html><body>");
        out.println("your method was "+ request.getMethod());
        out.println("</body></html>");
    }


    public String getServletInfo() {
        return "Short description";
    }

}

when i try to run this servlet am getting "Class newservlet neither have a main method nor it is the servlet specified in web.xml". Though i added this servlet named newservlet in web.xml but am continued with getting this error and i think so servlet do not have any main method. So please i need help!!!!!

Post the contents of your web.xml file, since the problem appears to be there.
Also I hope you have placed your class file (of the Servlet class), at the correct location (WEB-INF/classes or WEB-INF/lib[for jars]).

Another thing is remember to close your PrintWriter once you are done using it, like in processRequest().

I just added my servelt again to the web.xml this time and deployed it and it worked.. but somebody tell me why i was getting this error? and also what happens if we dont close PrintWriter object and why do we write this @Override??

but somebody tell me why i was getting this error?

You did not post the contents of your web.xml file, so unless and until someone has a crystal ball here which can look into your past, it cannot be answered. All that can be guessed is, you did not add your servlet correctly the first time around to your web.xml

@Override - is an annotation, a marker, If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.

As of Java 6, @Override annotation also works with interface methods implemented by classes.

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.