• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to redirect to another jsp page using multi-part form?

 
jenny tan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to all

I am currently using eclipse and tom cat to do up a website. However i face problem with redirecting to another jsp page after submission. this is due to the fact that i cannot do any response.sendRedirect(url); in Hashtable. Can anyone please help me to solve this issue? Thank you in advance


ps( i have did up the pages for upload of single file with text , using hashtable)


SaveSignUp.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.*, java.io.*" %>
<%@ page import="java.sql.*, java.text.*"%>
<%@ page import= " java.util.Calendar.*"%>

<!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=ISO-8859-1">
<title>Receive the submitted file</title>
<%! //note we use <%! to declare static fields and methods

private static final String UPLOAD_FOLDER = "C:\\peiszetemp\\workspace\\FEPPZ\\WebContent\\useruploads";
//C:\peiszetemp\workspace
//Retrieve file and save to a folder
private void saveFileToFolder(FileItem item) throws Exception
{
//item.getName() will retrieve the full name (including directory)
//of the file to be uploaded
File f = new File(item.getName());

//File.getName() will return us only the name (without directory)

String filename = f.getName();



//Save the file to our directory (UPLOAD_FOLDER see top).
File uploadedFile = new File(UPLOAD_FOLDER + "\\" + filename);
item.write(uploadedFile);
}

// save to Database using Hashtable
// - item
// - input form
private void saveToDatabase(Hashtable ht) throws Exception
{
String FirstName = (String)ht.get("FirstName"); // WORK
String LastName = (String)ht.get("LastName");
String Gender = (String)ht.get("Gender");
String Day = (String)ht.get("Day");
String HousePhone = (String)ht.get("HousePhone");
String MobilePhone = (String)ht.get("MobilePhone");
String Address = (String)ht.get("Address");
String Country = (String)ht.get("Country");
String ZipCode = (String)ht.get("ZipCode");
String Email = (String)ht.get("Email");
String Password = (String)ht.get("Password");
String FavColor = (String)ht.get("FavColor");
String FavTypeOfHat = (String)ht.get("FavTypeOfHat");
String Newsletter = (String)ht.get("Newsletter");
String Sq = (String)ht.get("Sq");

String filename = "" ; // WORK
//ht.put("item", item);
FileItem item = (FileItem)ht.get("item");
InputStream inputStream= (InputStream)item.getInputStream() ; // WORK
//InputStream a = (F)ht.getInputStream());
File file = new File(item.getName());
filename = file.getName();


Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/feppz", "root", "");
PreparedStatement ps = con.prepareStatement("INSERT INTO user VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
//InputStream is = f.getInputStream();

// InputStream in = new FileInputStream(src);
ps.setBinaryStream(17, inputStream);
ps.setString(18, filename);
ps.setString(1, FirstName);
ps.setString(2, LastName);
ps.setString(3, Gender);
ps.setString(4, Day);
ps.setString(5, HousePhone);
ps.setString(6, MobilePhone);
ps.setString(7, Address);
ps.setString(8, Country);
ps.setString(9, ZipCode);
ps.setString(10, Email);
ps.setString(11, Password);
ps.setString(12, FavColor);
ps.setString(13, FavTypeOfHat);
ps.setString(14, Newsletter);
ps.setString(15, Sq);
ps.setDate(16, new java.sql.Date(Calendar.getInstance().getTimeInMillis()));
ps.executeUpdate();
con.close();


}

private void saveFileToDatabase(FileItem item) throws Exception
{
File f = new File(item.getName());
String filename = f.getName(); // WORK
InputStream inputStream= item.getInputStream(); // WORK

//Load our mySQL driver
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/feppz", "root", "");
PreparedStatement ps = con.prepareStatement("INSERT INTO user (UserId, ProfileImg, ImgName) value(null, ?, ?)");

InputStream is = item.getInputStream();//where the secret is. Retrieve the list of file.getInputStream read the file.
ps.setBinaryStream(1, inputStream);
ps.setString(2, filename);
ps.executeUpdate();
con.close();

}
%>
</head>
<body>
<%
//Ensure that user send in a multipart form
boolean isMultiPart = ServletFileUpload.isMultipartContent(request);//in html in MultiPartContent
if(isMultiPart == false) {
response.sendRedirect("SignUp.jsp");
return;
}

//Creating a new DiskFileItemFactory that is needed when we create
//a new ServletFileUpload object
DiskFileItemFactory factory = new DiskFileItemFactory();//this is the guy that can create thing for us create a file upload object that can save file for us
File f = new File(UPLOAD_FOLDER);//define at the very top at the
factory.setRepository(f);//telling the factory where to store the file C:\\Users\\cylaw\\Documents\\Workspaces\\demoImageInDatabase\\ImageInDb\\WebContent\\uploads"; and correspond to the upload folder at web content.

//Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);//factory contains a file object where we cna store the file.

//The ServletFileUpload object will parse (ie read and extract data)
//the request to obtain the file and its related information
List files = upload.parseRequest(request); //parse is read the request and get the info for us.Go through the list of all the file.

//Go through the list of files
//Although we coded our form such that it only supports one upload
//we can actually handle muliple files, we use an iterator to go
//through all the files being uploaded
Iterator iter = files.iterator();
Hashtable ht = new Hashtable();
while (iter.hasNext()) {

//The iterator's next() method will return a FileItem
FileItem item = (FileItem) iter.next();

//if isFormField == true, it means that it is a regular string item
if (item.isFormField() == false) {
//a file, save to folder to database
//comment out the other
//saveFileToFolder(item);
//formfield is not string but is a file
//if want to save file and load
//Client want the file
//saveFileToDatabase(item); //to use this method you have to have database setup properly
ht.put("item", item);

}
else {
System.out.println(item.getString());
ht.put(item.getFieldName(), item.getString());
//ht.put(item.getFieldName(), item.getString());
}
}

saveToDatabase(ht);
%>
</body>
</html>
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags and limit posted source to only what is required to understand the problem.
 
jenny tan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that.
Picture1.png
[Thumbnail for Picture1.png]
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...

This FAQ entry describes using code tags.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not clear to me what problem you're having; you don't redirect to pages with forms or hash tables.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really not indent your code?
 
Loh Peggie
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Thread hi-jack removed]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!