⛔️ The import javax.servlet can't be resolved

import java.servlet cannot be resolved error.png
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.
How to resolve javax.servlet error!- Make sure that you have installed Apache Tomcat Server and have configured it to run your Application.
- Right Click on your Project folder,
- Now click on Properties at the bottom
- Select Java Build Path on the right.
- Go to Library tab.
- Click on Add External Library.
- Now go to the location where Apache Tomcat has been installed (e.g. : c:\apache-tomcat-7.0.101).
- Now go to lib directory and Select servlet-api jar file.
- Apply Changes (Save).
- Just Clean + Build your project.
- The errors must now get resolved.

Right-Click-on-Project-Folder-and-Select-Properties.png

Add-servlet-api-jar.png
Note: Alternatively, you just copy the servlet-api jar file to the lib folder of your project
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("");
out.println("Resolving Servlet Jar exception ");
out.println("");
out.println("Get : This works!.
");
out.println("");
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("");
out.println("Resolving Servlet Jar Exception ");
out.println("");
out.println("Post : This works!.
");
out.println("");
out.close();
}
}
More Posts related to Java,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- How to Get the List of Shells on Linux - Linux
- Android Eclipse This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in - Android
- Android : Remove ListView Separator/divider programmatically or using xml property - Android
- Java JDBC Select Multiple Records from table as List using PreparedStatement - Java
- How to know installed version of Homebrew - MacOS
- Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED - Android
- Redirect page using jQuery - jQuery
- Copy entire directory using Terminal Command [Linux, Mac, Bash] - Linux