Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File Upload using Jsp and servlets...

 
Pavithra Praveen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am to execute a small example to upload a file. It is a application developed using Jsp and servlets.

Folder Structure:

Upload(project folder placed in webapps folder)
Main.html
Jsp1.jsp
\WEB-INF
\classes
FileUpload.java

Main.html
---------

HTML
HEAD
TITLE File Upload TITLE
HEAD
BODY
FORM ACTION=Jsp1.jsp ENCTYPE="MULTIPART/FORM-DATA" METHOD=POST
Author: INPUT TYPE=TEXT Name=Author
BR
Company: INPUT TYPE=TEXT Name=Company
BR
Select file to upload: INPUT TYPE=FILE Name=Filename
BR
INPUT TYPE=SUBMIT VALUE="Upload"
FORM
BODY
HTML

Jsp1.jsp
--------

jsp:useBean id="TheBean" scope="page" class="SimpleBean"
%
TheBean.doUpload(request);
%

FileUpload.java
---------------

import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletInputStream;

public class FileUploadBean {

public void doUpload(HttpServletRequest request) throws
IOException {
PrintWriter pw = new PrintWriter(
new BufferedWriter(new FileWriter("Demo.out")));
ServletInputStream in = request.getInputStream();

int i = in.read();
while (i != -1) {
pw.print((char) i);
i = in.read();
}
pw.close();
}
}

I am trying to execute this application, but after i click on Upload Button it is showing error as follows:

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 1 in the jsp file: /Jsp1.jsp
Generated servlet error:
D:\jakarta-tomcat-5.0.28\work\Catalina\localhost\upload\org\apache\jsp\Jsp1_jsp.java:42: cannot resolve symbol
symbol : class SimpleBean
location: class org.apache.jsp.Jsp1_jsp
SimpleBean TheBean = null;
^


An error occurred at line: 1 in the jsp file: /Jsp1.jsp
Generated servlet error:
D:\jakarta-tomcat-5.0.28\work\Catalina\localhost\upload\org\apache\jsp\Jsp1_jsp.java:44: cannot resolve symbol
symbol : class SimpleBean
location: class org.apache.jsp.Jsp1_jsp
TheBean = (SimpleBean) _jspx_page_context.getAttribute("TheBean", PageContext.PAGE_SCOPE); ^


An error occurred at line: 1 in the jsp file: /Jsp1.jsp
Generated servlet error:
D:\jakarta-tomcat-5.0.28\work\Catalina\localhost\upload\org\apache\jsp\Jsp1_jsp.java:46: cannot resolve symbol
symbol : class SimpleBean
location: class org.apache.jsp.Jsp1_jsp
TheBean = new SimpleBean();
^
3 errors

org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:332)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:412)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:472)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 
krishnamoorthy kitcha
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi pavithra

check this link

http://www.devsphere.com/mapping/docs/guide/upload.html

tell your feedback
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pavithra,

Please dont post the same question in multiple forums
 
Pavithra Praveen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where will i find class FileBean.java, because not able to compile UploadBean.java
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error messages don't mention "FileBean", they talk about "SimpleBean" missing. Do you have that? It's not part of the directory hierarchy you showed.

