본문 바로가기

Java/java 기초

Servlet session 넘기기및 삭제

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<a href="hello">hello Servlet</a>
<br>
<a href="loginId">loginId servlet</a>

</body>
</html>

helloServlet

 

package sample11;

import java.io.IOException;
import java.io.PrintWriter;

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;

@WebServlet("/hello")
public class HelloServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html; charset=utf-8");
		PrintWriter pw = resp.getWriter();

		pw.println("<html>");

		pw.println("<head>");
		pw.println("<title>제목</title>");
		pw.println("</head>");

		pw.println("<body>");
		
		pw.println("<h1>Hello Servlet</h1>");
		
		HttpSession session = req.getSession(false);
		/*
		 * getSession(false)
		 * session Object가 존재하면, 현재 HttpSession을 반환한다.
		 * 존재하지 않으면, 그냥 null을 반환한다.
		 * 
		 * getSession(true) === getSession()
		 * * session Object가 존재하면, 현재 HttpSession을 반환한다.
		 * 존재하지 않으면, 새로 생성한다. 
		 * 
		 */
		if(session == null) {
			//생성 해야하는 경우
			//세션 객체가 있어야한다
			session = req.getSession(true);
			session.setMaxInactiveInterval(30); //	365 * 24 * 60 * 60
			session.setAttribute("visited", "1");
			
			pw.println("<p>첫번째 방문입니다</p>" );
		}else {
			//생성이 되있을경우
			String visited = (String)session.getAttribute("visited");
			int count = Integer.parseInt(visited);
			
			count++;
			
			pw.println("<p>방문회수는 " + count + "회 입니다" + "</p>" );
			session.setAttribute("visited", count + "");
		}
		
		
		pw.println("</body>");

		pw.println("</html>");

		pw.close();	
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
	}
	
	

}

 

 

package sample11;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/loginId")
public class loginIdServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html; charset=utf-8");
		PrintWriter pw = resp.getWriter();
		
		pw.println("<html>");

		pw.println("<head>");
		pw.println("<title>제목</title>");
		pw.println("</head>");

		pw.println("<body>");
		
		pw.println("<h1>Login Id Servlet</h1>");
		
		HttpSession session = null;
		
		//req.getSession().setAttribute(name, value);
		
		if(session == null) {
			pw.println("<p>session을 등록합니다</p>");
			session = req.getSession(true);
			
			session.setAttribute("name", "홍길동");
			session.setAttribute("age", "24");
		}
		
		pw.println("<p>등록되어 있는 session을 표시합니다</p>");
		
		//String name = (String)session.getAttribute("name");
		//pw.println("<p>name:" + name + "</p>");
		//세션에 있는 정보를 무조건 다 볼 수 있음
		
		Enumeration<String> enum_session = session.getAttributeNames();
		
		while(enum_session.hasMoreElements()) {
			String key = enum_session.nextElement();
			String value = (String)session.getAttribute(key);
			pw.println("<p>" + key + ":" + value + "</p>");
		}
		
		pw.println("<a href=delObject>age 삭제</a>");
		pw.println("<br>");
		pw.println("<a href=sessionDel>session 삭제</a>");
		
		pw.println("</body>");

		pw.println("</html>");

		pw.close();	
	}
	
}

 

 

package sample11;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/sessionDel")
public class SessionDelete extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html; charset=utf-8");
		PrintWriter pw = resp.getWriter();
		
		pw.println("<html>");

		pw.println("<head>");
		pw.println("<title>제목</title>");
		pw.println("</head>");

		pw.println("<body>");
		
		pw.println("<h1>SessionDelete</h1>");
		
		HttpSession session = req.getSession();
		
		session.invalidate();	// logout일 경우에 호출
		
		if(req.getSession(false) == null) {
			pw.println("<p>세션이 비어있습니다</p>");
		}
		
		
		pw.println("</body>");

		pw.println("</html>");

		pw.close();	
	}
}	

'Java > java 기초' 카테고리의 다른 글

jsp 기초  (0) 2020.01.20
Servlet 기초 응용  (0) 2020.01.17
Servlet 쿠키값 넘기기  (0) 2020.01.17
servlet a 태그 받아오기  (0) 2020.01.17
자바 JFRAME + JDBC + OracleDB를 활용한 카페 주문 시스템 구현하기  (3) 2019.12.31