Microsoft MVP성태의 닷넷 이야기
Java: 1. 닷넷 개발자가 처음 실습해 본 서블릿 [링크 복사], [링크+제목 복사],
조회: 31723
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 3개 있습니다.)

닷넷 개발자가 처음 실습해 본 서블릿

서블릿 테스트를 하려면 개발환경을 구축해야 겠지요. ^^ 아래의 항목들을 다운로드 해서 설치하고,

JDK (제 경우에는 JDK 6 설치)
; http://www.oracle.com/technetwork/java/javase/downloads/index.html

Eclipse IDE for Java Developers
; http://www.eclipse.org/downloads

아파치 톰캣
; http://tomcat.apache.org/
; https://www.sysnet.pe.kr/2/0/798

준비가 되었으면, Eclipse를 실행해서 예제 프로젝트를 생성한 후, HelloWorld.java 파일을 하나 추가해야 겠지요. ^^

그런데, import 구문 코드 작성하는 단계부터 다음과 같이 오류가 발생할텐데요.

how_to_test_servlet_1.png
"
The import javax.servlet cannot be resolved
"


해답은 다음의 글에 나오더군요.

[java]import javax.servlet.jsp.* can not be resolved 
; http://arima603.tistory.com/entry/javaimport-javaxservletjsp-can-not-be-resolved

즉, 기본적으로 서블릿 관련한 jar 파일이 JDK에 제공되지 않아서 그런 것인데, 이 파일들은 서블릿을 구동할 수 있는 웹 서버 측에서 보통 함께 제공되는 것 같습니다. 이 때문에 Tomcat의 압축을 해제한 '[설치폴더]\lib'에 있는 servlet-api.jar, jsp-api.jar 2개의 파일을 이클립스에 알려줘야 합니다.

이를 위해, 프로젝트 폴더를 오른쪽 버튼을 눌러 "Build Path" / "Configure Build Path..." 메뉴를 선택해서 톰캣에 있는 jar 파일들을 알려주면 됩니다.

how_to_test_servlet_2.png

그다음, 마저 서블릿 예제를 완성시키고 "Ctrl + B (Build All)" 키를 눌러 컴파일 시킵니다.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    
    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException
            {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        
        out.println("<html><body>Hello World</body></html>");
        
            }
}

그럼, 해당 프로젝트의 하위 /bin 폴더에 HelloWorld.class 파일이 생성되는데 이것을 웹 서버(톰캣)가 설치된 하위 "\webapps\ROOT\WEB-INF"에 "classes" 폴더를 생성하고 복사합니다.

"...[톰캣 압축 해제 폴더]\webapps\ROOT\WEB-INF\classes\HelloWorld.class"

아직 끝이 아닙니다. ^^ 아쉽게도 톰캣에서는 기본적으로 servlet을 활성화 해놓지 않고 있어서, 이에 대한 설정을 변경해 주어야 합니다.

JSP 0708 - TOMCAT Servlet을 돌리기위한 기본설정 2
; http://dandyboys.tistory.com/entry/JSP-0708-TOMCAT-Servlet-을-돌리기위한-기본설정-2

위의 글에 따라서 /conf/web.xml 파일을 편집기로 열고, invoker 관련 서블릿 구성(servlet, servlet-mapping)에 대해 주석을 해제합니다.

