복습 Quiz

바보·2023년 5월 18일

JSP

목록 보기
12/18

Quiz

  • 문제)

quiz.jsp			ID/PW를 입력 받는 form을 작성
					quiz_login.jsp로 전송. POST로

quiz_login.jsp		Account 클래스로 계정을 3개정도 선언부에 생성
					quiz.jsp 전달받은 정보와
                    일치하는 계정이 있으면 success.jsp로 포워드
                    일치하는 계정이 없으면 fail.jsp로 리다이렉트
                    
success.jsp			'id님 로그인을 환영합니다'를 출력

fila.jsp			로그인 실패를 출력

Account.java		userid, userpw를 가지는 Java Beans



  • quiz.jsp
<form action="quiz_login.jsp" method="POST">
	<p><input name="userid" type="text" placeholder="ID" required></p>
	<p><input name="userpw" type="password" placeholder="PW" required></p>
<button>로그인<button>
</form>



  • quiz_login.jsp
<%!
  Account[] ac = new 
%>
    
    Account가 없으니 만들러감



  • Account.java (Java /Resources/src/beans/Account.java)
패키지.beans;

public class Account {
	private String userid;
    private String userpw;
    
	public Account() {}
	
	public Account(String userid, String userpw) {
		super();
		this.userid = userid;
		this.userpw = userpw;
		
		
	}
	public String getUserid() {
		return userid;
	}
	public void setUserid(String userid) {
		this.userid = userid;
	}
	public String getUserpw() {
		return userpw;
	}
	public void setUserpw(String userpw) {
		this.userpw = userpw;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((userid == null) ? 0 : userid.hashCode());
		result = prime * result + ((userpw == null) ? 0 : userpw.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Account other = (Account) obj;
		if (userid == null) {
			if (other.userid != null)
				return false;
		} else if (!userid.equals(other.userid))
			return false;
		if (userpw == null) {
			if (other.userpw != null)
				return false;
		} else if (!userpw.equals(other.userpw))
			return false;
		return true;
	}
}



  • quiz_login.jsp
<%!
  Account[] ac = new Account[] {
  		new Account("root", "qwe"),
        new Account("user", "1234"),
        new Account("pang", "ha")
  };

	public Account login(Account input) {
    	for (int i = 0; i < ac.length; i++) {
        	if (ac[i].equals(input)) {
            	return ac[i]; 
            }
        }
      	return null;
    }
%>
  
  <% request.setCharacterEncoding("utf-8"); %>
  
  <jsp:useBean id="input" class="beans.Account"/>
  <jsp:setProperty property="*" name="input"/>
  
<%
		Account user = login(input);
		
		if (user != null) {
        	RequestDispatcher rd;
          	rd = request.getRequestDispatcher("success.jsp");
          	rd.forward(request, response);
        }
		else {
        	response.sendRedirect("fail.jsp");
        }
  %>



  • success.jsp
<h1>로그인 성공</h1>
<hr>

<h4><%=request.getParameter("userid") %>님 로그인을 환영합니다</h4>

<a href="quiz.jsp">
	<button>Home</button>
</a>
  • fail.jsp
<h1>로그인 실패</h1>
<hr>
	
<h4>일치하는 계정이 존재하지 않습니다</h4>
	
<a href="quiz.jsp">
	<button>Home</button>
</a>
profile
바보는 타이핑으로 공부하자

0개의 댓글