Issue
What I'm trying to do is making a simple web app where a teacher may take students' scores from excel files to the database
I have tried cleaning and rebuilding the project, cleaning tomcat workpath but none seemed to work
Here is my code of the servlet that is triggering the error
@WebServlet(name = "NhapFileExcelServlet", urlPatterns = {"/nhapFileExcelServlet"})
public class NhapFileExcelServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ParseException {
Part filePart = request.getPart("excel");
InputStream inputStream = filePart.getInputStream();
String fileName = filePart.getSubmittedFileName();
if(fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {
// Lấy file excel upload về server
String uploadFileDirectory = "\\Users\\thanhhieu\\Desktop\\test" + fileName;
File fileToSave = new File(uploadFileDirectory);
Files.copy(inputStream, fileToSave.toPath(), StandardCopyOption.REPLACE_EXISTING);
request.setAttribute("isValid", Boolean.TRUE);
ExcelController excelController = new ExcelController(uploadFileDirectory);
excelController.readExcel();
ArrayList<SinhVien> dsSinhVien = excelController.getDsSinhVien();
ArrayList<Diem> dsDiem = excelController.getDsDiem();
request.setAttribute("dsSinhVien", dsSinhVien);
request.setAttribute("dsDiem", dsDiem);
}
else {
request.setAttribute("isValid", Boolean.FALSE);
}
//Chuyển request và response sang GDdiem.jsp
String url = "/GDdiem.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
System.out.println("Ma lop gui sang servlet: " + request.getParameter("maLop"));
dispatcher.forward(request, response);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, FileNotFoundException {
try {
processRequest(request, response);
} catch (ParseException ex) {
Logger.getLogger(NhapFileExcelServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, FileNotFoundException {
try {
processRequest(request, response);
} catch (ParseException ex) {
Logger.getLogger(NhapFileExcelServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
The part that would call the servlet
<div style = "margin: auto">
<form action="nhapFileExcelServlet" method = "post" enctype="multipart/form-data">
<input type="hidden" id="lopID" name="maLop" value= <%= maLop %> />
<input type="hidden" id="monID" name="maMon" value= <%= maMon %> />
<label for="fileID">Chọn file Excel danh sách điểm:</label>
<input type="file" id="fileID" accept=".xls, .xlsx" name="excel"><br>
<button type="submit">Nhập</button>
</form>
</div>
My xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>NhapFileExcelServlet</servlet-name>
<servlet-class>servlet.NhapFileExcelServlet</servlet-class>
<multipart-config/>
</servlet>
<servlet>
<servlet-name>TinhDiemServlet</servlet-name>
<servlet-class>servlet.TinhDiemServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LuuDiemLopServlet</servlet-name>
<servlet-class>servlet.LuuDiemLopServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NhapFileExcelServlet</servlet-name>
<url-pattern>/nhapFileExcelServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>TinhDiemServlet</servlet-name>
<url-pattern>/tinhDiemServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LuuDiemLopServlet</servlet-name>
<url-pattern>/luuDiemLopServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>
</web-app>
This is the stacktrace
jakarta.servlet.ServletException: Error instantiating servlet class [servlet.NhapFileExcelServlet]
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:870)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1696)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:835)
cause mère
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet
java.base/java.lang.ClassLoader.defineClass1(Native Method)
java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2470)
org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:866)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1370)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1224)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:870)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1696)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:835)
cause mère
java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1401)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1224)
java.base/java.lang.ClassLoader.defineClass1(Native Method)
java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2470)
org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:866)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1370)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1224)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:870)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1696)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:835)
Any tip is appreciated
Solution
NVM I got it going
Turns out, not only importing the jar files but I also need to specify the dependency in pom.xml because the IDE doesn't do it automatically
Answered By - Long Doan
Answer Checked By - Gilberto Lyons (JavaFixing Admin)