WEB/jsp

[jsp] annotation으로 url 매핑하기

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

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

 

 

위와 같은 결과지만 다른 방법을 사용하겠습니다.

 

 

web.xml

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="false">
 
    <!-- <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

바뀐 점은 matadata-complete="false" 입니다.

원래는 true였는데 false로 바꿉니다.

그리고 밑에 servlet매핑 정보는 필요없으니 주석처리합니다.

 

Nana.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.newlecture.web;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@WebServlet("/hi")
public class Nana extends HttpServlet{
 
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        out.println("Hello~~");
    }
}
 
cs

달라진 점은 바로 @WebServlet("/hi") 입니다.

어노테이션을 주는 것입니다.

그렇게 실행을 합니다.

 

 

 

그러면 hi로 아까 파일이 열립니다!

728x90