본문 바로가기
Web/JSP/Servlet/JDBC

서블릿에서의 Session/ Session 이란 / Session 개념 / Session 예제

by 나비와꽃기린 2016. 6. 22.
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

3. 서블릿에서의 Session

3.1 세션의 개념

-세션범위를 가짐

-java.servlet.http.HttpSession 객체의 한 인스턴스

-요청을 보낸 클라이언트에 대해 생성된 것으로 http 요청에 대해서만 유효함.

-자동적으로 생성됨

-클라이언트가 jsp 페이지가 있는 웹 서버에 request를 하면, 서버는 response를 하여 클라이언트에게 세션을 부여함. à 각각의 클라이언트에게 부여되는 세션은 다르게 할당 됨.

***실습11

loginServlet2.java

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.RequestDispatcher;

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("/in2")

public class LoginServlet2 extends HttpServlet {

             private static final long serialVersionUID = 1L;

 

    public LoginServlet2() {

    }

 

             protected void doGet(HttpServletRequest request, HttpServletResponse response)

                                        throws ServletException, IOException {

                           doProcess(request, response);

                          

             }

            

             protected void doProcess(HttpServletRequest request, HttpServletResponse response)

                                        throws ServletException, IOException {

                          

                           request.setCharacterEncoding("euc-kr"); //설정하는 것이니 set  //인코딩 먼저! -> 하고 결과값 받아와야

                           String id=request.getParameter("id");

                           String passwd=request.getParameter("passwd");

                           response.setContentType("text/html;charset=euc-kr");

                           PrintWriter out=response.getWriter();

                          

                           if(id.equals("jsp")&&passwd.equals("1234")){

                                        HttpSession session=request.getSession();

                                        session.setAttribute("id", id);

                                        //요청을 보내주는 발송자(제어권, 값을 모두 넘기고, 주소는 변경 안됨)

                                        RequestDispatcher dispatcher=request.getRequestDispatcher("loginSuccess.jsp");

                                        dispatcher.forward(request, response);

                               //주소바뀌지 않고 그대로 전송,

                           }else{

                                        out.print("<script type=text/javascript>");

                                        out.print("alert('아이디나 비밀번호가 일치하지 않습니다.');");

                                        out.print("history.back();");

                                        out.print("</script>");

                                       

                           }

             }

            

 

             protected void doPost(HttpServletRequest request, HttpServletResponse response)

                                        throws ServletException, IOException {

                           doProcess(request, response);

             }

 

}

 

Index.html

<frameset cols="30%,*">

       

        <frame src="menu.jsp" name="leftFrame"/> <!-- 왼쪽30% leftFrame -->

        <frame src="login.jsp" name="rightFrame"/>        <!-- 오른쪽70% rightFrame -->

</frameset>

Menu.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<%

        String id=(String)session.getAttribute("id");

        if(id==null){

%>      <a href="login.jsp" target="rightFrame"/>로그인</a>       

<%

        }else{

%>

        <%=id %> 환영합니다.   

<% } %>

Login.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>

<form action="login" method="post">

아이디:<input type="text" name="id"/>

비밀번호:<input type="password" name="passwd"/>

<input type="submit" value="로그인"/>

</form>

</body>

</html>

 

loginSucess.jsp

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

<script type="text/javascript">

        top.leftFrame.location.href="menu.jsp";

</script>

</head>

<body>

<!-- 프레임에서 사용하는 속성들...(예약)

target="_selft"  //현재 창에 띄우겠다

target="_blank"  //빈창에 띄우겠다

target="_parent"  //얘를 호출한 프레임으로 가겠다

target="_top"    //가장 처음으로 가겠다

-->

축하합니다. 로그인 성공~!~!

</body>

</html>

 


'Web > JSP/Servlet/JDBC' 카테고리의 다른 글

JSP지시어란?/include/taglib/예제  (0) 2016.06.22
redirect/forword/include 란 /예제/샘플  (0) 2016.06.22
GET/POST 방식, GET/POST 예제, GET/POST example  (0) 2016.06.22
JSP란 / JSP 예제 / JSP example  (1) 2016.06.22
JSP란?  (0) 2015.01.30