Java WebServlet 无法解析为类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19447620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:15:05  来源:igfitidea点击:

WebServlet cannot be resolved to a type

javajspservlets

提问by MDP

I was able to make my app works again, following the advices of user2821894, but after trying to call a servlet tomcat 7 stopped working again!! If i try to delete the code where i call my servlet my web app doesent' work!! Once i have a problem with a servlet tomcat stops working.

按照 user2821894 的建议,我能够使我的应用程序再次运行,但是在尝试调用 servlet tomcat 7 后,它再次停止运行!如果我尝试删除我调用我的 servlet 的代码,我的网络应用程序不会工作!!一旦我遇到 servlet tomcat 的问题就停止工作。

I had problem launching my web project on eclipse. I had problem with Tomcat 7. So i 'delete' tomcat 7 from eclipse and then i added it again (again tomcat 7).

我在 eclipse 上启动我的 web 项目时遇到问题。我在使用 Tomcat 7 时遇到了问题。所以我从 eclipse 中“删除”了 tomcat 7,然后我再次添加了它(再次添加了 tomcat 7)。

now i have no problem launching my web project, but i have problem on my servlet. For example i get error like

现在我启动我的 web 项目没有问题,但是我的 servlet 有问题。例如我得到错误

 WebServlet cannot be resolved to a type
 The attribute value is undefined for the annotation type 

I added servlet-api 3.0.jar to my project but i still have these problems.

我将 servlet-api 3.0.jar 添加到我的项目中,但我仍然有这些问题。

This is the code of my servlet

这是我的 servlet 的代码

 package Jeans;
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;

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

    import com.sun.java.swing.plaf.windows.TMSchema.Part;


    import javax.servlet.http.Part;



    @WebServlet("/FileUploadDBServlet ")

  //// i got an error here//////////////////////////// 
    @MultipartConfig(maxFileSize = 16177215)  

    public class FileUploadDBServlet extends HttpServlet {


        private String dbURL = "db";
        private String dbUser = "dbuser";
        private String dbPass = "dbpassword";
        String messageMio = "da contorllare";  
        GestioneDB gestioneDB;
        boolean connessione;       
        Connection conn;

        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {




         String giorno= request.getParameter("giorno");
         String mese= request.getParameter("mese");
         String anno= request.getParameter("anno");         
         String dataFormatoItaliano = giorno + "-" + mese + "-" + anno;     

          String titolo = request.getParameter("titolo");
          String titoletto = request.getParameter("titoletto");
          String testoMouse = request.getParameter("testoMouse");
          String link = request.getParameter("link");
          String data = dataFormatoItaliano;
          String testo = request.getParameter("testo");




          //// i got an error here////////////////////////////
          Part filePart = request.getPart("immaginePrincipale");  






 String didascaliaImmaginePrincipale = request.getParameter("didascaliaImmaginePrincipale");       


            InputStream immaginePrincipale = null; 
            if (filePart != null) {             
                // obtains input stream of the upload file
                immaginePrincipale = filePart.getInputStream();
            }





            String message = null;  

            try {

                  gestioneDB = new GestioneDB(); 
                  conn = gestioneDB.cn();  



                  gestioneDB.inserimentoNews(titolo, titoletto, testoMouse, link, testo, data, immaginePrincipale, didascaliaImmaginePrincipale);



                String sql = "INSERT INTO allegati_news (allegato,didascalia,tipo,id_news,immagine) values (?,?,?,?,?)";




                PreparedStatement statement = conn.prepareStatement(sql);
                statement.setString(1, "firstName");
                statement.setString(2, "lastName");
                statement.setInt(3, 1);
                statement.setInt(4,1);            
                if (immaginePrincipale != null) {              
                    statement.setBlob(5, immaginePrincipale);             
                }




                int row = statement.executeUpdate();
                if (row > 0) {
                    message = "File salvato nel db";
                }
            } catch (SQLException ex) {
                message = "ERROR: " + ex.getMessage();
                ex.printStackTrace();
            } finally {
                if (conn != null) {

                    try {
                        conn.close();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }

               request.setAttribute("Message", gestioneDB.getInserimentoNewMessaggio());



               getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
            }
        }
    }

This is my web.xml file

这是我的 web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Jeans2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>prova</display-name>
    <servlet-name>prova</servlet-name>
    <servlet-class>Jeans.prova</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>prova</servlet-name>
    <url-pattern>/prova</url-pattern>
  </servlet-mapping>
  <servlet>

    <description></description>
    <display-name>FileUploadDBServlet</display-name>
    <servlet-name>FileUploadDBServlet</servlet-name>
    <servlet-class>Jeans.FileUploadDBServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>FileUploadDBServlet</servlet-name>
    <url-pattern>/FileUploadDBServlet</url-pattern>
  </servlet-mapping>

   <servlet>
    <description></description>
    <display-name>BlobDisplay</display-name>
    <servlet-name>BlobDisplay</servlet-name>
    <servlet-class>Jeans.BlobDisplay</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>BlobDisplay</servlet-name>
    <url-pattern>/BlobDisplay</url-pattern>
  </servlet-mapping>


</web-app>

采纳答案by user2821894

Try adding servlet-api.jar instead of servelt-api-3.0 jar.Stop the server. Refresh the project and then start the server and see. I think it should work. Make sure you are adding the servlet-api.jar from tomcat lib folder. Suppose your tomcat is in C:\Tomcat\lib. In eclipse right click your project-properties-javabuildpath-add external jars and then select the servlet-api.jar from your tomcat folder

尝试添加 servlet-api.jar 而不是 servelt-api-3.0 jar。停止服务器。刷新项目然后启动服务器看看。我认为它应该工作。确保从 tomcat lib 文件夹中添加 servlet-api.jar。假设您的 tomcat 在 C:\Tomcat\lib 中。在 Eclipse 中右键单击您的 project-properties-javabuildpath-add external jars,然后从您的 tomcat 文件夹中选择 servlet-api.jar

回答by ovunccetin

A possible reason for this error is using a wrong version of Servlet API. @WebServletannotation is supported by Servlet 3.0. You must change the version from possibly 2.5 to 3.0. To do this in eclipse, right click your project and open Properties. Select Project Facetsfrom the left menu of the page shown. Then change the version of Dynamic Web Modulefacet to 3.0.

此错误的一个可能原因是使用了错误版本的 Servlet API。Servlet 3.0@WebServlet支持注解。您必须将版本从 2.5 更改为 3.0。要在 Eclipse 中执行此操作,请右键单击您的项目并打开Properties。从所示页面的左侧菜单中选择Project Facets。然后将Dynamic Web Modulefacet的版本更改为 3.0。

Another possible reason can be related to your Tomcat version. Tomcat is supporting Servlet 3.0 beggining from version 7.0.

另一个可能的原因可能与您的 Tomcat 版本有关。Tomcat 从 7.0 版开始支持 Servlet 3.0。

回答by Gautami khante

Download servlet-api.jar there will be a zip file. Don't forget to unzip it. Because i was adding the zip folder and it was not working. Stop the server and clean it. publish it and restart it.

下载servlet-api.jar会有一个zip文件。不要忘记解压缩它。因为我正在添加 zip 文件夹,但它不起作用。停止服务器并清理它。发布它并重新启动它。

回答by tarun kumar143

Right click on project ---> Properties ---> Java Build Path ---> Add Library... ---> Server Runtime ---> Apache Tomcat ----> Finish.

右键单击项目 ---> 属性 ---> Java 构建路径 ---> 添加库... ---> 服务器运行时 ---> Apache Tomcat ----> 完成。