简体   繁体   English

JSP Java servlet在web.xml中保持获取错误

[英]JSP java servlet keep get error in web.xml

i tried to make this work . 我试图使这项工作。

But still give me error 但是还是给我错误

I still new in this 我还是这个新人

My .jsp file 我的.jsp文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Staff Page - Add Book</title>
    </head>

    <body>
                <center>
                    <h2 style="text-align: center;">Return Book Form.</h2>


                    <form name="uForm" method="get" action="addbookServlet" enctype="multipart/form-data">

                        <table align="center" width="300px" style="background-color:#f5f5f5;border:1px solid #e5e5e5; padding:5px;">
                            <tr><td colspan=2 align="center" height="10px"></td></tr>
                            <tr>
                                <td><b>Book ID</b></td>
                                <td><input type="text" name="book_id" size="50"></td>
                            </tr>
                            <tr>
                                <td><b>Book Title</b></td>
                                <td><input type="text" name="book_title" size="50"></td>
                            </tr>
                            <tr>
                                <td><b>Book Author</b></td>
                                <td><input type="text" name="book_author" size="50"></td>
                            </tr>
                            <tr>
                                <td><b>Book Quantity</b></td>
                                <td><input type="text" name="book_quantity" size="50"></td>
                            </tr>
                            <tr>
                                <td><b>Book Location</b></td>
                                <td><input type="text" name="book_location" size="50"></td>
                            </tr>
                            <tr>
                                <td><b>Book Image</b></td>
                                <td><input type="file" name="book_image" accept="image/x-png, image/gif, image/jpeg" size="50"></td>
                            </tr>
                            <tr>
                                <td></td>
                                <td><input type="submit" class="g-button" value="Submit"></td>
                            </tr>
                            <tr><td colspan=2 align="center" height="10px"></td></tr>
                        </table>

                    </form>
                </center>
    </body>
</html>

My .java file 我的.java文件

package caal;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
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.http.Part;
import caal.DbConnection.*;

public class AddBook extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //response.setContentType("text/html");
       // PrintWriter out = response.getWriter();
        Connection con = null; // connection to the database
        String message = null;  // message will be sent back to client
       // Statement stmt;


        String book_id = request.getParameter("book_id");
        String book_title = request.getParameter("book_title");
        String book_author = request.getParameter("book_author");
        String book_quantity = request.getParameter("book_quantity");
        String book_location = request.getParameter("book_location");
        //String book_image = request.getParameter("book_image");
        InputStream inputStream = null; // input stream of the upload file

        // obtains the upload file part in this multipart request
        Part filePart = request.getPart("book_image");
        if (filePart != null) {
            // prints out some information for debugging
            System.out.println(filePart.getName());
            System.out.println(filePart.getSize());
            System.out.println(filePart.getContentType());

            // obtains input stream of the upload file
            inputStream = filePart.getInputStream();
        }

        try {
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            con = DbConnection.getConn();
            String query = "INSERT INTO booktable(bookId, bookTitle, bookAuthor, bookQuantity, bookLocation, bookimage) VALUES (?,?,?,?,?,?)";
            System.out.println("query " + query);
            PreparedStatement statement = con.prepareStatement(query);
            statement.setString(1, book_id);
            statement.setString(2, book_title);
            statement.setString(3, book_author);
            statement.setString(4, book_quantity);
            statement.setString(5, book_location);

            if (inputStream != null) {
                // fetches input stream of the upload file for the blob column
                statement.setBlob(6, inputStream);
            }
            //stmt = con.createStatement();
            //stmt.executeUpdate(query);
            response.sendRedirect("login.jsp");
            con.close();
            System.out.println("Disconnected from database");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                // closes the database connection
                try {
                    con.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            // sets the message in request scope
            request.setAttribute("Message", message);

            // forwards to the message page
            getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
        }
    }
}

My web.xml become error . 我的web.xml变成错误。 if i put like this 如果我这样放

 <!-- SERVLET FOR ADD BOOK -->
   <servlet>
        <servlet-name>addBookServlet</servlet-name>
        <servlet-class>caal.AddBook</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>addBookServlet</servlet-name>
        <url-pattern>/addbookServlet</url-pattern>
    </servlet-mapping>
    <multipart-config>
    <location>/tmp</location>
    <max-file-size>20848820</max-file-size>
    <max-request-size>418018841</max-request-size>
    <file-size-threshold>1048576</file-size-threshold>
</multipart-config>

The error is 错误是

Error occurred during deployment: Exception while deploying the app [fpx] : org.xml.sax.SAXParseException; lineNumber: 52; columnNumber: 23; Deployment descriptor file WEB-INF/web.xml in archive [web].  cvc-complex-type.2.4.a: Invalid content was found starting with element 'multipart-config'. One of '{"http://java.sun.com/xml/ns/j2ee":description, "http://java.sun.com/xml/ns/j2ee":display-name, "http://java.sun.com/xml/ns/j2ee":icon, "http://java.sun.com/xml/ns/j2ee":distributable, "http://java.sun.com/xml/ns/j2ee":context-param, "http://java.sun.com/xml/ns/j2ee":filter, "http://java.sun.com/xml/ns/j2ee":filter-mapping, "http://java.sun.com/xml/ns/j2ee":listener, "http://java.sun.com/xml/ns/j2ee":servlet, "http://java.sun.com/xml/ns/j2ee":servlet-mapping, "http://java.sun.com/xml/ns/j2ee":session-config, "http://java.sun.com/xml/ns/j2ee":mime-mapping, "http://java.sun.com/xml/ns/j2ee":welcome-file-list, "http://java.sun.com/xml/ns/j2ee":error-page, "http://java.sun.com/xml/ns/j2ee":jsp-config, "http://java.sun.com/xml/ns/j2ee":security-constraint, "http://java.sun.com/xml/ns/j2ee":login-config, "http://java.sun.com/xml/ns/j2ee":security-role, "http://java.sun.com/xml/ns/j2ee":env-entry, "http://java.sun.com/xml/ns/j2ee":ejb-ref, "http://java.sun.com/xml/ns/j2ee":ejb-local-ref, "http://java.sun.com/xml/ns/j2ee":service-ref, "http://java.sun.com/xml/ns/j2ee":resource-ref, "http://java.sun.com/xml/ns/j2ee":resource-env-ref, "http://java.sun.com/xml/ns/j2ee":message-destination-ref, "http://java.sun.com/xml/ns/j2ee":message-destination, "http://java.sun.com/xml/ns/j2ee":locale-encoding-mapping-list}' is expected.. Please see server.log for more details.

I also have try using @MultipartConfig but it still not working . 我也尝试使用@MultipartConfig,但仍然无法正常工作。 I need some help . 我需要协助 。

In web.xml, you must have correct and corresponding DOCTYPE and DTD/XSD setting, otherwise you may get xml invalidation error. 在web.xml中,您必须具有正确且对应的DOCTYPE和DTD / XSD设置,否则可能会收到xml无效错误。

Try check your web.xml XSD setting refer here and try again. 请尝试在此处检查您的web.xml XSD设置然后重试。

The XSD version is preferred since JSP 2.0 / Servlets 2.4 (eg: Tomcat 5.5). 自JSP 2.0 / Servlets 2.4(例如:Tomcat 5.5)以来,首选XSD版本。 Note that the XML encoding can be specified as ISO-8859-1, UTF-8, or any other valid encoding in either version, and should match the actual encoding of your text file. 请注意,可以将XML编码指定为ISO-8859-1,UTF-8或任一版本中的任何其他有效编码,并且应与文本文件的实际编码匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM