세션ID값은 처음부터 (최초에 요청했을 때) 만들어진다.
세션에는 user오브젝트 등을 저장한다. (변수를 저장하는게 아니라 값들을 묶는다는 의미)
이름 저장
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;
/**
* Servlet implementation class SetNameServlet
*/
@WebServlet("/SetNameServlet")
public class SetNameServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SetNameServlet() {
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();
session.setAttribute("name", "오정원");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<h1>이름저장</h1>");
}
}
이름 가져오기
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;
/**
* Servlet implementation class GetNameServlet
*/
@WebServlet("/GetNameServlet")
public class GetNameServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GetNameServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
String name = (String)session.getAttribute("name");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<h1>name="+name+"</h1>");
}
}
'Web > Jsp' 카테고리의 다른 글
200527 (0) | 2020.05.27 |
---|---|
서블릿 특정 페이지 포워딩(이동) 방식 (0) | 2020.05.26 |
서블릿 / 하나의 파라미터 이름으로 여러 개의 파라미터 값이 전송 되어 올 경우 처리하기 (0) | 2020.05.26 |
Postman 프로그램을 이용한 서블릿 테스트 (0) | 2020.05.26 |
MIME 타입 (0) | 2020.05.26 |