[JSP/Servlet] 리다이렉트 기능 사용해서 간단한 로그인 인증 예제

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 action="testLogin.jsp" method="post">

<label for="userid"> 아이디 : </label>

<input type="text" name="id" id="userid"><br>

<label for="userpw">암 &nbsp;호 : </label>

<input type="password" name="pw" id="userpw"><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"%>


<%

String id="haha";

String pw="1234";

String name="홍길동";

request.setCharacterEncoding("UTF-8");

if(id.equals(request.getParameter("id"))&& pw.equals(request.getParameter("pw"))){

response.sendRedirect("main.jsp?name="+URLEncoder.encode(name,"UTF-8"));

}else{

response.sendRedirect("loginForm.jsp");

}

%>


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("name") %>님 안녕하세요!

<br>

저희 홈페이지에 방문해 주셔서 감사합니다.<br>

즐거운 시간 되세요!!<br>

</body>

</html>


결과


댓글

Designed by JB FACTORY