12. 서블릿(Servlet)
12.1 서블릿(Servlet)이란?
- 자바 서블릿(Java Servlet)은 자바를 사용하여 웹페이지를 동적으로 생성하는 서버측 프로그램 혹은 그 사양
- JSP는 HTML 문서 안에 Java 코드를 포함
- 서블릿은 자바 코드 안에 HTML을 포함
- JSP -> Servlet 으로 변환됨
- src > new > 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 설정
- @WebServlet("/Hello")
- http://localhost:8080/chap12/Hello
web.xml 설정
- WEB-INF/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 서블릿 요청 메서드 처리
- 컨테이너(Tomcat)가 자동으로 객체 생성 메서드 요청 해줌
- HttpServletRequest request 객체에 요청 정보 생성
- JSP의 내장객체 request와 동일
- HttpServletResponse response 객체에 응답 정보 생성
- JSP의 내장객체 response와 동일
- GET 요청시 : doGet() 실행
- POST 요청시 : doPost() 실행
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>
서블릿 요청 처리
- GET 요청 : doGet()
- POST 요청 : doPost()
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");
}
}
}