I am learning how to create a servlet that takes HTML code and makes a program in my browser. This code was supposed to output a simple welcome message in my browser via HTML. However, My eclipse IDE does not recognize any of the HTML commands in this code. It says that the imports are not accessible and that the HTTP variables can't be resolved to a type. I'm using tomcat version 10.0.12 and Java 13 for the code (I also have java 16 but eclipse only gave me the option to use 13). I've tried uninstalling and reinstalling Tomcat several times and adding servlet-api.jar but none of those changed a thing. I know there's an extra step I'm missing but I can't figure out what it is?
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;
public class WelcomeServlet extends HttpServlet {
@Override
protected void doGet( HttpServletRequest request,
HttpServletResponse response ) throws ServletException, IOException {
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
out.println( "<title>Welcome to Servlets!</title>" );
{...}
out.println( "</html>" );
out.close(); // close stream to complete the page
}
}
CodePudding user response:
You've messed up your imports. It sounds like you're following a tutorial that's 20 years old, a lot of what you're doing is extremely outdated. The web is a fairly fast moving environment; I strenously advise against using such old tutorials.
The correct import is import javax.servlet.http.HttpServletRequest;
.
Some notes on what you're doing that are bad ideas:
Star imports aren't advised; it's a bit too easy to get confused about where things live or which type(s) you are fetching from where. Your IDE manages your imports for you; let it.
You don't want to write HTML inside strings inside your code. Use a templating engine such as Freemarker, Velocity, Thymeleaf, or Google Closure Templates. Alternatively, write static HTML with a ton of javascript (probably with a client-side javascript-based framework) and write your server as an API that doesn't 'answer' in terms of HTML files; it answers in terms of some structured data format such as JSON. Your static (as in, unchanging; any file-serving HTTP service can provide them) HTML CSS Javascript does the job of calling your API to get this structured data, and then your HTML CSS JavaScript does the job of rendering it into HTML.
Raw servlets is outdated; the API is extremely old and it shows, for example, you can't even use a simple for-each loop to iterate over all parameter names, because the API returns the obsolete Enumeration instead of the more modern Iterator or even Stream. You neither get the benefit of storing intermediate state in fields (because you aren't guaranteed one instance per invocation), but you also don't get to just dump it all in static fields either (as the spec doesn't guarantee that there'll only ever be one instance) - the worst of both worlds. Look into JAX-RS, Jersey, Dropwizard, sparkjava, or other such frameworks.
CodePudding user response:
tl;dr
Your project lacks a JAR containing the definition of the Jakarta Servlet API (a set of interfaces).
Add jakarta.servlet-api
as a dependency to your project, to present to your IDE the needed JAR file during development but omitted from your final build’s WAR file.
Details
Your import
statements refer to the Jakarta Servlet API. That API is not built into Java. So you must make a Servlet API JAR file available to your IDE while developing.
Typically in Java development we use a tool such as Maven or Gradle to assist our IDE in obtaining external dependencies such as the Servlet API JAR.
If you are using Maven, edit your POM file to specify a dependency for the Servlet API JAR published as part of the Jakarta.ee project, by the Eclipse Foundation.
<!-- https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
Notice the scope
element, with a value of provided
. This means the JAR file should be downloaded, made available to your IDE while developing, but should not be copied into your resulting product when you build.
The output of your build will typically be a WAR file (not JAR). The provided
scope means we do not want a copy of the Servlet API JAR within that WAR. The reason is that when developing Jakarta EE web apps, we expect to deploy to a compliant application server that carries its own copy of the Servlet API. Every “profile” of Jakarta EE compliant servers are required to include an implementation of the Servlet API.
So your POM will look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>work.basil.example</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>demo</name>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</build>
</project>
And here is the source code for a simple servlet.
package work.basil.example.demo;
import java.io.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
@WebServlet ( name = "helloServlet", value = "/hello-servlet" )
public class HelloServlet extends HttpServlet
{
private String message;
public void init ( )
{
message = "Hello World!";
}
public void doGet ( HttpServletRequest request , HttpServletResponse response ) throws IOException
{
response.setContentType( "text/html" );
// Hello
PrintWriter out = response.getWriter();
out.println( "<html><body>" );
out.println( "<h1>" message "</h1>" );
out.println( "</body></html>" );
}
public void destroy ( )
{
}
}
You said:
I've tried uninstalling and reinstalling Tomcat several times
Completely unrelated to your problem, I expect.
The installation of Tomcat comes with an implementation of the Servlet API. But your IDE does not “see” Tomcat during development. Your IDE needs to see the interfaces defined in the Servlet API.
Your IDE does not actually execute your servlet, so it does not need an implementation, it needs only the interfaces. When you run your Servlet from the IDE, Tomcat is invoked externally from the IDE. The IDE attaches a debugger connection to Tomcat to facilitate interactive debugging, but the IDE and Tomcat are actually running as separate distinct processes on the host OS. And as I said, Tomcat comes bundled with its own implementation of those interfaces, so your servlet is able to execute.
You said you are using Java 13. Be aware that Java 13 is no longer supported. You should move to either the latest version of Java (17, as of 2021-11), or to one of the Long-Term Support (LTS) versions (8, 11, 17).
If using Java 11 or 17 rather than 8, I would suggest using Tomcat 10.1.x rather than Tomcat 10.0.x. Tomcat 10.1.x supports Jakarta EE 9.1 and 10, which supports Java 11 and later versus Java 8.
See the Tomcat version comparison page.