자바 웹 의 세 가지 생 성 Servlet 방식 설명
pom.xml 를 jar 패키지 에 의존 하도록 관리 합 니 다.
<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.0modelVersion>
<groupId>com.kuanggroupId>
<artifactId>servlet01artifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>servlet01 Maven Webappname>
<url>http://www.example.comurl>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.3version>
dependency>
dependencies>
<build>
<finalName>servlet01finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-pluginartifactId>
<version>3.1.0version>
plugin>
<plugin>
<artifactId>maven-resources-pluginartifactId>
<version>3.0.2version>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.0version>
plugin>
<plugin>
<artifactId>maven-surefire-pluginartifactId>
<version>2.22.1version>
plugin>
<plugin>
<artifactId>maven-war-pluginartifactId>
<version>3.2.2version>
plugin>
<plugin>
<artifactId>maven-install-pluginartifactId>
<version>2.5.2version>
plugin>
<plugin>
<artifactId>maven-deploy-pluginartifactId>
<version>2.8.2version>
plugin>
plugins>
pluginManagement>
build>
project>
방법 1:Servlet 인터페이스 직접 실현
import javax.servlet.*;
import java.io.IOException;
// servlet : Servlet
public class servletBuildByServlet implements Servlet {
// ( )
public servletBuildByServlet() {
System.out.println(" ");
}
// ( Servlet )
public void init(ServletConfig arg0) throws ServletException {
System.out.println(" init ");
}
// ( , , )
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
System.out.println("service ");
}
// severlet
//1.
//2.
public void destroy() {
System.out.println("destroy ");
}
// servlet
public ServletConfig getServletConfig() {
return null;
}
// servlet ,
public String getServletInfo() {
return null;
}
}
방법 2:GenericServlet 클래스 계승
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
// GenericServlet
public class servletBuildByGenericServlet extends GenericServlet {
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("servlet Build By GenericServlet class");
}
}
방법 3:HttpServlet 클래스 계승
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class servletBuildByHttpServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("servlet Build By HttpServlet class.");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
웹.xml 를 통 해 Servlet 를 등록 합 니 다.
<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>servletBuildByServletservlet-name>
<servlet-class>com.kuang.buildMethods.servletBuildByServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>servletBuildByServletservlet-name>
<url-pattern>/sbbsurl-pattern>
servlet-mapping>
<servlet>
<servlet-name>servletBuildByHttpServletservlet-name>
<servlet-class>com.kuang.buildMethods.servletBuildByHttpServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>servletBuildByHttpServletservlet-name>
<url-pattern>/sbbhsurl-pattern>
servlet-mapping>
<servlet>
<servlet-name>servletBuildByGenericServletservlet-name>
<servlet-class>com.kuang.buildMethods.servletBuildByGenericServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>servletBuildByGenericServletservlet-name>
<url-pattern>/sbbgsurl-pattern>
servlet-mapping>
web-app>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.