1. 서버와 클라이언트
서버 : 시스템을 제공하는 컴퓨터
클라이언트 : 시스템을 사용하는 컴퓨터
Server <--요청(request)-- Client
--응답(response)-->
코드
WAS web client(html)
(web application server)
Servlet : java + html (작성 방식 : 자바) -> html로 보여짐
JSP : java + html (작성 방식 : html) -> html로 보여짐
2. Servlet
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public FirstServlet() {
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() 메소드 실행");
}
}
HttpServlet 클래스에는 웹 개발에 필요한 기능이 정의되어 있고, 그 기능을 사용하기 위해서 HttpServlet 클래스를 상속받아 만든 클래스를 Servlet이라 지칭한다.
Servlet을 실행시키면 톰캣이 main 메소드를 자동으로 구동시켜 준다. main 메소드가 실행되면 doGet() 메소드나 doPost() 메소드 중 하나가 무조건 실행된다.
코드 실행창의 주소 -> http://localhost:8081/ServletProject/FirstServlet
http : 프로토콜의 한 종류 (프로토콜 : 컴퓨터와 컴퓨터가 대화하는 방식)
localhost : 내 컴퓨터
8081 : 8081번 port라는 뜻인데, 컴퓨터가 서비스를 주고 받을 때 사용하는 창구를 말함.
그 뒤에는 차례로 프로젝트 이름, 서블릿 클래스 이름이 나타난다.
3. JSP
JSP는 html 문법을 사용하는데, java 코드를 쓸 때는 <% >, <%=> 태그 안에서 쓴다.
스크립틀릿 : <% 자바 문법...~~~~ %>
표현식 : <%= 메소드 호출, 변수값 읽기 %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int value = 2;
String result = "";
if(value % 2 ==0) {
result = "짝수";
}
else {
result = "홀수";
}
%>
<%= result %><br>
/* ----------------------------------- */
<%int value2 = 3; %>
<%if(value2 % 2 == 0) {%>
짝수
<%} else {%>
홀수
<%} %>
</body>
</html>
'TIL' 카테고리의 다른 글
| 230214 [JSP, Servlet] (Controller) (0) | 2023.02.15 |
|---|---|
| 230213 [JSP, Servlet] (데이터 전달) (0) | 2023.02.13 |
| 230209 [CSS] (style) (0) | 2023.02.09 |
| 230208 [Html, CSS] (list / input / 선택자) (0) | 2023.02.08 |
| 230207 [Java, Html] (Set / Map / 태그 / table) (0) | 2023.02.07 |