<servlet>
    <servlet-name>invoker</servlet-name>
    <servlet-class>
        org.apache.catalina.servlets.InvokerServlet
    </servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>invoker</servlet-name>
    <url-pattern>/servlet/*</url-pattern>
</servlet-mapping>

위의 설정만 해제하고 Tomcat 6에서 웹서버를 구동하면 콘솔 화면에서 다음과 같은 오류를 확인할 수 있습니다.

Error deploying web application directory docs
java.lang.SecurityException: Servlet of class org.apache.catalina.servlets.InvokerServlet is privileged and cannot be loaded by this web application
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1146)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4421)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4734)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
        at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079)
        at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
        at org.apache.catalina.core.StandardService.start(StandardService.java:525)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

찾아보니, ^^ 다음과 같은 글이 나옵니다.

1.톰캣 6.0 서블릿 로딩 설정(context.xml)
; http://jaehwacho.springnote.com/pages/2499106.xhtml

따라서, /conf/context.xml 파일을 열어서 Context노드에 reloadable, privileged 속성을 다음과 같이 추가해 주면 됩니다.

<Context reloadable="true" privileged="true">

휴~~~, 이제 끝입니다. ^^ 마지막으로 '/bin/startup.bat' 파일을 실행하면 톰캣 웹 서버가 구동되고, "http://localhost:8080/servlet/HelloWorld" 주소를 웹 브라우저로 방문하면 실행되는 것을 확인할 수 있습니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 6/21/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 106  107  108  109  110  111  [112]  113  114  115  116  117  118  119  120  ...
NoWriterDateCnt.TitleFile(s)
11157정성태3/22/201722688.NET Framework: 648. Dictionary<TKey, TValue>를 deep copy하는 방법파일 다운로드1
11156정성태3/21/201723962.NET Framework: 647. 닷넷(C#) 코드로 인증서 요청 코드 만드는 방법파일 다운로드1
11155정성태3/21/201724232.NET Framework: 646. SslStream의 CipherAlgorithm 선택이 가능할까요?파일 다운로드1
11154정성태3/5/201730544VC++: 109. DLL에서 STL 객체를 인자/반환값으로 갖는 함수를 제공할 때, 그 함수를 외부에서 사용하는 경우 비정상 종료한다면? [2]파일 다운로드1
11153정성태3/5/201730053VC++: 108. DLL에 정의된 C++ template 클래스의 복사 생성자 문제파일 다운로드1
11152정성태3/4/201724284VC++: 107. VirtualAlloc, HeapAlloc, GlobalAlloc, LocalAlloc, malloc, new의 차이점 [1]파일 다운로드1
11151정성태3/3/201724477VC++: 106. DLL 개발자가 주의해야 할 Secure CRT 함수 사용 [1]파일 다운로드1
11150정성태2/21/201720441.NET Framework: 645. Visual Studio Fakes 기능에서 Shim... 클래스가 생성되지 않는 경우 [5]
11149정성태2/21/201724283오류 유형: 378. A 64-bit test cannot run in a 32-bit process. Specify platform as X64 to force test run in X64 mode on X64 machine.
11148정성태2/20/201723384.NET Framework: 644. AppDomain에 대한 단위 테스트 시 알아야 할 사항
11147정성태2/19/201721831오류 유형: 377. Windows 10에서 Fake 어셈블리를 생성하는 경우 빌드 시 The type or namespace name '...' does not exist in the namespace 컴파일 오류 발생
11146정성태2/19/201721097오류 유형: 376. Error VSP1033: The file '...' does not contain a recognized executable image. [2]
11145정성태2/16/201722541.NET Framework: 643. 작업자 프로세스(w3wp.exe)가 재시작되는 시점을 알 수 있는 방법 - 두 번째 이야기 [4]파일 다운로드1
11144정성태2/6/201725951.NET Framework: 642. C# 개발자를 위한 Win32 DLL export 함수의 호출 규약 (부록 1) - CallingConvention.StdCall, CallingConvention.Cdecl에 상관없이 왜 호출이 잘 될까요?파일 다운로드1
11143정성태2/5/201723480.NET Framework: 641. [Out] 형식의 int * 인자를 가진 함수에 대한 P/Invoke 호출 방법파일 다운로드1
11142정성태2/5/201731339.NET Framework: 640. 닷넷 - 배열 크기의 한계 [2]파일 다운로드1
11141정성태1/31/201725799.NET Framework: 639. C# 개발자를 위한 Win32 DLL export 함수의 호출 규약 (4) - CLR JIT 컴파일러의 P/Invoke 호출 규약 [1]파일 다운로드1
11140정성태1/27/201721177.NET Framework: 638. RSAParameters와 RSA파일 다운로드1
11139정성태1/22/201724094.NET Framework: 637. C# 개발자를 위한 Win32 DLL export 함수의 호출 규약 (3) - x64 환경의 __fastcall과 Name mangling [1]파일 다운로드1
11138정성태1/20/201722516VS.NET IDE: 113. 프로젝트 생성 시부터 "Enable the Visual Studio hosting process" 옵션을 끄는 방법 - 두 번째 이야기 [3]
11137정성태1/20/201720669Windows: 135. AD에 참여한 컴퓨터로 RDP 연결 시 배경 화면을 못 바꾸는 정책
11136정성태1/20/201720290오류 유형: 375. Hyper-V 내에 구성한 Active Directory 환경의 시간 구성 방법 - 두 번째 이야기
11135정성태1/20/201721117Windows: 134. Windows Server 2016의 작업 표시줄에 있는 시계가 사라졌다면? [1]
11134정성태1/20/201728698.NET Framework: 636. System.Threading.Timer를 이용해 타이머 작업을 할 때 유의할 점 [5]파일 다운로드1
11133정성태1/20/201724772.NET Framework: 635. C# 개발자를 위한 Win32 DLL export 함수의 호출 규약 (2) - x86 환경의 __fastcall [1]파일 다운로드1
11132정성태1/19/201736603.NET Framework: 634. C# 개발자를 위한 Win32 DLL export 함수의 호출 규약 (1) - x86 환경에서의 __cdecl, __stdcall에 대한 Name mangling [1]파일 다운로드1
... 106  107  108  109  110  111  [112]  113  114  115  116  117  118  119  120  ...