Servlet问题 - 索引页面没有弹出

时间:2011-07-13 13:28:06

标签: java servlets

我正在使用spring框架,我正在使用Index.jsp

代码:

<html>
<head></head>
<body>
    <p>File Upload</p>
    <form action="ImportService" enctype="multipart/form-data" method="POST">
        <input type="file" name="file1"><br>
        <input type="Submit" value="Upload File"><br>
    </form>
</body>

我有ImportService作为

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class ImportService extends HttpServlet {
    private static final String TMP_DIR_PATH = "c:\\tmp";
    private File tmpDir;
    private static final String DESTINATION_DIR_PATH = "c:\\files";
    private File destinationDir;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        tmpDir = new File(TMP_DIR_PATH);
        if (!tmpDir.isDirectory()) {
            throw new ServletException(TMP_DIR_PATH + " is not a directory");
        }
        // String realPath =
        // getServletContext().getRealPath(DESTINATION_DIR_PATH);
        destinationDir = new File(DESTINATION_DIR_PATH);
        if (!destinationDir.isDirectory()) {
            throw new ServletException(DESTINATION_DIR_PATH
                    + " is not a directory");
        }

    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String name = "param";
        String value = request.getParameter(name);

        String welcomeMessage = "Welcome " + value;
        System.out.println("Message=" + welcomeMessage);

        response.setContentType("text/html");
        response.setContentType("text/plain");
        System.out.println("Inside Get Method");

    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Inside Post Method");
        PrintWriter out = response.getWriter();

        DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();

        /*
         * Set the size threshold, above which content will be stored on disk.
         */
        fileItemFactory.setSizeThreshold(1 * 1024 * 1024); // 1 MB

        /*
         * Set the temporary directory to store the uploaded files of size above
         * threshold.
         */
        fileItemFactory.setRepository(tmpDir);

        ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
        try {
            /*
             * Parse the request
             */
            List items = uploadHandler.parseRequest(request);
            Iterator itr = items.iterator();
            while (itr.hasNext()) {
                FileItem item = (FileItem) itr.next();
                /*
                 * Handle Form Fields.
                 */
                if (item.isFormField()) {
                    out.println("File Name = " + item.getFieldName()
                            + ", Value = " + item.getString());
                } else {
                    // Handle Uploaded files.
                    out.println("Field Name = " + item.getFieldName()
                            + ", File Name = " + item.getName()
                            + ", Content type = " + item.getContentType()
                            + ", File Size = " + item.getSize());

                }

                out.close();
            }
        } catch (FileUploadException ex) {
            log("Error encountered while parsing the request", ex);
        } catch (Exception ex) {
            log("Error encountered while uploading file", ex);
        }
        // doGet(request, response);

    }
}

在我的web.xml中,我有

<servlet>
        <servlet-name>ImportService</servlet-name>
        <servlet-class>com.service.ImportService</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ImportService</servlet-name>
        <url-pattern>/ImportService/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>/jsp/Index.jsp</welcome-file>
    </welcome-file-list>

现在,当我尝试点击网址:http://localhost:8080/delta-webapp/ImportService/jsp/Index.jsp时,它会给我空白消息并且不会呈现html表单。

有任何建议或指示吗?

1 个答案:

答案 0 :(得分:1)

http://localhost:8080/delta-webapp/ImportService/jsp/Index.jsp更改您的输入网址 到http://localhost:8080/delta-webapp/jsp/Index.jsp

您已映射ImportService servlet以处理对/ ImportService /*.

的所有请求

您的index.jsp位于/jsp/Index.jsp。如果你只是把/ delta-webapp It / may / work。

总的来说,我不认为欢迎文件有硬编码路径。通常,您将index.jsp之类的内容指定为欢迎文件,如果在特定位置没有处理请求,它将回退以查看列出的欢迎文件之一是否可用并将呈现该内容。