hyeonga_code
Servlet_서블릿 매핑 HttpServletRequest로 요청 처리하기 본문
- servlet
--- servlet-api
1) 프로젝트 우클릭 > Build Path > Classpath
- Add External JARs... > servlet-api.jar 파일 추가하기
- 브라우저에서 요청하는 기본적인 방법
http:// [ IP주소 ] : [ 포트 번호 ] / [ 프로젝트 이름 ] / [ 패키지 이름이 포함된 클래스 이름 ]
서블릿 매핑 > web.xml 파일에 설정합니다.
<servlet>
<servlet-name> [ servlet-mapping에서 설정한 servlet-name ]</servlet-name>
<servlet-class> [ 실제 기능을 수행하는 클래스 경로 ] </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> [ 서블릿 이름 ] </servlet-name>
<url-pattern> [ 웹 브라우저에서 요청하는 이름 ] </url-pattern>
</servlet-mapping>
>>>
http:// [ IP 주소 ] / [ 프로젝트 이름 ] / [웹 브라우저에서 요청하는 이름]
- 주의사항 : 여러 개의 서블릿 매핑을 진행하는 경우 servlet끼리 위치시킵니다.
- 서블릿은 메모리에 존재하는 서블릿을 재사용함으로 빠르고 효율적으로 동작합니다.
- 서블릿 클래스 : HttpServlet 상속
2) 어노테이션으로 매핑하기
@WebServlet(" / [ 웹 브라우저에서 요청하는 이름 ] ") : 서블릿 클래스 위에 선언합니다.
- 주의사항 : 어노테이션으로 매핑하는 경우 반드시 HttpServlet을 상속받아야 합니다.
- 주의사항 : web.xml 파일에서 설정한 매핑 이름과 어노테이션으로 설정한 매핑 이름이 동일한 경우 오류 발생
A child container failed during start
ExecutionException
===== 실습 환경 =====
1. New > Spring Legacy Project 생성
--- Project Name : servletProject
--- Template : Spring MVC Project
--- package : com.basic.servlet
2. Java 버전 변경
프로젝트 우클릭 > Properties > Project Facet
3. pom.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<modelVersion>4.0.0</modelVersion>
<groupId>com.basic</groupId>
<artifactId>servlet</artifactId>
<name>servletProject</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>11</java-version>
<org.springframework-version>5.3.27</org.springframework-version>
<org.aspectj-version>1.9.19</org.aspectj-version>
<org.slf4j-version>2.0.7</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<!-- Servlet : 기본값
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
-->
<!-- javax.servlet-api : servlet-api 대신 사용 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<!-- javax.servlet.jsp-api : jsp-api 대신 사용 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- project 기본 설정 끝 -->
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<!-- maven-compiler-plugin : 버전 변경 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>11</source>
<target>11</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<!-- exec-maven-plugin : 버전 변경 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
|
4. src > main > webapp > form.html html 파일 생성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form.html</title>
</head>
<body>
<form name="formtag" method="get" action="formtag">
Input Data 1 : <input type="text" name="data1"> <br>
Input Data 2 : <input type="text" name="data2"> <br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
|
cs |
5. src/main/java > com.basic.servlet > FormServlet.java 클래스 생성
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
|
package com.basic.servlet;
import java.io.IOException;
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("/formtag")
public class FormServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException{
System.out.println("[ FormServlet ] : init()");
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("[ FormServlet ] : doGet()");
request.setCharacterEncoding("UTF-8");
String data1 = request.getParameter("data1");
String data2 = request.getParameter("data2");
System.out.println("Data 1 : " + data1);
System.out.println("Data 2 : " + data2);
}
@Override
public void destroy() {
System.out.println("[ FormServlet ] : destroy()");
}
}
|
cs |
6. 실행하기
1) 웹 브라우저에 http://localhost:8080/servlet/form.html
2) Submit buttom 클릭
응답 화면을 작성하지 않았으므로 브라우저에서는 보여지지 않지만 콘솔창에서 출력됩니다.
'JSP' 카테고리의 다른 글
Servlet_서블릿 매핑 어노테이션으로 작성하여 브라우저에 요청하기 (0) | 2024.01.24 |
---|---|
Servlet_서블릿 매핑 xml 파일로 작성하여 브라우저 요청하기 (0) | 2024.01.24 |