티스토리 뷰


1) Eclipse와 Library dependency하지 않도록 maven project를 생성.

일반적으로 이클립스에서 Java project로 생성하는 프로젝트는 이클립스의 native project로 생성된다. 이 말은 해당 프로젝트는 이클립스 자체 설정에 종속돼있는 라이브러리를 쓰는 프로젝트를 뜻함. 그리고 라이브러리 등의 설정들이 모두 개발도구(Eclipse 등)에 종속돼있기 때문에 자바코드는 개발환경과 분리가 안됨. 하지만 maven(또는 gradle)을 사용하면 내부에 라이브러리 설정을 셋팅하기 때문에 개발도구와 분리가 가능하게 되어 다른 이클립스 환경에서도 실행이 가능해진다.


2) Eclipse Console창에서의 실행이 아니라 WAS환경인 Tomcat위에서 프로젝트를 실행시킬 것이기 때문에 Packaging 타입을 war 로 설정. (Artifact id + Version + Packaging.war이름으로 war파일이 만들어진다)


이클립스 Maven Project 생성



3) project우클릭 후 - [Java EE Tools] - [Generate Deployment Descripter stub] 으로 web.xml 생성.


Web.xml 파일 경로



4) pom.xml파일에 maven설정을 위한 라이브러리 등록.

5) 작성한 라이브버리를 적용하기위해 project우클릭 후 - [Maven] - [Update Project]


<project xmlns="http://maven.apache.org/POM/4.0.0"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>

   <groupId>test</groupId>

   <artifactId>helloweb</artifactId>

   <version>0.0.1-SNAPSHOT</version>

   <packaging>war</packaging>


   <properties>

      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

   </properties>


   <dependencies>

      <dependency>

          <groupId>org.mariadb.jdbc</groupId>

          <artifactId>mariadb-java-client</artifactId>

          <version>2.4.0</version>

      </dependency>

   </dependencies>


   <build>

      <sourceDirectory>src/main/java</sourceDirectory>

      <resources>

         <resource>

            <directory>${project.basedir}/src/main/resources</directory>

            <excludes>

               <exclude>**/*.java</exclude>

            </excludes>

         </resource>

      </resources>      

      <plugins>

         <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-resources-plugin</artifactId>

            <configuration>

               <encoding>UTF-8</encoding>

            </configuration>

         </plugin>

         <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-compiler-plugin</artifactId>

            <version>3.8.0</version>

            <configuration>

               <source>1.8</source>

               <target>1.8</target>

            </configuration>

         </plugin>

         <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-war-plugin</artifactId>

            <configuration>

               <warSourceDirectory>src/main/webapp</warSourceDirectory>

               <failOnMissingWeb>false</failOnMissingWeb>

            </configuration>

         </plugin>

      </plugins>

   </build>   

</project>



6) war파일은 Java가 아니라 Tomcat위에서 동작하므로 런타임위치를 바꿔줘야함.

[Properties] - [Targeted Runtimes]에서 다운받은 톰캣을 선택하고 적용


이클립스 Targeted Runtime



7) 톰캣위에서 실행시킬 프로젝트를 등록.

[Servers]에 있는 톰캣 우클릭 - [Add and Remove] 로 프로젝트를 Configured로 Add 후 Finish하여 등록



8) src/main/java아래에 [new] - [servlet]으로 Servlet프로젝트를 생성.


이클립스 Servlet 생성



9) Servlet생성시 web,xml에 자동으로 Servlet설정이 작성돼있음을 확인 가능함.

<servlet-mapping>내의 <url-pattern>으로 Servlet의 url경로를 수정 가능


servlet-mapping.xml

저는 아래 Servlet의 경로(url-pattern)를 /hello로 수정



10) ServletTest작성.

response의 setContentType메소드로 한글이 안깨지도록 설정.


public class ServletTest extends HttpServlet {

private static final long serialVersionUID = 1L;


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String name = request.getParameter("name");

String email = request.getParameter("email");

String no = request.getParameter("no");

System.out.println(name + ":" + email + ":" + no);

// response

response.setContentType("text/html; charset=utf-8");

PrintWriter out = response.getWriter();

out.println("<h1> 안녕 안녕~ </h1>");

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}

}



11) servers 우측에 있는 'start the server(Ctrl + Alt + R)'를 클릭하여 톰캣 실행.


Tomcat v8.5 at localhost Servers



12) "localhost:톰캣포트번호/프로젝트이름/Servlet의 url경로" 로 정상동작 확인.


localhost:8080 확인







댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함