You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by JASON HOLT <j_...@msn.com> on 2011/01/15 00:11:54 UTC

Tomcat 7.06 HttpRequestServlet.getPart() returns null in servlet

As JSF 2.0 doesn't include a file upload component, I need to write my own multipart/form-data process. According to the information in bug # 49711, I cannot do this with a filter in Tomcat 7, but I can in a servlet. However, using the following servlet, HttpRequestServlet.getPart() returns null. This code works fine in Glassfish. It was mostly generated by Netbeans 6.9.1. The error message follows.
 
Can you give me some guidance? Thanks for your kind help.
 
 

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
@WebServlet(name="FileUpload", urlPatterns={"/fileUpload.up"})
@MultipartConfig
public class FileUpload extends HttpServlet
{
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try
        {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet UploadFile</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<form action='fileUpload.up' method='POST' enctype='multipart/form-data' >");
            out.println("<input type='file' name='fileData' />");
            out.println("<br /><br />");
            out.println("<input type='submit' value='Continue'/>");
            out.println("</form></body>");
            out.println("</html>");
        }
        finally
        {
            out.close();
        }
    }
 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }
 
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        javax.servlet.http.Part part = request.getPart("fileData");
        byte[] bytes = new byte[(int) part.getSize()];
        // and so on...
        //processRequest(request, response);
    }
}
java.lang.NullPointerException
 at FileUpload.doPost(FileUpload.java:75)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
 at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
 at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
 at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
 at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
 at com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:542)
 at com.sun.faces.application.view.JspViewHandlingStrategy.executePageToBuildView(JspViewHandlingStrategy.java:359)
 at com.sun.faces.application.view.JspViewHandlingStrategy.buildView(JspViewHandlingStrategy.java:150)
 at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:96)
 at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
 at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:135)
 at javax.faces.webapp.FacesServlet.service(FacesServlet.java:309)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
 at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:550)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:380)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
 at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
 at java.lang.Thread.run(Unknown Source)
  		 	   		  

RE: SOLVED Tomcat 7.06 HttpRequestServlet.getPart() returns null in servlet

Posted by JASON HOLT <j_...@msn.com>.
Never mind. I found that the JSF servlet could not handle getPart() properly so I had to map the servlet to the default Tomcat servlet.
 
Thanks for looking at it.
 
> Date: Sat, 15 Jan 2011 09:56:10 +0000
> From: markt@apache.org
> To: users@tomcat.apache.org
> Subject: Re: Tomcat 7.06 HttpRequestServlet.getPart() returns null in servlet
> 
> On 14/01/2011 23:11, JASON HOLT wrote:
> > 
> > As JSF 2.0 doesn't include a file upload component, I need to write my own multipart/form-data process. According to the information in bug # 49711, I cannot do this with a filter in Tomcat 7, but I can in a servlet. However, using the following servlet, HttpRequestServlet.getPart() returns null. This code works fine in Glassfish. It was mostly generated by Netbeans 6.9.1. The error message follows.
> > 
> > Can you give me some guidance? Thanks for your kind help.
> 
> The basic functionality works since this is how the Tomcat Manager
> application does file uploads (although it is configured in web.xml
> rather than with annotations).
> 
> I'd check web.xml. Does it have the correct schema definition for a
> servlet 3.0 webapp? If not, Tomcat won't scan for annotations.
> 
> 
> > java.lang.NullPointerException
> > at FileUpload.doPost(FileUpload.java:75)
> > at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
> 
> Which line is this? There are less than 75 lines in the source you posted.
> 
> Mark
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
 		 	   		  

RE: Tomcat 7.06 HttpRequestServlet.getPart() returns null in servlet

Posted by JASON HOLT <j_...@msn.com>.
> 
> The basic functionality works since this is how the Tomcat Manager
> application does file uploads (although it is configured in web.xml
> rather than with annotations).
> 
> I'd check web.xml. Does it have the correct schema definition for a
> servlet 3.0 webapp? If not, Tomcat won't scan for annotations.
> 
> 

Mark, below is the application web.xml. Below that is revised servlet code that checks if getPart() returns null rather than throwing the error.
 
I have not touched the default tomcat 7.0.6 /conf/web.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

 

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
@WebServlet(name = "FileUpload", urlPatterns = { "/fileUpload.up"} )
@MultipartConfig
public class FileUpload extends HttpServlet
{
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try
        {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet UploadFile</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<form action='fileUpload.up' method='POST' enctype='multipart/form-data' >");
            out.println("<input type='file' name='fileData' />");
            out.println("<br /><br />");
            out.println("<input type='submit' value='Continue'/>");
            out.println("</form></body>");
            out.println("</html>");
        }
        finally
        {
            out.close();
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        javax.servlet.http.Part part = request.getPart("fileData");
        if (part == null)
        {
            PrintWriter out = response.getWriter();
            try
            {
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Servlet UploadFile</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<p>Part is null</p?");
                out.println("</body></html>");
            }
            catch (Exception e)
            {
            }
            finally
            {
                out.close();
            }
        }
    }
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }
}

 
 		 	   		  

Re: Tomcat 7.06 HttpRequestServlet.getPart() returns null in servlet

Posted by Mark Thomas <ma...@apache.org>.
On 14/01/2011 23:11, JASON HOLT wrote:
> 
> As JSF 2.0 doesn't include a file upload component, I need to write my own multipart/form-data process. According to the information in bug # 49711, I cannot do this with a filter in Tomcat 7, but I can in a servlet. However, using the following servlet, HttpRequestServlet.getPart() returns null. This code works fine in Glassfish. It was mostly generated by Netbeans 6.9.1. The error message follows.
>  
> Can you give me some guidance? Thanks for your kind help.

The basic functionality works since this is how the Tomcat Manager
application does file uploads (although it is configured in web.xml
rather than with annotations).

I'd check web.xml. Does it have the correct schema definition for a
servlet 3.0 webapp? If not, Tomcat won't scan for annotations.


> java.lang.NullPointerException
>  at FileUpload.doPost(FileUpload.java:75)
>  at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)

Which line is this? There are less than 75 lines in the source you posted.

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org