Archive for the ‘ JSP&Servlet ’ Category

Servlet 만들기

GenericServlet을 상속받아 Servlet 만들기

1. tomcat을 start시키고

2. 프로젝트를 생성한다.

new -> Dynamic Web Project (없으면 Other -> Web -> Dynamic Web Project)

프로젝트명 : webtest

3. 클래스 생성

Java Resources: src 디렉토리에 클래스를 생성한다.

Package 명 : step1

Class 명 : MyServlet

4. 아래와 같이 MyServlet 클래스를 만들고

WebContent 디렉토리에 index.html을 만든다.

5. MyServlet.java 파일 내용

GenericServlet을 extends하고 service() 메서드를 오버라이딩한다.


package step1;

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

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyServlet extends GenericServlet {
 @Override
 public void service(ServletRequest request, ServletResponse response)
 throws ServletException, IOException {
 System.out.println("*** service ***");// 요청이 있을 때마다 프린트해준다.
 response.setContentType("text/html;charset=euc-kr");
 PrintWriter out = response.getWriter(); // 브라우저에 보여준다.
 out.println("<html><body>");
 out.println("<font size=7 color=blue>Hello Servlet!!</font>");
 out.println("</body></html>");
 out.close();

 }
}

6. web.xml 파일 내용

아래 내용을 추가한다.


<servlet>
 <servlet-name>hello</servlet-name>
 <servlet-class>step1.MyServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>hello</servlet-name>
 <url-pattern>/hello</url-pattern>
 </servlet-mapping>

7. Run as -> Run on Server 클릭하여 컴파일과 동기화

8. http://localhost:8888/webtest/hello 에 접속하면 아래와 같은 화면이 뜬다

웹브라우저의 요청이 있을 때마다 tomcat 에는 아래와 같이 out.println()을 이용해서 요청을 알린다

Eclipse와 Tomcat 연동

1. Tomcat 6.0 다운로드

http://tomcat.apache.org/

2. 적당한 위치에 다운로드 받은 압축파일을 풀고 폴더명을 tomcat으로 변경

3. tomcat\conf\server.xml 파일 편집

port를 8888로 변경(8080포트가 oracle과 충돌되므로), URIEncoding=”euc-kr” 추가(한글처리)

4. tomcat\conf\context.xml 파일 편집

reloadable=”true” 추가

5. Eclipse 메뉴 Window -> Open perspective -> Servers 추가

Eclipse 아래 Servers 탭에서 마우스 오른쪽 버튼 클릭하여 new -> apache -> tomcat6.0 선택

Tomcat Installation directory 에서 browse 눌러 tomcat 디렉토리 선택

JRE는 자신의 JDK를 선택 후 Finish

그러면 Servers 탭에 생성한 Tomcat 항목이 생긴다.

6. Tomcat 항목 더블클릭

Overview 에서 Server Locations 를 Use Tomcat installation 선택,

Deploy path 를 자신의 tomcat\webapps로 변경 후 ctrl + s 저장하기

마지막으로 Servers 탭의 tomcat 항목 선택 후 마우스 오른쪽 버튼 클릭

메뉴에서 start 버튼을 누르면 tomcat이 실행된다.

http://localhost:8888/ 로 접속하여 제대로 tomcat이 실행되는지 확인한다.

DD, deployment descriptor

deployment descriptor (DD) refers to a configuration file for an artifact that is deployed to some container/engine.

In the Java Platform, Enterprise Edition, a deployment descriptor describes how a web application or enterprise application should be deployed. It directs a deployment tool to deploy a module or application with specific container options, security settings and describes specific configuration requirements. XML is used for the syntax of these deployment descriptor files. For web applications, the deployment descriptor must be called web.xml and must reside in a WEB-INF subdirectory at the web application root. For Java EE applications, the deployment descriptor must be named application.xml and must be placed directly in the META-INF directory at the top level of the application .ear file.

출처 – http://en.wikipedia.org/wiki/Deployment_Descriptor