서블릿&JSP/뉴렉쳐 서블릿&JSP강의

[서블릿/jsp] 이클립스를 이용한 서블릿 프로그래밍

째로스 2023. 6. 17. 06:07

1. 루트 폴더에 index.html 생성

WebContent가 루트 폴더이다.

이 폴더에 아래와 같은 index.html을 생성하고 실행시켜보겠다.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	환영합니다.
</body>
</html>

 

그런데 주소를 보면 프로젝트 폴더가 표시되고 있다. 이는 보안적으로 취약하므로 프로젝트 폴더 표기없이 바로 index.html에 접근할 수 있도록 수정해보겠다.

 

2. URL에서 프로젝트 폴더명 숨기기

프로젝트 폴더 우클릭 => Properties => Web Project Settings => Context root 에서의 프로젝트명을 '/' 로 치환 => 적용

3. 서블릿 클래스인 Nana 클래스 생성

서블릿 클래스는 src 폴더 밑에 생성한다.

패키지명은 임의로 지어주면 되는데 com.newlecture.web이라고 명명해보겠다.

package com.newlecture.web;

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

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

public class Nana extends HttpServlet{
		@Override
		protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

			PrintWriter out = resp.getWriter();
			out.println("Hello ~~~ asdf ");
		}
}
<?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">
  <servlet>
  	<servlet-name>na</servlet-name>
  	<servlet-class>com.newlecture.web.Nana</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>na</servlet-name>
  	<url-pattern>/hello</url-pattern>
  </servlet-mapping>
  
  <display-name>JSPPj</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

서블릿 클래스에 바로 접근할 수 없도록 이름을 hello로 바꾸어준 모습이다.

 

4. @WebServlet 어노테이션 사용을 통한 URL 매핑

@WebServlet("/hello2")
public class Nana extends HttpServlet{
		@Override
		protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

			PrintWriter out = resp.getWriter();
			out.println("Hello ~~~ asdf ");
		}
}
<?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" 
    metadata-complete="false">

  <display-name>JSPPj</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

web.xml에 servlet과 관련한 코드를 없애도 @WebServlet 어노테이션을 통해 URL이 매핑되었음을 확인할 수 있다.