按照 user2821894 的建议,我能够让我的应用程序再次运行,但是在尝试调用 servlet tomcat 7 后再次停止工作!如果我尝试删除我调用我的 servlet 的代码,我的 web 应用程序不会工作!一旦我遇到 servlet 问题,tomcat 就会停止工作。
我在 Eclipse 上启动我的 Web 项目时遇到问题。我对Tomcat 7有问题。所以我从eclipse中“删除”了tomcat 7,然后我再次添加了它(再次是tomcat 7)。
现在我启动我的网络项目没有问题,但我的 servlet 有问题。例如我得到错误
WebServlet cannot be resolved to a type
The attribute value is undefined for the annotation type
我将 servlet-api 3.0.jar 添加到我的项目中,但我仍然遇到这些问题。
这是我的 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);
}
}
}
这是我的 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>