Solved

use passworded jsp page to get files

Posted on 2004-04-15
6
244 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mockito Method call failing. 3 187
logging jar 1 106
eclipse problems view not showing all the time 4 87
maven project jars 7 50
If your vDisk VHD file gets deleted from the image store accidentally or on purpose, you won't be able to remove the vDisk from the PVS console. There is a known workaround that is solid.
In 2017, ransomware will become so virulent and widespread that if you aren’t a victim yourself, you will know someone who is.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now