Session은 서버가 가지고있다. 접속을 하면 그 피시에 정보가 있는데
그 정보를 가지고 Session을 만들어 놓는다.
결론적으로 접속한 여러 사람들의 정보를 가지게 된다.
cookie 는 서버가 가지지 않고 client가 가지고 있다.
하지만 만드는것은 서버에 접속을 하면 그정보를 잘 담아서 생성을 해서 Client에게 줘버리고
다음에 접속할때 cookie를 요구하게된다.
cookie는 생명력이 있다. (최소 3개월)
결과화면
login.html
<!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>
<style>
th{
background-color:#cdcdcd;
font-size:9pt;
text-align:rigth;
}
td{
font-size:9pt;
}
input{
border:1px solid;
font-size:9pt;
}
</style>
<body>
<pre>
</pre>
<center>
<form action="LoginServlet" method="post">
<table style="border-collapse:collapse" border="1"
bordercolor="black" cellspacing="0" cellpadding="3" width="240">
<tr>
<th width="80">아이디 :</th>
<td><input name="id" size="9"></td>
</tr>
<tr>
<th>패스워드 :</th>
<td><input name="pwd" type="password" size="9"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="login">
<input type = "reset">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
LoginServlet.java
package member;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
public class LoginServlet extends javax.servlet.http.HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//HTML문서로 부터 파라메터를 받기 위한 작업
//입력란에서 한글을 입력하지 않는다는 확신이 있다면
//request.setCharacterEncoding("euc-kr");를 안해도 된다.
String s_id = request.getParameter("id");
String s_pwd = request.getParameter("pwd");
//db에서 인증절차를 하기윈한 준비 작업
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
response.setContentType("text/html;charset=euc-kr");
PrintWriter out = response.getWriter();
try {
InitialContext ic = new InitialContext();
Context ctx = (Context) ic.lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup("jdbc/oracle");
con = ds.getConnection();
pstmt = con.prepareStatement("SELECT * FROM loginTest_T WHERE id=? and pwd=?");
if(s_id != null && s_id.trim().length() > 0 && s_pwd != null && s_pwd.trim().length() > 0){
pstmt.setString(1, s_id.trim());
pstmt.setString(2, s_pwd.trim());
rs = pstmt.executeQuery();
//sql문장을 수행 했을 때 결과는 1개의 record 또는 비어잇는 ResultSet를 받게 된다.
if(rs.next()){ //1개 일때 사용
//인증된 자원일 경우 - 세션 처리
String s_name = rs.getString("name");
//db이라는 컬럼에 있는 자원을 가져와서 s_name이라는 변수에 대입한다.
//세션 객체를 만들고, 속성을 설정
HttpSession ss = request.getSession();
ss.setAttribute("id", s_id); //id라는 속성명으로
//s_id가 기억하고 있는 값을 속성값으로 설정
ss.setAttribute("name", s_name);
out.println("<body>");
out.println(s_name + "환영합니다.");
out.println("<input type='button' value='이동' style='cursor:hand' onClick='location.href=\"LoginServlet1\"'>");
}else{
out.println("id 또는 password가 틀렸습니다.");
out.println("<input type='button' value='돌아가기' onClick='history.back(-1);'>");
}
}else{
}
} catch (Exception e) {
// TODO: handle exception
}finally{
out.close();
try {
if(rs != null){
rs.close();
}
if(pstmt != null){
pstmt.close();
}
if(con != null){
con.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
LoginServlet1.java
package member;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet1 extends javax.servlet.http.HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 현재 서블릿은 로그인 작업을 거친 후, 수행되는 서블릿이다.
// 즉, 세션 처리 후가 된다.
// 세션 구하기
HttpSession hs = request.getSession();
String s1 = (String)hs.getAttribute("id"); //id라는 속성의 이름으로
//등록된 속성의 값을 s1에 대입한다.
String s2 = (String)hs.getAttribute("name");
response.setContentType("text/html;charset=euc-kr");
PrintWriter out = response.getWriter();
out.println(s1+"(<font color=blue><b>"+s2+
"</b></font>)님 LoginServlet2에 오신걸 환영합니다.");
out.close();
}
}
[출처] Servlet - Login|작성자 비틀박
'Programing > JSP' 카테고리의 다른 글
JSP 에러관리 로그파일로 생성하기 (0) | 2008.11.19 |
---|---|
Servlet - GET과 POST 요청을 위한 HTML작성 (0) | 2008.07.29 |
Servlet - JDBC활용 (0) | 2008.07.29 |
Servlet - 공지파일 예제 (0) | 2008.07.29 |
Servlet - WAS 구동과정 및 Servlet의 생명 주기 (1) | 2008.07.29 |