0

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!!!!!

Edited by mith_cool: n/a

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by ~s.o.s~
0

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().

Edited by stephen84s: n/a

0

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??

Edited by mith_cool: n/a

0

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.

Edited by stephen84s: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.