0

It's me again :(

Now I crushed into some problems.

I am learning Servlet these day with IDEA.

I've written the code and configured Tomcat(version 10.0.10) like:

package yj.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(urlPatterns = {"/user"})
public class UserServlet extends HttpServlet {
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("来了个get请求");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("来了个post请求");
        doGet(req, resp);
    }
}

Tomcat Configuration 1

and

Tomcat Configuration 2

and to make sure, my Servlet version here

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
    </dependency>

also my web-app version is 3.1(which might mean @WebServlet() is allowed to use)

I think there should be something printed in tomcat's output, like:

Running tomcat

But this page shows up:

404 page

when I typed http://localhost:8089/user as address

so is there anything wrong with my servlet and how can I do this ..

Any help will be appreciated!

Project Structure is simple here.

Project Structure

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • It seems that Tomcat 9.0 works, but why?? – CsJuniorInNuist Dec 07 '21 at 15:49
  • For God's sake, I find this... ... 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.*. This will almost certainly require code changes to enable applications to migrate from Tomcat 9 and earlier to Tomcat 10 and later. A migration tool is under development to aid this process. .... ..... this probably means I should change name of packages..... – CsJuniorInNuist Dec 07 '21 at 15:52

2 Answers2

0

I think I've found this answer to the question.

instead of

import javax.servlet.ServletException;
// ... and so on

we should use

import jakarta.servlet.ServletException;
// ... and so on

that is because as for Tomcat (version>=10)

, there is a transfer of Java EE to the Eclipse Foundation

so the packages have to be renamed.

But this problem really confuses me for a while...

Thanks for your time and wish all of you a good day!

0

I'm starting to study servlets and stuff, I came across this problem after using webServlet(), The solution I found was to redo the project in another workspace in Eclipse EE, it seems kind of rough but it worked @~@