java.lang.ClassNotFoundException: javax.servlet.xxxx occurs

I use tomcat10 and eclipse2022-6

Using eclipse to start servers reports an error java.lang.ClassNotFoundException: javax.servlet.xxxx

I also read the answers of many bloggers, using various methods but still can't solve the problem, until I saw this big guy's post

http://t.csdn.cn/9xcDv, only to know that tomcat10 has no javax package. It is precisely because tomcat10 does not have a javax package, so there will be an error that javax cannot be photographed. This is also a problem I found during my study, I hope it can help everyone.

solution:

Change the javax in the import package to jakarta .

(The code is used to make up the number of words, you can skip it, thank you for your understanding)

package com.woniu.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;



public class HelloServlet extends HttpServlet{
	@Override
	//重写service方法
	public void service(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
        //配置UTF-8字符集以支持中文
		response.setContentType("text/html;charst=utf-8");
		response.setCharacterEncoding("utf-8");
        //获取输出流以输出数据到浏览器
		PrintWriter pw =  response.getWriter();
		pw.write("我是servlet服务端发送给浏览器的数据:Hello Servlet!");
		pw.close();
	}
}

Guess you like

Origin blog.csdn.net/weixin_63571132/article/details/126788317