1. 아파치만 실행하기
- 톰캣을 사용안하려면 컴파일 안하면 됨
- 순수하게 그림밖에 못그림
HTML 파일 생성해서 아파치만으로 HTML 파일 찾아서 응답해주기
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>test page</h1> </body> </html>
2. 톰캣 실행하기
- 동적 페이지로 만들기
String html = " html 코드 붙여넣기 "; String html = """ html 코드 붙여넣기 """; // 자바 15이상부터 지원
package com.example.userapp; import jakarta.servlet.ServletException; import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletResponse; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.time.LocalDateTime; @WebServlet("/join-form") public class JoinFormServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { LocalDateTime now = LocalDateTime.now(); String html = "<!DOCTYPE html>\n" + "<html lang=\"en\">\n" + "<head>\n" + " <meta charset=\"UTF-8\">\n" + " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" + " <title>Document</title>\n" + "</head>\n" + "<body>\n" + " <h1>회원가입 페이지"+now+"</h1>\n" + " <hr>\n" + " <form action=\"/join\" method=\"post\">\n" + " <input type=\"text\" placeholder=\"username\" name=\"username\">\n" + " <input type=\"text\" placeholder=\"password\" name=\"password\">\n" + " <input type=\"text\" placeholder=\"email\" name=\"email\">\n" + " <button>회원가입</button>\n" + " </form>\n" + "</body>\n" + "</html>"; resp.getWriter().println(html); } }
- Servlet은 버퍼에 데이터를 담아서 자바 코드를 돌릴 수 있음
자바 변수에 html 코드를 담고 섞어놓을 수 있음
http 문장; //html 끊기 for (int i = 0; i < 5; i++) { // for문으로 5번 돌리기 html = html + " <h1>"+i+"회원가입 페이지"+now+"</h1>\n"; } html = html + "나머지 http문장";
package com.example.userapp; import jakarta.servlet.ServletException; import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletResponse; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.time.LocalDateTime; @WebServlet("/join-form") public class JoinFormServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { LocalDateTime now = LocalDateTime.now(); String html = "<!DOCTYPE html>\n" + "<html lang=\"en\">\n" + "<head>\n" + " <meta charset=\"UTF-8\">\n" + " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" + " <title>Document</title>\n" + "</head>\n" + "<body>\n"; //html 끊기 for (int i = 0; i < 5; i++) { html = html + " <h1>"+i+"회원가입 페이지"+now+"</h1>\n"; // for문으로 5번 돌아감 } html = html + "<hr>\n" + " <form action=\"/join\" method=\"post\">\n" + " <input type=\"text\" placeholder=\"username\" name=\"username\">\n" + " <input type=\"text\" placeholder=\"password\" name=\"password\">\n" + " <input type=\"text\" placeholder=\"email\" name=\"email\">\n" + " <button>회원가입</button>\n" + " </form>\n" + "</body>\n" + "</html>"; resp.getWriter().println(html); } }
너무 복잡함..css 넣는 것도 불편함…
- 현재 상태 : 페이지가 없어서 오류 남 → 페이지를 만들어야 함
- 페이지 생성하기
package com.example.userapp.user; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; @WebServlet("/join") public class JoinServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // username=ssar&password=1234&email=ssar@nate.com BufferedReader br = req.getReader(); String requestBody = ""; while(true){ String line = br.readLine(); if(line == null) break; requestBody = requestBody + line; } System.out.println(requestBody); } }
- 페이지 생성하기 - 톰켓이 제공하는 메서드 사용하기
package com.example.userapp.user; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; @WebServlet("/join") public class JoinServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // username=ssar&password=1234&email=ssar@nate.com // 1. 파싱 쉽게 하기 -> 커퍼에 키값을 찾아줌 String username = req.getParameter("username"); String password = req.getParameter("password"); String email = req.getParameter("email"); System.out.println("username : " + username); System.out.println("password : " + password); System.out.println("email : " + email); } }
버퍼는 꺼내면 소비됨 ⇒ 버퍼에 담긴 것이 없음
- 유효성 검사
package com.example.userapp.user; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; @WebServlet("/join") public class JoinServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // username=ssar&password=1234&email=ssar@nate.com // 1. 파싱 쉽게 하기 -> 버퍼에 있는 값을 split해서 key값을 찾아줌 String username = req.getParameter("username"); String password = req.getParameter("password"); String email = req.getParameter("email"); System.out.println("username : " + username); System.out.println("password : " + password); System.out.println("email : " + email); // 2. 유효성 검사(1000줄 정도 됨...) if(username.length() < 3 || username.length() > 10) { // 필터링으로 사용 resp.setHeader("Content-type", "text/html; charset=utf-8"); //헤더에 바디를 설명 resp.getWriter().println("<h1>username 글자수가 3~10 사이여야 합니다."); } }
한글 깨짐 해결 → 헤더에 바디를 설명
resp.setHeader("Content-type", "text/html; charset=utf-8");
- 메인 페이지 그리기
package com.example.userapp.user; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; @WebServlet("/join") public class JoinServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setHeader("Content-type", "text/html; charset=utf-8"); //헤더에 바디를 설명 // username=ssar&password=1234&email=ssar@nate.com // 1. 파싱 쉽게 하기 -> 버퍼에 있는 값을 split해서 key값을 찾아줌 String username = req.getParameter("username"); String password = req.getParameter("password"); String email = req.getParameter("email"); System.out.println("username : " + username); System.out.println("password : " + password); System.out.println("email : " + email); // 2. 유효성 검사(1000줄 정도 됨...) if(username.length() < 3 || username.length() > 10) { // 필터링으로 사용 resp.getWriter().println("<h1>username 글자수가 3~10 사이여야 합니다."); } // 3. DB연결 -> DAO에 연결하기 위함 // 4. DAO의 insert 메서드를 회출 // 5. 메인 페이지 그리기 String html = "<!DOCTYPE html>\n" + "<html lang=\"en\">\n" + "\n" + "<head>\n" + " <meta charset=\"UTF-8\">\n" + " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" + " <title>Document</title>\n" + "</head>\n" + "\n" + "<body>\n" + " <h1>메인 페이지</h1>\n" + " <hr>\n" + "</body>\n" + "\n" + "</html>"; resp.getWriter().println(html); } }
Share article