PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, June 25, 2022

[FIXED] Why do I get error when compiling java servlet?

 June 25, 2022     compiler-errors, java, servlets, tomcat     No comments   

Issue

I have created a java servlet named TestingServlet and get error when I try to compile file. I have set the classpath for servlet-api.jar and also tried including classpath when compiling:

javac -cp "C:\Program Files\Apache Software Foundation\Tomcat 10.0\lib\servlet-api.jar" TestingServlet.java

Code is :

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

public class TestingServlet extends HttpServlet {

 public void doGet(HttpServletRequest request, 
  HttpServletResponse response) 
  throws ServletException, IOException {
  
  PrintWriter out = response.getWriter();
  out.println("<HTML>");
  out.println("<HEAD>");
  out.println("<TITLE>Servlet Testing</TITLE>");
  out.println("</HEAD>");
  out.println("<BODY>");
  out.println("Welcome to the Servlet Testing Center");
  out.println("</BODY>");
  out.println("</HTML>");
 }
}

Error is:

TestingServlet.java:6: error: cannot find symbol
public class TestingServlet extends HttpServlet {
                                    ^
  symbol: class HttpServlet
TestingServlet.java:8: error: cannot find symbol
 public void doGet(HttpServletRequest request,
                   ^
  symbol:   class HttpServletRequest
  location: class TestingServlet
TestingServlet.java:9: error: cannot find symbol
  HttpServletResponse response)
  ^
  symbol:   class HttpServletResponse
  location: class TestingServlet
TestingServlet.java:10: error: cannot find symbol
  throws ServletException, IOException {
         ^
  symbol:   class ServletException
  location: class TestingServlet
TestingServlet.java:1: error: package javax.servlet does not exist
import javax.servlet.*;
^
TestingServlet.java:2: error: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
6 errors

Solution

Tomcat 10 Servlet API uses the new jakarta namespace, so the package names changed, as explained in the Tomcat 10 page:

Users of Tomcat 10 onwards should be aware that, as a result of the move from Java EE to Jakarta EE as part of the transfer of Java EE to the Eclipse Foundation, the primary package for all implemented APIs has changed from javax.* to jakarta.*.

So in your case

import jakarta.servlet.*
import jakarta.servlet.http.*


Answered By - Federico klez Culloca
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing