본문 바로가기

JSP, Servlet/JSP, Servlet 기초

[JSP/Servlet] redirect 실습(로그인)

<04_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="04_testLogin.jsp" name="loginForm">
		<table>
			<tr>
				<td>아이디</td>
				<td><input type="text" id="id" name="id"></td>
			</tr>
			<tr>
				<td>비밀번호 </td>
				<td><input type="password" id="pwd" name="pwd"></td>
			</tr>
			<tr>
				<td><input type="submit" value="로그인"></td>
				<td><input type="reset" value="취소"></td>
			</tr>
		</table>
	</form>
</body>
</html>

로그인폼을 만들어준다.

 

<04_testLogin.jsp>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.net.URLEncoder" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
	String name = "이순신";
		request.setCharacterEncoding("UTF-8");
	String id = request.getParameter("id");
	String pwd = request.getParameter("pwd");

	if (id.equals("abcd") && pwd.equals("1234")) {
		response.sendRedirect("04_main.jsp?name" + URLEncoder.encode(name, "UTF-8"));
	} else {
		response.sendRedirect("04_loginForm.jsp");
	}
	%>
</body>
</html>

로그인을 처리해줄 페이지를 따로 만든다.

로그인정보 일치는 임의로 지정해준다.

 

<04_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>
<h3>Main page</h3>
<%=request.getParameter("name") %>님 안녕하세요<br>
</body>
</html>

로그인에 성공하면 넘어갈 페이지를 만들어준다.

 

728x90
반응형