//Struts2 : MVC모델제공 => FrameWork
모델1, 모델2 DAO 나 자바빈은 변하지않아 재활용가능
예제. MEMBER (회원관리)
회원가입,아이디중복확인,로그인,정보수정,회원탈퇴(회원삭제)
Java Resources
src
com.action
ConfirmIdAction
package com.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.controller.Action; import com.dao.MemberDao; public class ConfirmIdAction implements Action{ @Override public String execute(HttpServletRequest request,HttpServletResponse response) throws Throwable { String id =request.getParameter("id"); MemberDao manager = MemberDao.getInstance(); int check=manager.confirmId(id); request.setAttribute("check", new Integer(check)); request.setAttribute("id", id); return "/view2/confirmId.jsp"; } }
DeleteFormAction
package com.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.controller.Action; public class DeleteFormAction implements Action{ @Override public String execute(HttpServletRequest request,HttpServletResponse response) throws Throwable { HttpSession session = request.getSession(); if(session.getAttribute("memId") == null) return "/view2/loginForm.jsp"; return "/view2/deleteForm.jsp"; } }
DeleteProAction
package com.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.controller.Action; import com.dao.MemberDao; public class DeleteProAction implements Action{ @Override public String execute(HttpServletRequest request,HttpServletResponse response) throws Throwable { request.setCharacterEncoding("utf-8"); //1. 로그인 여부 체크 HttpSession session =request.getSession(); if(session.getAttribute("memId")==null) return"/view2/loginForm.jsp"; //2. session에서 id를 추출해고 전송받은 비밀번호를 String id =(String)session.getAttribute("memId"); String passwd = request.getParameter("passwd"); // memberDao의 userCheck 에 전달 MemberDao manager = MemberDao.getInstance(); int check = manager.userCheck(id, passwd); //3. check 1인 경우 deleteMember 메소드에 id 전달 if(check== 1){ manager.deleteMember(id); session.invalidate(); } //4. check를 request에 저장 request.setAttribute("check", check); return "/view2/deletePro.jsp"; } }
InputFormAction
package com.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.controller.Action; public class InputFormAction implements Action{ @Override public String execute(HttpServletRequest request,HttpServletResponse response) throws Throwable { return "/view2/inputForm.jsp"; } }
InputProAction
package com.action; import java.sql.Timestamp; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.controller.Action; import com.dao.MemberDao; import com.domain.Member; public class InputProAction implements Action{ @Override public String execute(HttpServletRequest request,HttpServletResponse response) throws Throwable { //1.인코딩처리 request.setCharacterEncoding("utf-8"); //2.request에서 전송된 데이터를 추출 //3.자바빈에 데이터 셋팅 Member member =new Member(); member.setId(request.getParameter("id")); member.setName(request.getParameter("name")); member.setPasswd(request.getParameter("passwd")); member.setJumin1(request.getParameter("jumin1")); member.setJumin2(request.getParameter("jumin2")); member.setEmail(request.getParameter("email")); member.setBlog(request.getParameter("blog")); member.setReg_date(new Timestamp(System.currentTimeMillis())); //4.MemberDao의 insertMember 메소드 호출 MemberDao manager = MemberDao.getInstance(); manager.insertMember(member); return "/view2/inputPro.jsp"; } }
LoginAction
package com.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.controller.Action; public class LoginAction implements Action{ @Override public String execute(HttpServletRequest request,HttpServletResponse response) throws Throwable { HttpSession session =request.getSession(); if(session.getAttribute("memId")==null) return "/view2/loginForm.jsp"; return "/view2/login.jsp"; } }
LoginFormAction
package com.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.controller.Action; public class LoginFormAction implements Action{ @Override public String execute(HttpServletRequest request,HttpServletResponse response) throws Throwable { return "/view2/loginForm.jsp"; } }
LoginProAction
package com.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.controller.Action; import com.dao.MemberDao; public class LoginProAction implements Action{ @Override public String execute(HttpServletRequest request,HttpServletResponse response) throws Throwable { String id =request.getParameter("id"); String passwd =request.getParameter("passwd"); MemberDao manager = MemberDao.getInstance(); int check= manager.userCheck(id, passwd); if(check==1){ HttpSession session=request.getSession(); session.setAttribute("memId", id); } request.setAttribute("check", new Integer(check)); return "/view2/loginPro.jsp"; } }
LogoutAction
package com.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.controller.Action; public class LogoutAction implements Action{ @Override public String execute(HttpServletRequest request,HttpServletResponse response) throws Throwable { HttpSession session =request.getSession(); session.invalidate(); return "view2/logout.jsp"; } }
ModifyFormAction
package com.action; import java.sql.Timestamp; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.controller.Action; import com.dao.MemberDao; import com.domain.Member; public class ModifyFormAction implements Action{ @Override public String execute(HttpServletRequest request,HttpServletResponse response) throws Throwable { //1. 로그인 여부 조사 HttpSession session =request.getSession(); if(session.getAttribute("memId")==null) return"/view/loginForm.jsp"; //2.session에 저장된 id 호출 String id = (String)session.getAttribute("memId"); //3.MemberDao의 getMember 메서드에 id 전달 MemberDao manager = MemberDao.getInstance(); Member member = manager.getMember(id); //4.반환받은 Member를 request에 저장 request.setAttribute("member", member); return "/view2/modifyForm.jsp"; } }
ModifyProAction
package com.action; import java.sql.Timestamp; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.controller.Action; import com.dao.MemberDao; import com.domain.Member; public class ModifyProAction implements Action{ @Override public String execute(HttpServletRequest request,HttpServletResponse response) throws Throwable { //1.로그인 여부 조사 HttpSession session =request.getSession(); if(session.getAttribute("memId")==null) return"/view/loginForm.jsp"; //2.인코딩처리 request.setCharacterEncoding("utf-8"); //3.자바빈을 생성하고 자바빈에 전송된 데이터를 저장 Member member =new Member(); member.setId((String)session.getAttribute("memId")); member.setName(request.getParameter("name")); member.setPasswd(request.getParameter("passwd")); member.setJumin1(request.getParameter("jumin1")); member.setJumin2(request.getParameter("jumin2")); member.setEmail(request.getParameter("email")); member.setBlog(request.getParameter("blog")); member.setReg_date(new Timestamp(System.currentTimeMillis())); //4.MemberDao의 updateMember 메소드에 자바빈 전달 MemberDao manager = MemberDao.getInstance(); manager.updateMember(member); return "/view2/modifyPro.jsp"; } }
com.controller
Action
package com.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //요청 파라미터로 명령어를 전달하는 방식의 슈퍼 인터페이스 public interface Action { public String execute(HttpServletRequest request,HttpServletResponse response)throws Throwable; }
Controller
package com.controller; import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Controller extends HttpServlet { private Map commandMap = new HashMap();//명령어와 명령어 처리 클래스를 쌍으로 저장 //명령어와 처리클래스가 매핑되어 있는 properties 파일을 읽어서 Map객체인 commandMap에 저장 //명령어와 처리클래스가 매핑되어 있는 properties 파일은 Command.properties파일 public void init(ServletConfig config) throws ServletException { String configFile = config.getInitParameter("configFile"); Properties prop = new Properties(); FileInputStream fis = null; try { String configFilePath = config.getServletContext().getRealPath( configFile); fis = new FileInputStream(configFilePath); prop.load(fis); } catch (IOException e) { throw new ServletException(e);ㅠ } finally { if (fis != null) try { fis.close(); } catch (IOException ex) { } } Iterator keyIter = prop.keySet().iterator(); while (keyIter.hasNext()) { String command = (String) keyIter.next(); String handlerClassName = prop.getProperty(command); try { Class handlerClass = Class.forName(handlerClassName); Object handlerInstance = handlerClass.newInstance(); commandMap.put(command, handlerInstance); } catch (ClassNotFoundException e) { throw new ServletException(e); } catch (InstantiationException e) { throw new ServletException(e); } catch (IllegalAccessException e) { throw new ServletException(e); } } } public void doGet(//get방식의 서비스 메소드 HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { requestPro(request, response); } protected void doPost(//post방식의 서비스 메소드 HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { requestPro(request, response); } //시용자의 요청을 분석해서 해당 작업을 처리 private void requestPro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String view = null; Action com=null; try { String command = request.getRequestURI(); if (command.indexOf(request.getContextPath()) == 0) { command = command.substring(request.getContextPath().length()); } com = (Action)commandMap.get(command); view = com.execute(request, response); } catch(Throwable e) { throw new ServletException(e); } RequestDispatcher dispatcher =request.getRequestDispatcher(view); dispatcher.forward(request, response); } }
com.dao
MemberDao
package com.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; import javax.naming.Context; import javax.naming.InitialContext; import com.domain.Member; public class MemberDao { /* @싱글턴 패턴 생성자의 접근지정자는 private으로 지정 static한 getInstance()를 메소드를 사용 객체의 주소를 보관하는 static 참조변수 사용 //참조변수 instance에 객체 주소할당 //객체를 한번생성해서 계속가지고있슴 (변경에관한게 아무것도없슴) 객체를 하나만 생성해서 공유하고자 싱글턴 패턴 구현 @멤버변수가 있는 경우에는 절대로 싱글턴 패턴을 구현하면 안된다. //멤버변수를 공유시켜버리면 여러사용자가 멤버변수를 같이 변경하게됨 */ private static MemberDao instance =new MemberDao(); public static MemberDao getInstance(){ return instance; } //생성자 인데 private 외부에서는 생성못하게 막아둔거 //메소드를 static 하게만들어 메서드를 이용해서 //Member.getInstance(); private MemberDao(){ } //커네션풀로 부터 커넥션을 할당 받는 메소드 private Connection getConnection() throws Exception{ Context initCtx= new InitialContext(); Context envCtx=(Context)initCtx.lookup("java:comp/env"); DataSource ds=(DataSource)envCtx.lookup("jdbc/orcl"); return ds.getConnection(); } //회원가입 public void insertMember(Member member)throws Exception{ Connection conn= null; PreparedStatement pstmt = null; String sql=""; int cnt = 0; try{ //커넥션 풀로 부터 커넥션 할당 conn= getConnection(); sql ="insert into MEMBER values(?,?,?,?,?,?,?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(++cnt, member.getId()); pstmt.setString(++cnt, member.getPasswd()); pstmt.setString(++cnt, member.getName()); pstmt.setString(++cnt, member.getJumin1()); pstmt.setString(++cnt, member.getJumin2()); pstmt.setString(++cnt, member.getEmail()); pstmt.setString(++cnt, member.getBlog()); pstmt.setTimestamp(++cnt, member.getReg_date()); pstmt.executeUpdate(); }catch(Exception e){ e.printStackTrace(); }finally{ execClose(null,pstmt,conn); } } //회원 아이디 , 비밀번호 체크 public int userCheck(String id, String passwd)throws Exception{ Connection conn= null; PreparedStatement pstmt = null; ResultSet rs =null; String sql=""; String dbpasswd=""; int x = -1; try{ conn =getConnection(); sql ="select passwd from MEMBER where id = ?"; pstmt =conn.prepareStatement(sql); pstmt.setString(1, id); rs=pstmt.executeQuery(); if(rs.next()){ dbpasswd =rs.getString("passwd"); if(dbpasswd.equals(passwd)) x=1; //인증성공 else x=0; //비밀번호 틀림 }else x=-1; //해당 아이디 없음 }catch(Exception e){ e.printStackTrace(); }finally{ execClose(rs,pstmt,conn); } return x; } //회원 상세정보 public Member getMember(String id)throws Exception{ Connection conn =null; PreparedStatement pstmt = null; ResultSet rs = null; Member member=null; String sql=""; try{ conn=getConnection(); sql="select * from MEMBER where id= ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, id); rs= pstmt.executeQuery(); if(rs.next()){ member=new Member(); member.setId(rs.getString("id")); member.setPasswd(rs.getString("passwd")); member.setName(rs.getString("name")); member.setJumin1(rs.getString("jumin1")); member.setJumin2(rs.getString("jumin2")); member.setEmail(rs.getString("email")); member.setBlog(rs.getString("blog")); member.setReg_date(rs.getTimestamp("reg_date")); } }catch(Exception ex){ ex.printStackTrace(); }finally{ execClose(rs,pstmt,conn); } return member; } //회원정보 수정 public void updateMember(Member member)throws Exception{ Connection conn=null; PreparedStatement pstmt =null; String sql = null; int cnt =0; try{ conn =getConnection(); sql = "update MEMBER set passwd=?,name=?,email=?, blog=? where id=?"; pstmt =conn.prepareStatement(sql); pstmt.setString(++cnt, member.getPasswd()); pstmt.setString(++cnt, member.getName()); pstmt.setString(++cnt, member.getEmail()); pstmt.setString(++cnt, member.getBlog()); pstmt.setString(++cnt, member.getId()); pstmt.executeUpdate(); }catch(Exception ex){ ex.printStackTrace(); }finally{ execClose(null,pstmt,conn); } } //회원탈퇴 , 회원정보 삭제 public void deleteMember(String id)throws Exception{ Connection conn=null; PreparedStatement pstmt =null; String sql = null; try{ conn =getConnection(); sql = "delete from MEMBER where id=?"; pstmt =conn.prepareStatement(sql); pstmt.setString(1, id); pstmt.executeUpdate(); }catch(Exception e){ e.printStackTrace(); }finally{ execClose(null,pstmt,conn); } } //ID 중복 체크 public int confirmId(String id)throws Exception{ Connection conn =null; PreparedStatement pstmt = null; ResultSet rs = null; String sql=""; int x=-1; try{ conn=getConnection(); sql="select * from MEMBER where id= ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, id); rs= pstmt.executeQuery(); if(rs.next()) x=1; //해당아이디 있음 else x=-1;//해당아이디 없음 }catch(Exception ex){ ex.printStackTrace(); }finally{ execClose(rs,pstmt,conn); } return x; } //자원 정리를 위한 메소드 //계란노른자 //Connection 를통해서 PreparedStatement 를생성하고 //PreparedStatement 를 통해서 ResultSet 를 생성하기때문에 //종료할때는 ResultSet=>PreparedStatement=>Connection 와같이 생성순서의 역순으로 close 해줘야한다 public void execClose(ResultSet rs, PreparedStatement pstmt, Connection conn)throws Exception{ //자원정리 if(rs !=null) try{rs.close();}catch(SQLException sqle){} if(pstmt !=null) try{pstmt.close();}catch(SQLException sqle){} //커넥션 풀로 반납 if(conn !=null) try{conn.close();}catch(SQLException sqle){} } }
com.doamin
Member
package com.domain; import java.sql.Timestamp; //자바빈 public class Member { //프로퍼티 private String id; private String passwd; private String name; private String jumin1; private String jumin2; private String email; private String blog; private Timestamp reg_date; //getter public String getId() { return id; } //setter public void setId(String id) { this.id = id; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getJumin1() { return jumin1; } public void setJumin1(String jumin1) { this.jumin1 = jumin1; } public String getJumin2() { return jumin2; } public void setJumin2(String jumin2) { this.jumin2 = jumin2; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getBlog() { return blog; } public void setBlog(String blog) { this.blog = blog; } public Timestamp getReg_date() { return reg_date; } public void setReg_date(Timestamp reg_date) { this.reg_date = reg_date; } @Override public String toString() { return "Member [id=" + id + ", passwd=" + passwd + ", name=" + name + ", jumin1=" + jumin1 + ", jumin2=" + jumin2 + ", email=" + email + ", blog=" + blog + ", reg_date=" + reg_date + "]"; } }
WebContent
view2
confirmId.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head><title>ID 중복확인</title> <script type="text/javascript"> <!-- function setid() { opener.document.userinput.id.value="${id}"; self.close(); } //--> </script> </head> <body> <c:if test="${check == 1}"> <table width="270" border="0" cellspacing="0" cellpadding="5"> <tr> <td height="39" >${id }이미 사용중인 아이디입니다.</td> </tr> </table> <form name="checkForm" method="post" action="confirmId.do"> <table width="270" border="0" cellspacing="0" cellpadding="5"> <tr> <td align="center"> 다른 아이디를 선택하세요.<p> <input type="text" size="10" maxlength="12" name="id"> <input type="submit" value="ID중복확인"> </td> </tr> </table> </form> </c:if> <c:if test="${check != 1}"> <table width="270" border="0" cellspacing="0" cellpadding="5"> <tr> <td align="center"> <p>입력하신 ${id} 는 사용하실 수 있는 ID입니다. </p> <input type="button" value="닫기" onclick="setid()"> </td> </tr> </table> </c:if> </body> </html>
deleteForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head><title>회원탈퇴</title> <link href="style.css" rel="stylesheet" type="text/css"> <script type="text/javascript"> <!-- function begin(){ document.myform.passwd.focus(); } function checkIt(){ if(!document.myform.passwd.value){ alert("비밀번호를 입력하지 않으셨습니다."); document.myform.passwd.focus(); return false; } return true; } //--> </script> </head> <BODY onload="begin()"> <form name="myform" action="deletePro.do" method="post" onSubmit="return checkIt()"> <table cellspacing=1 cellpadding=1 width="260" border=1 align="center" > <tr height="30"> <td colspan="2" align="center"> <font size="+1" ><b>회원 탈퇴</b></font></td></tr> <tr height="30"> <td width="110" align=center>비밀번호</td> <td width="150" align=center> <input type=password name="passwd" size="15" maxlength="12"></td></tr> <tr height="30"> <td colspan="2" align="center"> <input type=submit value="회원탈퇴"> <input type="button" value="취 소" onclick="location.href='login.do'"></TD></TR> </TABLE> </form> </BODY> </HTML>
deletePro.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!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=UTF-8"> <title>회원탈퇴</title> </head> <body> <c:if test="${check == 1}"> 회원정보가 삭제되었습니다. <input type="button" value="확인" onclick="location.href='loginForm.do'"> </c:if> <c:if test="${check != 1}"> <script> alert("비밀번호가 맞지 않습니다."); history(-1); </script> </c:if> </body> </html>
inputForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>회원가입</title> <script type="text/javaScript"> function checkIt() { var userinput = document.userinput; if(!userinput.id.value) { alert("ID를 입력하세요"); userinput.id.focus(); return false; } if(!userinput.passwd.value ) { alert("비밀번호를 입력하세요"); userinput.passwd.focus(); return false; } if(userinput.passwd.value != userinput.passwd2.value){ alert("비밀번호를 동일하게 입력하세요"); userinput.passwd2.focus(); return false; } if(!userinput.name.value) { alert("사용자 이름을 입력하세요"); userinput.name.focus(); return false; } if(!userinput.jumin1.value) { alert("주민등록번호를 입력하세요"); userinput.jumin1.focus(); return false; } if(!userinput.jumin2.value) { alert("주민등록번호를 입력하세요"); userinput.jumin2.focus(); return false; } } // 아이디 중복 여부를 판단 function openConfirmid(userinput) { // 아이디를 입력했는지 검사 if (userinput.id.value == "") { alert("아이디를 입력하세요"); return; } // url과 사용자 입력 id를 조합합니다. url = "confirmId.do?id=" + userinput.id.value ; // 새로운 윈도우를 엽니다. open(url, "confirm", "toolbar=no, location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=300, height=200"); } //주민번호 6자리 입력 후 자동으로 다음 칸으로 이동 function nextField(userinput,checklength){ if( userinput.jumin1.value.length >= checklength ) { userinput.jumin2.focus(); } return true; } </script> </head> <body> <form method="post" action="inputPro.do" name="userinput" onSubmit="return checkIt()"> <table width="600" border="1" cellspacing="0" cellpadding="3" align="center" > <tr> <td colspan="2" height="39" align="center"> <font size="+1" ><b>회원가입</b></font></td> </tr> <tr> <td width="200"><b>아이디 입력</b></td> <td width="400"> </td> </tr> <tr> <td width="200"> 사용자 ID</td> <td width="400"> <input type="text" name="id" size="10" maxlength="12"> <input type="button" name="confirm_id" value="ID중복확인" OnClick="openConfirmid(this.form)"> </td> </tr> <tr> <td width="200"> 비밀번호</td> <td width="400" > <input type="password" name="passwd" size="15" maxlength="12"> </td> </tr> <tr> <td width="200">비밀번호 확인</td> <td width="400"> <input type="password" name="passwd2" size="15" maxlength="12"> </td> </tr> <tr> <td width="200"><b>개인정보 입력</b></td> <td width="400"> </td> </tr> <tr> <td width="200">사용자 이름</td> <td width="400"> <input type="text" name="name" size="15" maxlength="10"> </td> </tr> <tr> <td width="200">주민등록번호</td> <td width="400"> <input type="text" name="jumin1" onKeyUp="nextField(this.form,6);" size="7" maxlength="6"> -<input type="text" name="jumin2" size="7" maxlength="7"> </td> </tr> <tr> <td width="200">E-Mail</td> <td width="400"> <input type="text" name="email" size="40" maxlength="30"> </td> </tr> <tr> <td width="200"> Blog</td> <td width="400"> <input type="text" name="blog" size="60" maxlength="50"> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="confirm" value="등 록" > <input type="reset" name="reset" value="다시입력"> <input type="button" value="가입안함" onclick="javascript:window.location='login.do'"> </td> </tr> </table> </form> </body> </html>
inputPro.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <script> alert("축하! 축하!"); location.href="login.do" </script>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head><title>메인입니다..</title> </head> <body> <table width=500 cellpadding="0" cellspacing="0" align="center" border="1" > <tr> <td width="300" height="20">하하하</td> <td rowspan="3" align="center"> ${sessionScope.memId}님이 <br> 방문하셨습니다 <form method="post" action="logout.do"> <input type="submit" value="로그아웃"> <input type="button" value="회원정보변경" onclick="javascript:window.location='modifyForm.do'"> <input type="button" value="회원탈퇴" onclick="javascript:window.location='deleteForm.do'"> </form> </td> </tr> <tr> <td rowspan="2" width="300" >메인입니다.</td> </tr> </table> <br> </body> </html>
loginForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head><title>로그인</title> <script type="text/javascript"> <!-- function begin(){ document.myform.id.focus(); } function checkIt(){ if(!document.myform.id.value){ alert("이름을 입력하지 않으셨습니다."); document.myform.id.focus(); return false; } if(!document.myform.passwd.value){ alert("비밀번호를 입력하지 않으셨습니다."); document.myform.passwd.focus(); return false; } return true; } //--> </script> </head> <BODY onload="begin()"> <form name="myform" action="loginPro.do" method="post" onSubmit="return checkIt()"> <table cellspacing=1 cellpadding=1 width="260" border=1 align="center" > <tr height="30"> <td colspan="2" align="center"><strong>회원로그인</strong></td></tr> <tr height="30"> <td width="110" align=center>아이디</td> <td width="150" align=center> <input type="text" name="id" size="15" maxlength="12"></td></tr> <tr height="30"> <td width="110" align=center>비밀번호</td> <td width="150" align=center> <input type=password name="passwd" size="15" maxlength="12"></td></tr> <tr height="30"> <td colspan="2" align="center"> <input type=submit value="로그인"> <input type="button" value="회원가입" onclick="location.href='inputForm.do'"></td></tr> </table> </form> </body> </html>
loginPro.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:if test="${check == 1}"> <script> location.href="login.do"; </script> </c:if> <c:if test="${check == 0}"> <script> alert("비밀번호가 맞지 않습니다."); history.go(-1); </script> </c:if> <c:if test="${check == -1}"> <script> alert("아이디가 맞지 않습니다."); history.go(-1); </script> </c:if>
logout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% response.sendRedirect("loginForm.do"); %>
modifyForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>회원정보수정</title> <link href="style.css" rel="stylesheet" type="text/css"> <script type="text/javaScript"> <!-- function checkIt() { var userinput = document.userinput; if(!userinput.passwd.value ) { alert("비밀번호를 입력하세요"); return false; } if(!userinput.name.value) { alert("사용자 이름을 입력하세요"); return false; } return true; } //--> </script> </head> <body> <form method="post" action="modifyPro.do" name="userinput" onsubmit="return checkIt()"> <table width="600" border="1" cellspacing="0" cellpadding="3" align="center"> <tr> <td colspan="2" height="39" align="center"> <font size="+1" ><b>회원 정보수정</b></font></td> </tr> <tr> <td colspan="2" align="center">회원의 정보를 수정합니다.</td> </tr> <tr> <td width="200"><b>아이디 입력</b></td> <td width="400"> </td> </tr> <tr> <td width="200"> 사용자 ID</td> <td width="400">${member.id}</td> </tr> <tr> <td width="200"> 비밀번호</td> <td width="400"> <input type="password" name="passwd" size="10" maxlength="10" value="${member.passwd}"> </td> </tr> <tr> <td width="200"><b>개인정보 입력</b></td> <td width="400"> </td> </tr> <tr> <td width="200">사용자 이름</td> <td width="400"> <input type="text" name="name" size="15" maxlength="10" value="${member.name}"> </td> </tr> <tr> <td width="200">주민등록번호</td> <td width="400"> ${member.jumin1}-${member.jumin2} </td> </tr> <tr> <td width="200">E-Mail</td> <td width="400"> <input type="text" name="email" size="40" maxlength="30" value="${member.email}"> </td> </tr> <tr> <td width="200">Blog</td> <td width="400"> <input type="text" name="blog" size="60" maxlength="50" value="${member.blog}"> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" name="modify" value="수 정" > <input type="button" value="취 소" onclick="javascript:window.location='login.do'"> </td> </tr> </table> </form> </body> </html>
modifyPro.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>회원정보 수정</title> </head> <body> 회원정보가 수정되었습니다. <input type="button" value="메인으로" onclick="location='login.do'"> </body> </html>
WEB-INF
lib
commons-collections-3.1.jar
commons-dbcp-1.2.1.jar
commons-pool-1.2.jar
jstl-1.2.jar
commandMap.properties
/inputForm.do=com.action.InputFormAction /inputPro.do=com.action.InputProAction /loginForm.do=com.action.LoginFormAction /loginPro.do=com.action.LoginProAction /login.do=com.action.LoginAction /logout.do=com.action.LogoutAction /modifyForm.do=com.action.ModifyFormAction /modifyPro.do=com.action.ModifyProAction /deleteForm.do=com.action.DeleteFormAction /deletePro.do=com.action.DeleteProAction /confirmId.do=com.action.ConfirmIdAction
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>member</display-name> <!-- Controller start --> <servlet> <servlet-name>Controller</servlet-name> <servlet-class>com.controller.Controller</servlet-class> <init-param> <param-name>configFile</param-name> <param-value>/WEB-INF/commandMap.properties</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Controller</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- Controller end --> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
회원가입 테스트 : InputFormAction -> inputForm -> InputAction -> input -> LoginFormAction -> loginForm
로그인 테스트 : LoginAction(섹션내용으로 로그인체크) ->로그아웃상태 -> LoginFormAction -> LoginProAction->
로그아웃 테스트 LogoutAction -> logout ->login.do
수정 테스트 : ModifyFormAction -> modifyForm.jsp -> ModifyProAction -> modifyPro.jsp -> login.do
회원탈퇴 테스트 : DeleteFormAction -> deleteForm.jsp -> DeleteProAction -> deletePro.jsp -> loginForm.do
중복아이디 체크 ConfirmIdAction-> confirmId
'JSP > 기본(Oracle)' 카테고리의 다른 글
MVC 모델2 : (Album게시판) (2) | 2012.06.20 |
---|---|
방명록 (0) | 2012.06.20 |
모델2 : MVC : Model View Controller - DB연동 (0) | 2012.06.20 |
모델2 : MVC : Model View Controlle r- DB연동없이 (0) | 2012.06.20 |
MVC: Model View Controller 모델2 ( 완전중요) (0) | 2012.06.20 |