开发者

Servlet Issue - Index page not popping up

I am using spring framework and i am having Index.jsp like :

Code:

<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>

I have ImportService as

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);

    }
}

In my web.xml, I have

<servlet>
        <servlet-name>ImportService</servlet-name>
        <servlet-class>com.servic开发者_C百科e.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>

Now when I try to hit the url: http://localhost:8080/delta-webapp/ImportService/jsp/Index.jsp then it gives me blank message and html form is not rendered.

Any suggestions or pointers?


change your entry URL from http://localhost:8080/delta-webapp/ImportService/jsp/Index.jsp to http://localhost:8080/delta-webapp/jsp/Index.jsp

You've mapped ImportService servlet to handle all requests to /ImportService/*.

Your index.jsp is located at /jsp/Index.jsp. If instead you just put /delta-webapp It /may/ work.

In general though, I don't think welcome files have hard coded paths. Typically you specify something like index.jsp as your welcome file and if nothing handles the request at a particular location, it will fall back to see if one of the welcome files listed is available and will render that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