import servlet API to eclipse project (javax.servlet cannot be resolved error)


Posted on : Wed, 15th Apr 15 01:56 pm (Updated on : Wed, 15th Apr 15 01:56 pm) UTC
Author : Code2care
100+ votes
The import javax.servlet can't be resolved

Import java.servlet cannot be resolved Error

If you receive the above error message in your java files (servlet classes) while working with eclipse IDE then you are most probably missing the API servlet-api.jar file that is needed by your project.



Advertisements





How to resolve javax.servlet error!

Add server-api as external jar file

Note : Alternatively, you just copy the servlet-api jar file to the lib folder of your project
File : servlet.java Language : Java

package com.code2care.org;


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

public class ServletError extends HttpServlet
{


  /**
   * HTTP doGet request.
   */
  public void doGet(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException
  {
   
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>Resolving Servlet Jar exception</title></head>");
    out.println("<body>");
    out.println("<p>Get : This works!.</p>");
    out.println("</body></html>");
    out.close();
  }

  /**
   * HTTP doPost request.
   */
  public void doPost(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException
  {
  

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>Resolving Servlet Jar Exception</title></head>");
    out.println("<body>");
    out.println("<p>Post : This works!.</p>");
    out.println("</body></html>");
    out.close();
  }
}

Code2care's mission is to share varied knowledge in technical and non-technical areas gathered during day-to-day learnings and development activities so that our visitors can leverage this portal to find solutions to their queries without re-inventing the wheel. Technical posts include Learnings, Tutorials, Video Tutorials, Code Snippets, Tips-n-tricks.

Follow us : Facebook - Twitter - Google +