File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes problem in using Servlet API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic

problem in using Servlet API

yoman sharma

Joined: Feb 10, 2005
Posts: 13
Actually i have downloaded a JAR file.
I have also placed the jar file in jspbook/WEB-INF/lib/ folder .
I am trying file upload through a HTML file which has following code. actually i am accessing servlet FileUploadCommons though this HTML file and using an API for file upload present in that JAR file.

<title>Example HTML Form</title>
<p>Select a file to upload or <a href="/jspbook/files/">browse
currently uploaded files.</a></p>
<form action=""
method="post" enctype="multipart/form-data">
File: <input type="file" name="file"><br>
<input value="Upload File" type="submit">

The source code for servlet FileUploadCommons is as follows ::::

package com.jspbook;

import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import java.util.*;

public class FileUploadCommons extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

PrintWriter out = response.getWriter();

out.print("File upload success. <a href=\"/jspbook/files/");
out.print("\">Click here to browse through all uploaded ");

ServletContext sc = getServletContext();
String path = sc.getRealPath("/files");
org.apache.commons.fileupload.FileUpload fu = new
try {
List l = fu.parseRequest(request);
Iterator i = l.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem);
// trim out full path info if it is included
String filename = fi.getName();
int slash = filename.lastIndexOf("\\");
if (slash != -1) {
filename = filename.substring(slash + 1);
// write the file to the 'files' directory
catch (Exception e) {
throw new ServletException(e);

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doPost(request, response);

Also i am using Tomcat 5 as the web server.

But when i try to upload the file by running the HTML file i am getting the following ERROR MESSAGE :

HTTP Status 500 -


type Exception report


description The server encountered an internal error () that prevented it from fulfilling this request.


javax.servlet.ServletException: Servlet execution threw an exception

root cause

java.lang.NoSuchMethodError: org.apache.commons.fileupload.FileUpload.setSizeMax(I)V
com.jspbook.FileUploadCommons.doPost(Unknown Source)

note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.


Apache Tomcat/5.0.28

Please tell me what to do?
I agree. Here's the link:
subject: problem in using Servlet API
jQuery in Action, 3rd edition