12. 서블릿(Servlet)

12.1 서블릿(Servlet)이란?

Servlet

Hello.java

package chap12;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Hello")
public class Hello extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Hello() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("Hello Servlet!!");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

}

12.1.1 서블릿 실행 요청

어노테이션 Anotation 설정

web.xml 설정

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>chap12</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- servlet 설정 -->
    <servlet>
        <servlet-name>Hello</servlet-name>
        <servlet-class>chap12.Hello</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>


</web-app>

Server HTML 응답

Hello.java

package chap12;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Hello")
public class Hello extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Hello() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html; charset=UTF-8");
        PrintWriter pw = response.getWriter();

        pw.println("<html>");
        pw.println("<title>");
        pw.println("servlet test");
        pw.println("</title>");
        pw.println("<body>");
        pw.println("<h1>");
        pw.println("Hello Servlet!!");
        pw.println("</h1>");
        pw.println("</body>");
        pw.println("</html>");

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

}

12.1.2 서블릿 요청 메서드 처리

GET 요청 확인

login.html - GET

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login - 로그인</title>
<style>
    th {width:100px; background-color: rgb(200, 150, 200); align:center;}
</style>
</head>
<body>
<center>
<h1>Login - 로그인</h1>
<hr>
<form action="hello" method="get">
<table border="1">
    <tr><th>ID</td><td><input type="text" name="id"></td></tr>
    <tr><th>PW</td><td><input type="password" name="password"></td></tr>
    <tr><td colspan="2" align="center"><input type="submit" value="LogIn"></td></tr>
</table>
</form>
<hr>
</center>
</body>
</html>

서블릿 요청 처리

Hello.java

package chap12;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Hello")
public class Hello extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Hello() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doGet");


        response.setContentType("text/html; charset=UTF-8");
        String id = request.getParameter("id");
        String password = request.getParameter("password");

        System.out.println("ID:" + id);
        System.out.println("PW:" + password);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doPost");


        response.setContentType("text/html; charset=UTF-8");
        String id = request.getParameter("id");
        String password = request.getParameter("password");

        System.out.println("ID:" + id);
        System.out.println("PW:" + password);

    }

}

POST 요청 확인

login.html - POST

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login - 로그인</title>
<style>
    th {width:100px; background-color: rgb(200, 150, 200); align:center;}
</style>
</head>
<body>
<center>
<h1>Login - 로그인</h1>
<hr>
<form action="hello" method="post">
<table border="1">
    <tr><th>ID</td><td><input type="text" name="id"></td></tr>
    <tr><th>PW</td><td><input type="password" name="password"></td></tr>
    <tr><td colspan="2" align="center"><input type="submit" value="LogIn"></td></tr>
</table>
</form>
<hr>
</center>
</body>
</html>

12.2 View(JSP)와 서블릿(JAVA) 분리

12.2.1 로그인

login.html - POST

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login - 로그인</title>
<style>
    th {width:100px; background-color: rgb(200, 150, 200); align:center;}
</style>
</head>
<body>
<center>
<h1>Login - 로그인</h1>
<hr>
<form action="loginProc" method="post">
<table border="1">
    <tr><th>ID</td><td><input type="text" name="id"></td></tr>
    <tr><th>PW</td><td><input type="password" name="password"></td></tr>
    <tr><td colspan="2" align="center"><input type="submit" value="LogIn"></td></tr>
</table>
</form>
<hr>
</center>
</body>
</html>

LoginProc.java

package user.service;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import user.serviceImpl.UserServiceImpl;
import users.vo.UserVO;

@WebServlet("/loginProc")
public class LoginProc extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public LoginProc() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doGet");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doPost");

        // 한글 처리
        response.setContentType("text/html; charset=UTF-8");

        // 1. 넘어온 값 확인
        String id = request.getParameter("id");
        String pw = request.getParameter("password");

        // user  생성
        UserVO vo = new UserVO(id, pw, "", "");

        // insert Servie 진행
        UserService service = new UserServiceImpl();
        UserVO rs = service.getUSER(vo);

        if(rs == null){
            response.sendRedirect("login.html");
        }else {
            HttpSession session = request.getSession();

            session.setAttribute("id", rs.getId());
            session.setAttribute("name", rs.getName());

            response.sendRedirect("main.jsp");
        }

    }

}