Java tomcat交互问题

问题遇到的现象和发生背景

JavaWeb问题,利用浏览器上的表单和tomcat交互,并填充信息到数据库;

用代码块功能插入代码,请勿粘贴截图
public class AddServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String fname = req.getParameter("fname");
        String price = req.getParameter("price");
        String fcount = req.getParameter("fcount");
        String remark = req.getParameter("remark");
        Fruit fruit = new Fruit(0, Integer.parseInt(price), Integer.parseInt(fcount), fname, remark);
        FruitDAOImpl fruitDAO = new FruitDAOImpl(); ** //debug在这里走不下去了。**
        boolean isFlag = fruitDAO.add(fruit);
        System.out.println(isFlag ? "添加成功" : "添加失败");
    }
}

web.xml


<web-app 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_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>addServletservlet-name>
        <servlet-class>com.sunrain.servlets.AddServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>addServletservlet-name>
        <url-pattern>/addurl-pattern>
    servlet-mapping>
web-app>

html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
<form action="add" method="post">
    品种:<input type="text" name="fname"><br>
    价格:<input type="text" name="price"><br>
    数量:<input type="text" name="fcount"><br>
    备注:<input type="text" name="remark"><br>
    <input type="submit" value="提交">
    <input type="reset" value="重置">
form>
body>
html>

运行结果及报错内容
HTTP Status 500 - Servlet execution threw an exception
type Exception report

message Servlet execution threw an exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NoClassDefFoundError: com/sunrain/dao/FruitDAOImpl
    com.sunrain.servlets.AddServlet.doPost(AddServlet.java:28)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.ClassNotFoundException: com.sunrain.dao.FruitDAOImpl
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
    com.sunrain.servlets.AddServlet.doPost(AddServlet.java:28)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.42 logs.

Apache Tomcat/8.0.42

我的解答思路和尝试过的方法

我测试直接在java层面通过fruitDAOImpl直接添加数据到数据库发现没有问题;
也尝试过重新配置deployment和artifacts还是不行;

我想要达到的结果

可以通过浏览器添加数据到数据库



保存不是两个impl没找到么,FruitDAOImpl里边写的什么内容呢,如果有 interface FruitDAO 的话 改成 下边试试

        FruitDAO fruitDAO = new FruitDAOImpl(); 

_debug展示_
从表单拿到数据了

img


但下一步就报错了

img

  • 关于该问题,我找了一篇非常好的博客,你可以看看是否有帮助,链接:Java启动Tomcat