<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<h1>로그인</h1>
<form method="post" action="/lesson01/quiz10">
<input type="text" class="form-control col-4" name="id" placeholder="아이디를 입력하세요.">
<input type="password" class="form-control col-4 mt-1" name="password" placeholder="비밀번호를 입력하세요.">
<input type="submit" value="로그인" class="btn btn-primary mt-3"></input>
</form>
</div>
</body>
</html>
package com.test.lesson01;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(("/lesson01/quiz10"))
@WebServlet("") 직접 쓰기보다는 action의 값을 복사하는 게 낫다
public class PostMethodQuiz10 extends HttpServlet {
//doPost 메소드 바깥쪽에 위치
private final Map<String, String> userMap = new HashMap<String, String>() {
{
put("id", "yjkim2");
put("password", "qwerty1234");
put("name", "김유정");
}
};
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 응답 헤더 - content type
response.setContentType("text/html");
// request param
String id = request.getParameter("id");
String password = request.getParameter("password");
// 응답값 뿌리기
PrintWriter out = response.getWriter();
out.print("<html><head><title>결과</title></head><body>");
if (userMap.get("id").equals(id) == false) {
out.print("id가 일치하지 않습니다.");
} else if (userMap.get("password").equals(password) == false) {
out.print("비밀번호가 일치하지 않습니다.");
} else {
out.print(userMap.get("name") + "님 환영합니다!!");
}
out.print("</body></html>");
}
}