WEB/jsp

[jsp] 이클립스에서 servlet 프로그램 만들고 실행해보기

자바칩 프라푸치노 2020. 12. 28. 12:13

■ 브라우저에서 바로 열기

여기서 선택을 해주시면 자기가 선택한 웹 브라우저로 뜹니다.

 

 

■ 프로젝트 루트 설정하기

 

프로젝트 -> 우클릭-> 속성-> Web Project Settings에서 Context root를 /로 바꿉니다.

 

 

 

그리고 서버를 멈추어 줍니다.

 

아래에 있던 JSPprj를 delete합니다.

그리고 다시 실행합니다.

 

 

 

 

프로젝트로 안들어가고 바로 NewFile.jsp가 뜹니다.

 

 

 


■ 자바 코드 만들기

 

Java Resources에 src에 패키지와 클래스를 만들어줍니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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~~");
    }
}
 
cs

이렇게 적어주었습니다.

 

 

그리고 위의 경로에 있는 web.xml을 복사하여 

 

WEB-INF안에 복사하여 넣습니다.

 

 

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
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<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"
  metadata-complete="true">
 
    <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>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
 
</web-app>
 
cs

그리고 이렇게 수정을 해주었습니다.

servlet부분을 추가한 것입니다.

servlet-class태그안에는 패키지명.클래스명을 적어줍니다.

 

그러면 이렇게 실행이 됩니다.

728x90