-3

I am uploading a file in a form, that form also contains some textfields I enter some values in the textfields. I want this value to remain when I click upload button. And there is also a save button, when I click this button uploaded file should get saved in database. Can any one help me out?

JSP file is here:

<body>
    <form action="./upload" enctype="multipart/form-data" >ID: <input type="text" name="id" value="" />Name: <input type="text" name="name" value="" />
       <input name="uploaded" type="file" />
       <input name="save" type="submit" value="Upload" />

       <input type="submit" value="save1" name="save" /></form>
</body>

I need the bussiness logic in a servlet..

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
user2154010
  • 1
  • 1
  • 1

3 Answers3

0

Your options are:

The easiest option is the IFRAME.

Zach Riggle
  • 2,975
  • 19
  • 26
0

Hey you are saying the text field values should be there while clicking Upload button. You don't have to do this thing. By default it will be there. You should it will venish man? Please mention your exact requirement.

sanit
  • 1,646
  • 1
  • 18
  • 21
  • initially textfields are empty..m entering data..after that m uploading a file..my uploading logic is in a servlet..from servlet again this jsp page is called...since it is reloaded again, all the values in the textfield disappears...i want these value to remain in the textfields... – user2154010 Mar 11 '13 at 05:24
0

See there is no provision to keep the file field data using value attribute.

See this link

package comunity.stackoverflow.test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestController
 */
public class TestController extends HttpServlet {
    private static final long serialVersionUID = 1L;
public TestController() {
    super();
}

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    process(request, response);
}

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

private void process(HttpServletRequest request,
        HttpServletResponse response) {
    storeInRequest(request, "id");
    storeInRequest(request, "name");
    storeInRequest(request, "uploaded");

    // write your upload logic here then navigate to the same page;

    try {
        request.getRequestDispatcher("/test.jsp").forward(request, response);
    } catch (ServletException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private void storeInRequest(HttpServletRequest request,String param){
    String val =  request.getParameter(param);
    if(param!=null && !param.isEmpty()){
        System.out.println(val);
        request.setAttribute(param, val);
    }
}

}

Use standard.jat and jstl.jar

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
<form action="upload.do" enctype="multipart/form-data" >
    ID: <input type="text" name="id" value="${id}"/>
    Name: <input type="text" name="name" value="${name}" />
       <input name="uploaded" type="file" />
       <input name="save" type="submit" value="Upload" />

       <input type="submit" value="save1" name="save" /></form>

</body>
</html>

Use this JSP file. It might solve your problem.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
<form action="upload.do" enctype="multipart/form-data" >
    ID: <input type="text" name="id" value="${id}"/><br/>
    Name: <input type="text" name="name" value="${name}" /><br/>
    <input type="file" id="selectedFile" style="display: none;" />
    <input type="text" name="uploaded" id="uploaded" value="${uploaded}" readonly="readonly" size="60">
    <input type="button" value="Browse..." onclick="mymethod()" /><br/>
       <input name="save" type="submit" value="Upload" />

       <input type="submit" value="save1" name="save" /></form>

</body>
<script type="text/javascript">
function mymethod(){
    document.getElementById('selectedFile').click();
    var val = document.getElementById('selectedFile').value;
    document.getElementById('uploaded').value = val;
}
</script>
</html>
Community
  • 1
  • 1
sanit
  • 1,646
  • 1
  • 18
  • 21