javaweb-servlet(demo註解或xml)
阿新 • • 發佈:2020-12-29
技術標籤:javaweb
建立maven-webapp專案
Maven匯入jsp和servlet依賴
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
建立servlet類
新建類繼承HttpServlet
@WebServlet("/hello")
public class DemoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("hello world");
}
}
配置servlety對映(註解或xml)
- 註解
@WebServlet("/hello")
public class DemoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("hello world");
}
}
- xml
<!--servlet對映-->
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.ckh.servlet.DemoServlet</servlet-class>
</servlet>
<!--servlet路徑-->
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>