https://getinthere.notion.site/2-x-www-form-urlencoded-json-913a9d08c0044cb0b5c86bf7385f82de
다음과 같은 구조로 만들었다. 여기서 css와 images는 이전 airbnb의 사진들이다. 화면에 뿌려주는 역할을 한다.
생성자와 getter, setter를 만든다.
MyServlet.java
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/login")
public class MyServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// http://localhost:20000/login?username=ssar&password=1234 -> 잘라서 가져오기
// 쿼리 스트링 값을 받는다
String username = req.getParameter("username"); // ssar
String password = req.getParameter("password"); // 1234
resp.setContentType("application/json"); // 응답 객체의 타입을 json으로 만든다.
ResponseMsg responseMsg = loginService(username, password);
Gson gson = new Gson();
String json = gson.toJson(responseMsg); // 파싱을 한다.
PrintWriter out = resp.getWriter(); // 출력한다.
out.println(json);
// 서버쪽 출력(Client가 아닌 Server console에 나오는 것)
System.out.println();
System.out.println();
System.out.println("=========== request 요청됨 ===========");
System.out.println("getRequestURI : " + req.getRequestURI());
System.out.println("getContextPath : " + req.getContextPath());
System.out.println("getMethod : " + req.getMethod());
System.out.println("getRequestURL" + req.getRequestURL());
System.out.println("getQueryString : " + req.getQueryString());
System.out.println("getParameter : " + req.getParameter(username));
System.out.println("getParameter : " + req.getParameter(password));
System.out.println("getSession().getId() : " + req.getSession().getId());
System.out.println("getCharacterEncoding : " + req.getCharacterEncoding());
System.out.println("getContextLength : " + req.getContextPath());
System.out.println("getContentType : " + req.getContentType());
System.out.println("Cookie Start ==================================");
for (Cookie cookie: req.getCookies()) {
System.out.println(cookie.getName() + " = " + cookie.getValue());
System.out.println(";");
}
System.out.println();
System.out.println("Cookie End ==================================");
System.out.println("getProtocol : " + req.getProtocol());
System.out.println("getServerPort : " + req.getServerPort());
System.out.println("getLocalAddr(서버 IP) : " + req.getLocalAddr());
System.out.println("getLocalName(서버 이름) : " + req.getLocalName());
System.out.println("getRemoteAddr(요청자 IP) : " + req.getRemoteAddr());
System.out.println("getRemoteUser(요청자 이름) : " + req.getRemoteUser());
System.out.println("getRemotePort(요청자 포트) : " + req.getRemotePort());
System.out.println("getLocale: " + req.getLocale());
}
// 분기가 되는 logic
private ResponseMsg loginService(String username, String password){
ResponseMsg responseMsg = null;
if (!username.isEmpty() && password.isEmpty()) {
responseMsg = new ResponseMsg(-1, "You did not put your PASSWORD, put your PASSWORD again");
} else if (username.isEmpty() && !password.isEmpty()) {
responseMsg = new ResponseMsg(-1, "You did not put your ID, put your ID again");
} else if (username.isEmpty() && password.isEmpty()) {
responseMsg = new ResponseMsg(-1, "You did not put your ID and PASSWORD, put your PASSWORD again");
} else if (!(username.isEmpty() && password.isEmpty())) {
responseMsg = new ResponseMsg(1, "Login success, hope you enjoy");
}
return responseMsg;
}
}
secure.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>this jsp file is secure from WEB-INF !</title>
</head>
<body>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>airbnb.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
This is Welcome file for 2nd assignment !
</body>
</html>
user.jsp
<%@ page import="java.io.PrintWriter" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>User 페이지</h1>
<hr/>
<%!
String getUsername(){
return "ssar";
}
%>
<%
PrintWriter pw = response.getWriter();
pw.println(getUsername());
%>
<h3><%=getUsername()%></h3> (표현식)
</body>
</html>
그럴 듯하게 나온다.
여기서 결과에 초점을 두지 말고 과정에 초점을 두면
일단 나는 화면에 나온 창을 ID 와 PW를 입력하면 해당 값이 URL의 쿼리 문으로 가도록 만든 것이다.
그 URL이 reqeust의 getParameter 함수를 통해 각각의 쿼리스트링을 구분하고 그 값을 Gson을 통해 json형태로 파싱 후 분기를 하는 loginLogic 메소드로 가서 다음과 같은 화면이 나온 것이다.