0

There is this certain website that I'm trying to get a csv file from but it requires a user to login first before downloading.

I've tried logging in using Internet Explorer and download using that session but it didn't work. So I'm guessing I need to POST user data to the web, get an answer and download the file.

How can I create a session wtih Java and download the file?

Thank you in advance.

Vitalij Kornijenko
  • 559
  • 1
  • 10
  • 22

1 Answers1

1

You will need to use the javax.servlet.http package. In order to use this package, you will need to download a web servlet container such as Jetty, Tomcat, or WildFly. The following two classes create and retrieve a session. You can uses these as a guide to solve your issue.

If you are using Eclipse, adding a web servlet to your projects build path is fairly easy. Eclipse: How do I add the javax.servlet package to a project?

CreateLoginSession.java

import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CreateLoginSession extends HttpServlet {
    private static final long serialVersionUID = -881190270020485083L;

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // Get the session if exists or create a new one.
        HttpSession session = request.getSession(true);

        // Set session attributes
        session.setAttribute("username", username);
        session.setAttribute("password", password);

        try {
            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();
            writer.printf("<html><body>Thank you, %s. You are now logged into the system.<br>", username);

            // Encodes the specified URL by including the session ID in it,
            // or, if encoding is not needed, returns the URL unchanged
            String newURL = response.encodeURL("/ServletSession/GetSession");

            // Return a <a> tag with the new url
            writer.printf("Click <a href=\"%s\">here</a> for another servlet</body></html>", newURL);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

GetLoginSession.java

import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class GetLoginSession extends HttpServlet {
    private static final long serialVersionUID = 632668633781294570L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        // Get the session if it exists
        HttpSession session = request.getSession(false);

        try {
            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();
            writer.println("<html><body>");

            // If you are not in a session - you are not logged in
            if (session == null) {
                writer.println("<p>You are not logged in</p>");
            } else {
                writer.println("Thank you, you are already logged in");
                writer.println("Here is the data in your session");

                Enumeration<String> attrs = session.getAttributeNames();

                while (attrs.hasMoreElements()) {
                    String name = attrs.nextElement();
                    Object value = session.getAttribute(name);
                    writer.printf("<p>name=\"%s\" value=\"%s\"</p>", name, value);
                }
            }
            // Write html for a new login
            writer.println("<p><a href=\"/ServletSession/login.html\">Return</a> to login page</p></body></html>");
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Community
  • 1
  • 1
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132