Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 838437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:18:18+00:00 2026-05-15T05:18:18+00:00

I’m attempting to upload a file into a jsp and then use the file

  • 0

I’m attempting to upload a file into a jsp and then use the file in some other code. My problem is that it comes into the servlet as an Object via the request.getAttribute() call so I don’t know what to cast it to.

I have this code so far to try and test what it is but I’m getting a NullPointerException.

test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Input Test</title>
    </head>
    <body>
        <form action="InputServlet" method="POST">
        <input type="file" name="file1">
        <input type="submit" value="submit">
        </form>
    </body>
</html>

inputservlet.java

public class InputServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)       throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        out.println(request.getAttribute("file1").getClass());
    }

}

Is my understanding of whats going on flawed or am I just coding it up wrong?

Also I’m expecting the type to be Object so if anyone knows what I should cast it too that would be very helpful as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:18:19+00:00Added an answer on May 15, 2026 at 5:18 am

    It’s likely null because it concerns a brand new and different request. You probably have sent a redirect to the servlet instead of a forward?

    Regardless, you should not process the file upload in a JSP file, but in a real servlet class. It’s otherwise recipe for trouble since it’s a view technology.

    See also:

    • How to retrieve uploaded image and save to a file with JSP?
    • Apache Commons FileUpload user guide

    Update: as per your code update, this won’t work. You need to set form’s enctype to multipart/form-data and use Commons FileUpload to process it in servlet. Also see the given links.

    To the point, the multipart/form-data encoding is not supported by the Servlet API prior to 3.0 and the input values are not available by request.getParameter() and consorts. The use of request.getAttribute() here is a misconception. There it is not for. You would need to parse the request.getInputStream() yourself as per RFC2388. You would however like to use Apache Commons FileUpload for this instead of reinventing and maintaining a wheel for years. Apache Commons already did it for you, take benefit of it.

    If you’re already on Servlet 3.0 (Glassfish v3), then you can use the builtin request.getParts() to gather the items. Most servletcontainers will use Commons FileUpload under the hoods, you only don’t see it in the /WEB-INF/lib or the imports, if that disturbs you for some reason.

    See also:

    • How to get the file name for <input type="file"> in jsp
    • How to upload an image using JSP -Servlet and EJB 3.0
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I am currently running into a problem where an element is coming back from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.