본문 바로가기
CLASS/SERVLET

#3-1 / form 예제

by eungSe__ 2024. 5. 30.

MVC

: Module(class) , View(JSP) , Controller(Servlet)

⚡ SPA 로그인 - 일반회원,사업자 : 해당 조건 외에는 로그인 실패하는 프로세서 제작

radio,checkbox name이 동일한 경우 java에서 배열로 사용
ex) <input name="part" type="radio"> 2개 => part[0],part[1]

!! radio,checkbox onclick="함수명(this.value)" 활용시 해당 value값 인자로 전달 !! good

* html
<body>
<form id="frm" method="post" action="./loginok.do">
    <p>
        <label><input type="radio" name="part" value="P" onclick="mb_part(this.value)" checked>일반 회원</label>
        <label><input type="radio" name="part" value="C" onclick="mb_part(this.value)">사업자 회원</label>
    </p>
    <p>
        <input type="text" name="mid" placeholder="아이디를 입력하세요"><br>
        <input type="text" name="mpass" placeholder="패스워드를 입력하세요"><br>
        <input type="text" name="corpno" placeholder="사업자 번호10자리를 입력하세요" maxlength="10" style="display:none;">
    </p>
    <input type="button" value="로그인" onclick="js_submit()">
</form>
</body>

<script type="text/javascript">
	var option= "";//radio를 선택하는 값을 받는 전역변수
	function mb_part(pt){
		option = pt;
		if(pt == "C"){
			frm.corpno.style.display = "block";
		}else{
			frm.corpno.style.display = "none";
		}
	}
	
	function js_submit(){
		if(frm.mid.value==""){
			alert("아이디를 입력하세요");
		}else if(frm.mpass.value==""){
			alert("패스워드를 입력하세요");
		}else{
			//전역변수의 값을 체크하여 일반,사업자 확인 후 전송
			if(option == "C"){
				if(frm.corpno.value=="" || frm.corpno.value.length < 10){
					alert("사업자번호 10자리를 입력하셔야합니다");
					frm.corpno.value="";
					frm.corpno.focus();
				}else{
					frm.submit();
				}
			}else{
				frm.submit();
			}
		}
	}
</script>




* java servlete 파일 - Controller(servlete) 담당

public class loginok extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// Controller -login

	private login_part lp = new login_part(); // 별도파일(Module) class 로드
	String msg = ""; // 결과값을 받는 전역변수
	PrintWriter pw = null;

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");

		String part = request.getParameter("part").intern(); // 연산기호 사용가능
		String mid = request.getParameter("mid").intern();
		String mpass = request.getParameter("mpass").intern();

		if (part == "C") { // 사업자회원
			String corpno = request.getParameter("corpno").intern();
			
			// module class에 있는 해당 메소드 값을 return받음
			this.msg = this.lp.corpers(mid,mpass,corpno); 
		} else { // 일반회원
			this.msg = this.lp.person(mid,mpass);
		}
		
		this.pw = response.getWriter();
		this.pw.print(this.msg);
		this.pw.flush();
		this.pw.close();
	}

}


* 별도파일 - Module 담당

//Module제작 - login

public class login_part {
	private String result = "";

	public String person(String id, String pw) { //일반회원
		if(id == "apink" && pw == "0486") {
			this.result = "로그인 하셨습니다"; 
		}else {
			this.result = "아이디 및 패스워드를 확인하지 못하였습니다.";
		}
		return this.result;
	}
	
	public String corpers(String id, String pw,String no) { //사업자 회원
		if(id == "sm" && pw == "sm1234" && no=="1234567899") {
			this.result = "로그인 하셨습니다"; 
		}else {
			this.result = "패스워드 및 사업자번호를 확인하지 못하였습니다.";
		}
		return this.result;
	}
}

 

 

▶ spa란 ? https://dev-eunse.tistory.com/109

'CLASS > SERVLET' 카테고리의 다른 글

#3-3 / html-> html 값 전달  (0) 2024.05.30
#3-2 / 암호화(security)  (0) 2024.05.30
#2-4 / double form  (0) 2024.05.29
#2-3 / html 태그별 데이터 전송  (0) 2024.05.29
#2-2 / 검색에 따른 get 전송방식  (0) 2024.05.29