Also note that beans you use in JSP pages must be in a package, e.g. "mypackage.SimpleBean". Change your source code accordingly (don't forget to remove the old SimpleBean class file), and see how it goes.
 
kuldeep lakhani
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting following compiler error while trying to run below jsp in weblogic8.1 to upload multiple files to server. Code is working fine under Apache Tomcat environment, so theres no problem in code.

Problem is that, code is not able to find commons-upload.jar file containing org.apache.commons.fileupload package. I have added jar file to system classpath and path varibles.

could anyone please help.
Thanks in advance.


Error is like...-----------------------------------------------------

Compilation of 'C:\bea\user_projects\domains\mydomain\.\myserver\.wlnotdelete\extract\myserver_maximo_maximo\jsp_servlet\_webclient\_controls\_imageupload\_jsp\__uploadprocessor.java' failed:
--------------------------------------------------------------------------------
C:\bea\user_projects\domains\mydomain\.\myserver\.wlnotdelete\extract\myserver_maximo_maximo\jsp_servlet\_webclient\_controls\_imageupload\_jsp\__uploadprocessor.java:167: cannot resolve symbol
probably occurred due to an error in /webclient/controls/imageupload/JSP/uploadProcessor.jsp line 24:
if (FileUpload.isMultipartContent(request))

C:\bea\user_projects\domains\mydomain\.\myserver\.wlnotdelete\extract\myserver_maximo_maximo\jsp_servlet\_webclient\_controls\_imageupload\_jsp\__uploadprocessor.java:170: cannot resolve symbol
probably occurred due to an error in /webclient/controls/imageupload/JSP/uploadProcessor.jsp line 27:
DiskFileUpload diskFileUpload = new DiskFileUpload();

C:\bea\user_projects\domains\mydomain\.\myserver\.wlnotdelete\extract\myserver_maximo_maximo\jsp_servlet\_webclient\_controls\_imageupload\_jsp\__uploadprocessor.java:170: cannot resolve symbol
probably occurred due to an error in /webclient/controls/imageupload/JSP/uploadProcessor.jsp line 27:
DiskFileUpload diskFileUpload = new DiskFileUpload();

C:\bea\user_projects\domains\mydomain\.\myserver\.wlnotdelete\extract\myserver_maximo_maximo\jsp_servlet\_webclient\_controls\_imageupload\_jsp\__uploadprocessor.java:174: cannot resolve symbol
probably occurred due to an error in /webclient/controls/imageupload/JSP/uploadProcessor.jsp line 31:
FileItem fileItem = null;

C:\bea\user_projects\domains\mydomain\.\myserver\.wlnotdelete\extract\myserver_maximo_maximo\jsp_servlet\_webclient\_controls\_imageupload\_jsp\__uploadprocessor.java:183: cannot resolve symbol
probably occurred due to an error in /webclient/controls/imageupload/JSP/uploadProcessor.jsp line 40:
FileItem fileItemTemp = (FileItem)it.next();

C:\bea\user_projects\domains\mydomain\.\myserver\.wlnotdelete\extract\myserver_maximo_maximo\jsp_servlet\_webclient\_controls\_imageupload\_jsp\__uploadprocessor.java:183: cannot resolve symbol
probably occurred due to an error in /webclient/controls/imageupload/JSP/uploadProcessor.jsp line 40:
FileItem fileItemTemp = (FileItem)it.next();


--------------------------------------------------------------------------------
Full compiler error(s):
C:\bea\user_projects\domains\mydomain\.\myserver\.wlnotdelete\extract\myserver_maximo_maximo\jsp_servlet\_webclient\_controls\_imageupload\_jsp\__uploadprocessor.java:167: cannot resolve symbol
symbol : variable FileUpload
location: class jsp_servlet._webclient._controls._imageupload._jsp.__uploadprocessor
if (FileUpload.isMultipartContent(request)) //[ /webclient/controls/imageupload/JSP/uploadProcessor.jsp; Line: 24]
^
C:\bea\user_projects\domains\mydomain\.\myserver\.wlnotdelete\extract\myserver_maximo_maximo\jsp_servlet\_webclient\_controls\_imageupload\_jsp\__uploadprocessor.java:170: cannot resolve symbol
symbol : class DiskFileUpload
location: class jsp_servlet._webclient._controls._imageupload._jsp.__uploadprocessor
DiskFileUpload diskFileUpload = new DiskFileUpload(); //[ /webclient/controls/imageupload/JSP/uploadProcessor.jsp; Line: 27]
^
C:\bea\user_projects\domains\mydomain\.\myserver\.wlnotdelete\extract\myserver_maximo_maximo\jsp_servlet\_webclient\_controls\_imageupload\_jsp\__uploadprocessor.java:170: cannot resolve symbol
symbol : class DiskFileUpload
location: class jsp_servlet._webclient._controls._imageupload._jsp.__uploadprocessor
DiskFileUpload diskFileUpload = new DiskFileUpload(); //[ /webclient/controls/imageupload/JSP/uploadProcessor.jsp; Line: 27]
^
C:\bea\user_projects\domains\mydomain\.\myserver\.wlnotdelete\extract\myserver_maximo_maximo\jsp_servlet\_webclient\_controls\_imageupload\_jsp\__uploadprocessor.java:174: cannot resolve symbol
symbol : class FileItem
location: class jsp_servlet._webclient._controls._imageupload._jsp.__uploadprocessor
FileItem fileItem = null; //[ /webclient/controls/imageupload/JSP/uploadProcessor.jsp; Line: 31]
^
C:\bea\user_projects\domains\mydomain\.\myserver\.wlnotdelete\extract\myserver_maximo_maximo\jsp_servlet\_webclient\_controls\_imageupload\_jsp\__uploadprocessor.java:183: cannot resolve symbol
symbol : class FileItem
location: class jsp_servlet._webclient._controls._imageupload._jsp.__uploadprocessor
FileItem fileItemTemp = (FileItem)it.next(); //[ /webclient/controls/imageupload/JSP/uploadProcessor.jsp; Line: 40]
^
C:\bea\user_projects\domains\mydomain\.\myserver\.wlnotdelete\extract\myserver_maximo_maximo\jsp_servlet\_webclient\_controls\_imageupload\_jsp\__uploadprocessor.java:183: cannot resolve symbol
symbol : class FileItem
location: class jsp_servlet._webclient._controls._imageupload._jsp.__uploadprocessor
FileItem fileItemTemp = (FileItem)it.next(); //[ /webclient/controls/imageupload/JSP/uploadProcessor.jsp; Line: 40]
^
6 errors
---------------------------------------------------------------

Code is as below : uploadProcessor.jsp
---------------------------------------------------------------

< !-- uploadProcessor.jsp -->

<%@ page import="org.apache.commons.fileupload.*, java.util.*, java.io.*, java.lang.Exception" %>

.....some html code....

if (FileUpload.isMultipartContent(request))
{
System.out.println("UploadProcessor.jsp : In FileUpload.isMultipartContent...");
DiskFileUpload diskFileUpload = new DiskFileUpload();
List fileItemsList = diskFileUpload.parseRequest(request);

String optionalFileName = "";
FileItem fileItem = null;
File saveTo;

Iterator it = fileItemsList.iterator();

while (it.hasNext())
{
out.println("Hello");

FileItem fileItemTemp = (FileItem)it.next();
if (fileItemTemp.isFormField())
{


%>
Field name: <%= fileItemTemp.getFieldName() %>

Field value: <%= fileItemTemp.getString() %>



<%<br /> }<br /> else <br /> {<br /> <br /> fileItem = fileItemTemp;<br /> } //end if <br /> <br /> if (fileItem!=null)<br /> {<br /> //fileItem.setFieldName("kuldeep");<br /> String fileName = fileItem.getName();<br /> %>

Uploaded File Info:

Content type: <%= fileItem.getContentType() %>

Field name: <%= fileItem.getFieldName() %>

File name: <%= fileName %>

File size: <%= fileItem.getSize() %>


< !-- -->

<%<br /> /* Save the uploaded file if its size is greater than 0. */<br /> if (fileItem.getSize() > 0)
{
if (optionalFileName.trim().equals(""))
fileName = (new File(fileName)).getName();
/*else
fileName = optionalFileName; */


String extn=fileName.substring(fileName.indexOf("."));
////out.println(extn);
String dirName ="c:\\DOCLINKS\\CustomMaps\\";
try
{
//// saveTo = new File(dirName+cuser.trim()+".doc");
////saveTo = new File(dirName+"yog"+extn);
saveTo = new File(dirName+fileName);

fileItem.write(saveTo);
byte[] data = fileItem.get();

%>
Data Received at the Server


The uploaded file has been saved successfully.



<% }<br /> catch (Exception e)<br /> {<br /> %>An error occurred..
<%<br /> }<br /> }// if > 0
} // IF not null

} // while loop

} //If
%>

...some HTML code....

---------------------------------------------------------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic