在 Java 应用程序中创建会话

Create a Session in Java Application

我正尝试从某个网站获取 csv 文件,但它要求用户先登录才能下载。

我试过使用 Internet Explorer 登录并使用该会话下载,但没有成功。所以我猜我需要 POST 将用户数据上传到网络,得到答案并下载文件。

如何与 Java 创建会话并下载文件?

提前致谢。

您将需要使用 javax.servlet.http 包。为了使用这个包,你需要下载一个 web servlet 容器,比如 Jetty, Tomcat, or WildFly。以下两个 类 创建和检索会话。您可以将这些作为解决问题的指南。

如果您使用的是 Eclipse,将 Web servlet 添加到您的项目构建路径是相当容易的。 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();
        }
    }
}