2013년 11월 3일 일요일

[서블릿강좌]JavaServlet,자바서블릿인증/로그인예제

 [서블릿강좌]JavaServlet,자바서블릿인증/로그인예제

[SessionTest.java]

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class SessionTest extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    res.setContentType("text/html; charset=euc-kr");     
    PrintWriter out = res.getWriter();   
    String szID="";
    String szPasswd= "";
    String n=null;   
    HttpSession session = req.getSession(false);                            
    // 이미 세션이 Setting되어 있는지 확인,인증되어 있다면 인증박스가 없는 html 표시, 아니면 인증창이 있는 htmnl 표시
    if (session != null) {
      szID = (String) session.getValue("id");
           
      szPasswd =(String) session.getValue("passwd");
      out.println("<html><head><title>”);
      out.println(“Session Test Program”);
      out.println(“</title></head>");
      out.println("<body><table width=500><tr>");
      out.println("<form method=get action=/servlet/SessionExpire name=logout >");
      out.println("Id : "+szID + "    Passwd : "+szPasswd); 
      out.println("<input type=submit name=Submit value=LogOut></tr><tr></form><hr>");
      out.println("This is Session sample program<p>");
      out.println("[11/05]<a href=/servlet/SessionNewsDetail>”);
      out.println(“오엔제이프로그래밍실무교육센터 실무교육을 이끌다!!</a>");
      out.println("</tr></table>”);
      out.println(“</body>
      out.println(“</html>");              
    }
   
else {
      out.println("<html><head>”);
     out.println(“<title>Session Test Program</title></head>");
      out.println("<body>”);
      out.println(“<table width=500><tr>");
      out.println("<form method=post action=/servlet/SessionIsOk name=login >              ");
      out.println("<input type=text size=10  maxlength=40 name=id>");
      out.println("PassWord : ");
      out.println("<input type=password size=10  maxlength=40 name=passwd>");
      out.println("<input type=submit name=Submit value=OK>");
      out.println("</form></tr><hr>");
      out.println("This is Session sample program<p></tr>”);
      out.println(“<tr>");
      out.println(“[11/05]<a href=/servlet/SessionNewsDetail>오엔제이프로그래밍실무교육센터 실무교육을 이끌다!!</a>");
      out.println("</td></tr></table></body></html>");
    }                                       
  }   
 

[SessionIsOk.java]

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class SessionIsOk extends HttpServlet {
  public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    res.setContentType("text/html; charset=euc-kr"); 
    PrintWriter out = res.getWriter();   
    String szID=null;
    String szPasswd=null;    
    if (req.getParameter("id") != null)
        szID = req.getParameter("id");
    if (req.getParameter("passwd") != null)
        szPasswd = req.getParameter("passwd");               
    // 인증이 성공이라면, 흔히 이부분의 인증을 DB의 사용자 Table등의 ID/PassWd를
   // 비교하여 인증이 성공인지의 여부를 판단한다.
   //본예제의 경우 DB를 연겨랗지않고 ID가 onjoraclejava 이면 OK라고 하자
    if ((szID.equals(“onjoraclejava")) &&
            
      (szPasswd.equals("onjoraclejava"))) {
      HttpSession session = req.getSession(true);
      session.putValue("id", szID);
      session.putValue("passwd", szPasswd);          
      out.println("<html><head>");      
   res.setHeader("Refresh","3; URL=http://localhost/servlet/SessionTest");
   out.println("Session Setting OK!! <br>");
      out.println("3초후면 첫페이지로 이동합니다.");
      out.println("</head></html>");    
    }
 // Id와 PassWord가 맞지 않는 경우
    else {
      out.println("<html><head>");
     res.setHeader("Refresh","3; URL=http://localhost/servlet/SessionTest");
   out.println("ID PassWord not correct...<br>");
      out.println("3초후면 첫페이지로 이동합니다.");     
      out.println("</html></head>");      
    }                                       
  }
}
 
[SessionExpire.java]

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class SessionExpire extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {          
    HttpSession session = req.getSession(false);   
    if (session != null)  session.invalidate();
      
    res.setContentType("text/html; charset=euc-kr");
    res.sendRedirect("/servlet/SessionTest");
  }
}
 

[SessionNewsDetail.java]

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class SessionNewsDetail extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    res.setContentType("text/html; charset=euc-kr");     
    PrintWriter out = res.getWriter();   
    String szID="";
    String szPasswd= "";
    String n=null;  
    HttpSession session = req.getSession(false);   
    //이미 인증이 되어 있는 경우라면
 if (session != null) {
       out.println("<html><head>< title>Cookie Test Program</title>");
      out.println("</head>");
       out.println("<body>");
       out.println("<table>");
out.println("<tr><td>");
       out.println("<center>");
       out.println("<b>실무 개발자 교육 : 오엔제이프로그래밍");
       out.println("</b><hr>");
       out.println("입력일 : 2013/11/05 <hr></center>");
      out.println(“onjprogramming.co.kr<br>");
       out.println("<p>");      
       out.println("</body></html>");
    }
    else {
       res.setHeader("Refresh","3; URL=http://localhost/servlet/SessionTest");
       out.println("첫페이로 가서 인증을 해주시기 바랍니다.<br>");
       out.println("3초후면 첫페이지로 이동합니다.");
    }  
  }
}

댓글 없음:

댓글 쓰기