본문 바로가기
Spring/JSP

[JSP] 로그인 기능

by JJH0100 2022. 11. 15.
728x90
반응형

loginForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form method="post" action="testLogin.jsp">
		<label for="userid">아이디 : </label>
		<input type="text" name="id" id="userid"> <br>
		
		<label for="userpwd">암 &nbsp; 호 : </label>
		<input type="password" name="pwd" id="userpwd"> <br>
		
		<input type="submit" value="로그인">
	</form>
</body>
</html>

 

testLogin.jsp

<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	request.setCharacterEncoding("utf-8");
	String id = "root";
	String pw = "1234";
	
	if(id.equals(request.getParameter("id")) && pw.equals(request.getParameter("pwd"))){
		response.sendRedirect("main.jsp?id=" + URLEncoder.encode(id, "UTF-8"));
	}else{
		response.sendRedirect("loginForm.jsp");
	}
%>
</body>
</html>

loginForm에서 root, 1234를 입력하면 main페이지로 이동하고 아니면 loginForm으로 되돌아감.

main페이지로 이동할 때 id값을 같이 넘김.

 

 

main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%= request.getParameter("id") %> 님 환영합니다.
</body>
</html>

728x90
반응형

댓글