지만 벽처럼 따로 따로 사용하는 것이 아니라 각각의 장점을 활용하여 역할을 나누었다.
JSP는 사용자에게 보여지는 화면을 담당, Sevlet은 사용자의 요청을 받아 이를 분석하여 Business 로직으로 보내고 이를 다시 받는 역할까지 수행하며 해당 결과를 JSP에게 주게 된다.
웹 어플리케이션 경로를 컨텍스트 경로(Context path)
🎇WEB-INF 하위는 외부에서 접근 불가능한 자원이기 때문에 서블릿또한 web.xml을 통해 경로를 직접 맵핑시켜 주어야 함.
자바 웹 애플리케이션은 배포 설명자 파일(DD)을 사용하여 시작파일목록, URL이 서블릿에 매핑되는 방법, 인증이 필요한 URL, 기타 정보를 확인합니다.
이 파일은 이름이 web.xml이며 WEB-INF/ 디렉터리에 위치
Web.xml 파일의 경우, 웹 어플리케이션 디렉토리마다 하나씩만 존재할 수 있다.
DD는 WAS 구동 시, /WEB-INF 디렉토리에 존재하는 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">
<!-- 시작 파일 설정 1 -> 2 -> 3 순위 .. -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 서블릿 등록-->
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>servlet.HelloServlet</servlet-class> <<- 패키지내 실제이름
</servlet>
<!-- 서블릿 맵핑-->
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern> <<- URL
</servlet-mapping>
</web-app>
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
Date today = new Date();
out.println("<html>" +
"<body>" +
"<h1 align=center>Servlet </h1><br>" +
"<br>" + today +"</body></html>");
out.append(" hello hello ! "); // 한글 인코딩 안되네
}
}
어노테이션을 통해 Web.xml에 자동으로 등록된다.
@WebServlet(name = "HiServlet", value = "/hi") // = urlPatterns = "/hi"
public class HiServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter writer = response.getWriter();
writer.println("<h1>power overwhelming !!</hi> ");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
HTML 페이지에 자바코드가 덧붙여져 있다.
정적(Static page)페이지가 아니라 자바코드에 의해 동적인 페이지이다
<!-- 시작 파일 설정 1 -> 2 -> 3 순위 .. -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
서버 시작시 시작 페이지 설정. 여기서 index.html 파일이 없다면 index.jsp는 동적으로 화면을 구성한다.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> 기본페이지 </title>
</head>
<body>
<h1> 기본페이지 </h1>
<h1><%= new Date()%> </h1>
<br>
<br>1번
<%= new TextOut().getInfo()%>
<br>2번
<%
TextOut output = new TextOut();
out.print(output.getInfo());
%>
</body>
</html>
<% 자바 코드 작성, 객체 생성 및 선언 if ~ %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%-- ctrl + shift + / print가 없어도 출력값이 나온다 --%>
영문 대문자로 변환 : <%= new String("Hello the world!!").toUpperCase() %>
<br><br>
25 곱하기 4 -> <%= 25 * 4 %>
<br><br>
판단하기 true or false : <%= 1 > 2 %>
</body>
</html>