WEB/Study

[0401][JSP] 로그인 세션 , 파일 업로드 구현 하기

로그인 세션 , 파일 업로드 구현 하기

HttpSession 객체

■ HTTP 프로토콜은 무상태(stateless) 연결 프로토콜이다.

■ 클라이언트가 서버와 연결을 맺고, 요청을 보낸 뒤, 서버가 요청을 처리한 후

응답을 보내면 클라이언트와 서버 사이의 연결은 끊어진다.

서버에서 클라이언트의 정보를 유지하기 위하여 HttpSession 객체를 사용한다.

■ HttpSession의 동작 원리

 

 

1) 클라이언트의 요청 접수

2) 클라이언트의 정보를 저장 할 HttpSession 객체를 생성, 고유 id가 부여된다.

3) HttpSession 객체 안에 클라이언트의 정보를 저장, key, value 저장.

4) 고유한 id값을 응답에 넣어서 클라이언트에게 보낸다.

5) 클라이언트는 다음 번 요청부터 고유한 id 값을 서버로 전송한다.

 - 서버는 id에 해당하는 HttpSession 객체를 찾아서 요청을 처리한다.

 

 

속성(Attribute)과 스코프(Scope)

■ 속성(Attribute)은 PageContext, HttpServletRequest, HttpSession, ServletContext

객체 중 하나에 설정해놓은(binding해놓은)객체를 말한다.

■ 속성은 이들 객체에 Map 인스턴스와 마찬가지로 이름/값의 쌍으로 저장되어 진다.

■ 각각의 객체에 저장된 속성들은 서로 다른 생존범위(Scope)를 가진다.

 

 


ScopeDemo

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
	// 속성(Attribute)와 스코프(scope) 마무리
	pageContext.setAttribute("pageAtt", "김길동");
	request.setAttribute("reqAtt", "010-1233-5677");
	session.setAttribute("sesAtt", "gildong@naver.com");
	application.setAttribute("appAtt", "kosmo");
	// 직접 해보기 second.jsp로 forward 시켜보기
	System.out.println(pageContext.getAttribute("pageAtt"));
/*  	RequestDispatcher rd = request.getRequestDispatcher("second.jsp");
	rd.forward(request, response);  */
	// 리다이렉트로 보내기
	//response.sendRedirect("second.jsp");
%>
<%-- <jsp:forward page="second.jsp"/> JSP에서 포워드(forward) 보내기 --%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>first.jsp</title>
<%-- <script>
	location = "second.jsp";	// 리다이렉트
</script> --%>
</head>
<body>
	<h2>JSP</h2>
	<a href="second.jsp">이동</a> <%-- 사용자에게 입력 받아서 이동 --%>
	<ul>
		<li>이름 : <%=pageContext.getAttribute("pageAtt") %></li>
		<li>전번 : <%=request.getAttribute("reqAtt") %></li>
		<li>메일 : <%=session.getAttribute("sesAtt") %></li>
		<li>회사 : <%=application.getAttribute("appAtt") %></li>
	</ul>
	<h2>EL</h2>
	<ul>
		<li>이름 : ${pageAtt}</li>
		<li>전번 : ${reqAtt}</li>
		<li>메일 : ${sesAtt}</li>
		<li>회사 : ${appAtt}</li>
	</ul>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>second.jsp</title>
</head>
<body>
<h1>Second.jsp</h1>
	<h2>JSP</h2>
	<ul>
		<li>이름 : <%=pageContext.getAttribute("pageAtt") %></li>
		<li>전번 : <%=request.getAttribute("reqAtt") %></li>
		<li>메일 : <%=session.getAttribute("sesAtt") %></li>
		<li>회사 : <%=application.getAttribute("appAtt") %></li>
	</ul>
	<h2>EL</h2>
	<ul>
		<li>이름 : ${pageScope.pageAtt}</li>
		<li>전번 : ${requestScope.reqAtt}</li>
		<li>메일 : ${sessionScope.sesAtt}</li>
		<li>회사 : ${applicationScope.appAtt}</li>
	</ul>
</body>
</html>

 

멤버전용 게시판 만들기

SQL 문

CREATE TABLE member_board
(
  board_num 		NUMBER 		NOT NULL,
  board_id 		VARCHAR2(50),
  board_subject 		VARCHAR2(100),
  board_content 		VARCHAR2(2000), -- clob : 텍스트 저장 (4만자 가량)
  board_file 		VARCHAR2(100), -- blob : 파일 자체를 저장 (주로 클라에서)
  Board_re_ref 		NUMBER,
  Board_re_lev 		NUMBER,
  Board_re_seq 		NUMBER,
  Board_count 		NUMBER,
  Board_date		DATE,
  BOARD_PARENT     	NUMBER,              
  CONSTRAINT PK_Member_Board PRIMARY KEY(board_num),
  constraint pk_board_id foreign key(board_id)
 REFERENCES MEMBER(id)
);

create sequence member_board_seq
increment by 1
start with 1;

 

VO 만들기

package vo;

public class MemberBoardVO {
	private int board_num;			// 글 번호
	private String board_id;		// 글 작성자
	private String board_subject;	// 글 제목
	private String board_content;	// 글 내용
	private String board_file;		// 첨부파일 이름
	private int board_re_ref;		// 글 그룹번호
	private int board_re_lev;		// 답변글 깊이
	private int board_re_seq;		// 답변글 순서
	private int board_count;		// 글 조회수
	private String board_date;		// 글 작성일
	private int board_parent;		// 부모글 번호
	
	public int getBoard_num() {
		return board_num;
	}
	public void setBoard_num(int board_num) {
		this.board_num = board_num;
	}... // 아래에 모든 멤버 변수의 getter, setter 
}

 

Dao 만들기

public class MyBoardDao {
	private static MyBoardDao dao;

	public MyBoardDao() {}

	public static synchronized MyBoardDao getDao() {
		if (dao == null) dao = new MyBoardDao();
		return dao;
	}
	
	public void boardInsert(MemberBoardVO vo) {
		StringBuffer sb = new StringBuffer();
		sb.append("insert into member_board (board_num, board_id,");
		sb.append("board_subject, board_content, board_file, Board_date)");
		sb.append(" values(member_board_seq.nextVal, ?, ?, ?, ?, sysdate)");
		Connection con = null;
		PreparedStatement pstmt = null;
		try {
			con = MyConn.getDs();
			pstmt = con.prepareStatement(sb.toString());
			pstmt.setString(1, vo.getBoard_id());
			pstmt.setString(2, vo.getBoard_subject());
			pstmt.setString(3, vo.getBoard_content());
			pstmt.setString(4, vo.getBoard_file());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ClosedObj.autoCloseObj(con, pstmt);
		}
	}
}