자바 의 session 과 Cookie 처리

3965 단어
import java.io.File; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.naming.NamingException; import javax.servlet.ServletContext;
import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import java.sql.SQLException; import java.awt.datatransfer.*; import java.util.Date; import java.util.logging.Logger;
// @WebServlet("/Login") public class Login extends HttpServlet { private static final long serialVersionUID = 1L;
/**
 * @see HttpServlet#HttpServlet()
 */
public Login() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //     
    HttpSession session = request.getSession(false);
    if (session == null) {
        System.out.println("    ");
        session = request.getSession(true);
        Date date = new Date();
        long lastdatetime = date.getTime();
        String dateString = String.valueOf(lastdatetime);
        session.setAttribute("lastdatetime", dateString);
    } else {
        System.out.println("   ");
        String lastDateTimeString = (String) session.getAttribute("lastdatetime");
        System.out.println("      :" + lastDateTimeString);
    }
    session.setMaxInactiveInterval(10);

    // Cookie
    Cookie[] sss = request.getCookies();
    if (sss.length > 1) {
        System.out.println("have");
        this.printCookieList(sss, response);
    } else {
        System.out.println("no have");
        Cookie myCookie = new Cookie(URLEncoder.encode("username", "utf-8"), URLEncoder.encode("   ", "utf-8"));
        myCookie.setMaxAge(10);
        response.addCookie(myCookie);
        this.printCookieList(request.getCookies(), response);
    }

    //         
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    //     
    String name = new String(request.getParameter("username").getBytes("ISO8859-1"), "UTF-8");
    String pwd = new String(request.getParameter("pwd").getBytes("ISO8859-1"), "UTF-8");
    out.println("   :" + name + "  :" + pwd);

    ConnectSql connectSql = new ConnectSql();
    try {
        connectSql.connectMysql();
    } catch (ClassNotFoundException | SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ServletContext servletContext = getServletContext();
    out.println("realpath = 
" + servletContext.getRealPath("./") + File.pathSeparator + "nhfc99"); Logger logger = Logger.getLogger("username"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } private void printCookieList(Cookie[] object, HttpServletResponse response) throws UnsupportedEncodingException { for (int i = 0; i < object.length; i++) { Cookie oneCookie = object[i]; System.out.println("cookie name = " + URLDecoder.decode(oneCookie.getName(), "utf-8")); System.out.println("cookie value = " + URLDecoder.decode(oneCookie.getValue(), "utf-8")); } }

}

좋은 웹페이지 즐겨찾기