Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

use passworded jsp page to get files

Posted on 2004-04-15
6
247 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
grep code 4 199
Can't access image with tomcat/html 6 102
I get error: useBean: Duplicate bean name: {0} 1 148
designing in object programming 12 94
The business world is becoming increasingly integrated with tech. It’s not just for a select few anymore — but what about if you have a small business? It may be easier than you think to integrate technology into your small business, and it’s likely…
There's a lot of hype surrounding blockchain technology. Here's how it works and some of the novel ways it' s now being used - including for data protection.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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