in html body

    <form method="get" action="MovieTiming.jsp">
        <table  cellspacing="0" cellpadding="0" border="2" style="width:50%; height:   400px;" bgcolor="pink">
            <tr style="height:4px;">
                <td id="Date" colspan="5">
                    Please input date (yyyymmdd)<input  name="Date" type="text" size="8" maxlength="8" value="<%=date%>"></input>
                    <button type="submit" value="Submit">Get Movies List</button>     
                </td>        
                <td>
                   <button   onclick=fileWrite();>save file</button>
                   <input type="file" id="loc" name="loc" >[/B]</input>


                </td>        
            </tr>
            <tr bgcolor="white">
                <td id="output" colspan="10" style="height:400Px;">&nbsp;</td><td><textarea id="out" name="out" rows=20 cols=20 ></textarea></td> 
            </tr>
        </table>
        </form>




function fileWrite
{
<%
            String loc  = request.getParameter("file");
            String func = request.getParameter("out");
            if(func!=null)
            {

                File strFile        = new File(loc);
                Writer objWriter    = new BufferedWriter(new FileWriter(strFile));
                objWriter.write(func);
                objWriter.flush();
                objWriter.close();
            }
            %>
}

but this code is throwing exception whenever i click on save as button says that no such file exist.
is there any other way of write data to the file using html javascript or jsp(embed in html)
prefer javascript

please help
please help

Dani AI

Generated

The root problem is a mix of client-side and server-side assumptions. Server-side JSP runs before the page reaches the browser, so embedding server write logic inside a JavaScript function will not execute on the server when the button is clicked. Also, modern browsers do not expose a full client file path from an <input type="file"> for security reasons, and sending file data requires a POST with enctype="multipart/form-data" rather than GET.

To save content on the server: post the text (or upload the file) to a server endpoint and let server code perform the write. For multipart uploads use the servlet API (or Apache Commons FileUpload on older containers). Store files in a dedicated, configurable directory (outside the webapp if possible), sanitize filenames to avoid traversal, and ensure the container user has write permission. Example (Servlet 3.0 style) for saving an uploaded part:

Part p = request.getPart("file");
String name = Paths.get(p.getSubmittedFileName()).getFileName().toString();
Path target = Paths.get("/var/app/data").resolve(name);
try (InputStream in = p.getInputStream()) {
  Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
}

If the intent was to save a textarea to the user's machine (client-side), no server write is needed. Use JavaScript to create a Blob and trigger a download (works offline and avoids server permissions):

const text = document.getElementById('out').value;
const blob = new Blob([text], {type:'text/plain'});
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'output.txt';
a.click();
URL.revokeObjectURL(a.href);

As noted, file-permission issues are common—check server logs and the exception stack trace to see whether the error is a missing path, permission denied, or malformed request. For uploads remember method="post" and enctype="multipart/form-data", prefer absolute server paths for testing, and never trust a client-supplied path without validation.

check the location, may be there is some permission problem in server, there u have to put file read/write permission some time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.