在Java应用程序中创建会话

时间:2015-01-04 02:24:33

标签: java session login

有一个网站,我正在尝试从中获取csv文件,但它需要用户在下载之前先登录。

我尝试使用Internet Explorer登录并使用该会话下载,但它无效。所以我猜我需要将用户数据发布到网上,得到答案并下载文件。

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

提前谢谢。

1 个答案:

答案 0 :(得分:1)

您需要使用javax.servlet.http包。要使用此包,您需要下载一个Web servlet容器,例如JettyTomcatWildFly。以下两个类创建和检索会话。您可以使用这些作为解决问题的指南。

如果您使用的是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();
        }
    }
}