Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default How to get the values from one jsp page to another jsp page

    Hi
    I have one jsp page with two text fields and one file upload button. Successfully i can upload the file to a specific folder.

    But the problem is when i am trying to get the values from those textfields in another jsp/servlet, i got null values for the textfields. I have written the code in second jsp page for both uploading and get the values of textfields.Please help me.

    Thanks in Advance.

    My second jsp page code is below:




    import java.io.DataInputStream;
    import com.ajel.Getcon;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
    * Servlet implementation class Upload
    */
    public class Upload extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    PrintWriter out=res.getWriter();
    res.setContentType("text/html");
    String rname=req.getParameter("recruiter");
    out.println(rname);
    String contentType = req.getContentType();
    try {
    Connection con=Getcon.getConnection();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
    DataInputStream in = new DataInputStream(req.getInputStream());
    //we are taking the length of Content type data
    int formDataLength = req.getContentLength();
    byte dataBytes[] = new byte[formDataLength];
    int byteRead = 0;
    int totalBytesRead = 0;

    //this loop converting the uploaded file into byte code
    while (totalBytesRead < formDataLength) {
    byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
    totalBytesRead += byteRead;
    }
    String file = new String(dataBytes);
    //for saving the file name
    String saveFile = file.substring(file.indexOf("filename=\"") + 10);
    saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
    saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
    int lastIndex = contentType.lastIndexOf("=");
    String boundary = contentType.substring(lastIndex + 1,contentType.length());
    int pos;
    //extracting the index of file
    pos = file.indexOf("filename=\"");
    pos = file.indexOf("\n", pos) + 1;
    pos = file.indexOf("\n", pos) + 1;
    pos = file.indexOf("\n", pos) + 1;
    int boundaryLocation = file.indexOf(boundary, pos) - 4;
    int startPos = ((file.substring(0, pos)).getBytes()).length;
    int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
    // creating a new file with the same name and writing the content in new file
    saveFile="E:\\workspace\\Ajelsite\\Resumes\\"+save File;
    FileOutputStream fileOut = new FileOutputStream(saveFile);
    fileOut.write(dataBytes, startPos, (endPos - startPos));

    fileOut.flush();

    fileOut.close();
    out.println("File saved as " +saveFile);
    }

    }
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Use opensource fileupload apis like cos.jar or apache-commons-fileupload which allow you to read both the file and the parameters submitted with the file.
    To get the parameters to display on the second JSP you'd need to set them in some scope (request or session) in the servlet before forwarding to that second JSP.

  3. #3
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Please send me the sample code.
    Thanks in advance.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default

    Quote Originally Posted by durgamalleswari View Post
    Please send me the sample code.
    This is a forum, not a code factory. Recmmended reading: How to ask questions the smart way

    db

Similar Threads

  1. Displaying array values in jstl page
    By keshaba in forum Advanced Java
    Replies: 0
    Last Post: 04-23-2010, 10:00 AM
  2. Submit values to a parent page
    By carag in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 12:33 AM
  3. to retrieve multiple values from html page through jsp
    By raghu9198 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 02-17-2009, 02:43 PM
  4. unable to update MYSQL with values from jsp page
    By koushika in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-27-2008, 01:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •