Solved

use passworded jsp page to get files

Posted on 2004-04-15
6
245 Views
Last Modified: 2010-04-01
Hey i want to have a jsp page where user can type in a login/password,  the jsp page will then just do a simple if(password="blabla") to check access. Now if the password is correct, the jsp page should allow the user (or just send it to the user?) to download a zip file.  My question is how do i do the part where jsp gets the file.  Can someone provide me with the code.   So basically

1)jsp check password
2) if correct password grab file and send it to user to download
3) the file should be in WEB-INF directory so people cant just type in the url and download it.

thanks!

gaga
0
Comment
Question by:gagaliya
  • 5
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10831077
use a download servlet something like:

public class Download extends HttpServlet
{
       public void doGet(HttpServletRequest req ,HttpServletResponse res) throws IOException,ServletException
       {
             doPost(req, res);
       }
       
       public void doPost(HttpServletRequest req ,HttpServletResponse res) throws IOException,ServletException
       {
        res.setContentType("application/octet-stream" + "; name=\"" + filename + "\"");
        res.setHeader("Content-Disposition","attachment; filename=" + filename +";");
        ServletOutputStream outputstream = res.getOutputStream();
        BufferedInputStream inputstream = new BufferedInputStream(new FileInputStream(downloadFile));
               
        int data;

        byte[] buffer = new byte[4096];
        while((data = inputstream.read(buffer))!=-1)
        {
            outputstream.write(buffer, 0, data);
        }
        inputstream.close();
        outputstream.close();
    }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 10831080
you just need to add the file details, plus do the password check.
let me know if you need a hand
0
 
LVL 1

Author Comment

by:gagaliya
ID: 10833425
hey objects,

can you post the jsp version? once again same problem:P i dont have access to the app server so cant deploy new servlets. But jsp can access the WEB-INF directory? because i remember normal html pages is restricted from accessing the WEB-INF directory.  thanks!

gaga
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 92

Expert Comment

by:objects
ID: 10837860
jsp's are meant for returning text ( typically html) and not binary content.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 10837870
try something like:

<%@ page import = "java.util.*, java.io.*" %>
<%
        response.setContentType("application/octet-stream" + "; name=\"" + filename + "\"");
        response.setHeader("Content-Disposition","attachment; filename=" + filename +";");
        OutputStream outputstream = response.getOutputStream();
        BufferedInputStream inputstream = new BufferedInputStream(new FileInputStream(downloadFile));
               
        int data;

        byte[] buffer = new byte[4096];
        while((data = inputstream.read(buffer))!=-1)
        {
            outputstream.write(buffer, 0, data);
        }
        inputstream.close();
%>
0
 
LVL 92

Expert Comment

by:objects
ID: 10846665
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maven project import to eclipse problems 13 105
Multiple Images Upload [JSP Web-application] 3 38
excpetion with multiple catch 11 116
mask rule in struts validation.xml 5 87
Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question