idea的servlet项目怎么用,怎么在idea里导入servlet的包

首页 > 机动车 > 作者:YD1662024-01-14 15:37:09

代码案例

pom.xml配置:

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- Web Dependencies --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--测试使用 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies>

OneServlet.java

package com.what21.servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpservletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class OneServlet extends HttpServlet { private static final long serialVersionUID = -2725864234106838841L; @Override public void init(ServletConfig config) throws ServletException { super.init(config); } @Override public void init() throws ServletException { super.init(); } @Override public void destroy() { super.destroy(); } @Override protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doDelete(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //super.doGet(req, resp); resp.getWriter().print("<h1>OneServlet doGet()<h1>"); } @Override protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doHead(req, resp); } @Override protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doOptions(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } @Override protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPut(req, resp); } @Override protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doTrace(req, resp); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.service(req, resp); } @Override public void service(ServletRequest sreq, ServletResponse sresp) throws ServletException, IOException { super.service(sreq, sresp); } }

TwoServlet

package com.what21.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("/two") public class TwoServlet extends HttpServlet { private static final long serialVersionUID = 6313903606540274020L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().print("<h1>TwoServlet doGet()<h1>"); } }

web.xml配置

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>OneServlet</servlet-name> <servlet-class>com.what21.servlet.OneServlet</servlet-class> <init-param> <param-name>init_name</param-name> <param-value>init_value</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>OneServlet</servlet-name> <url-pattern>/one</url-pattern> </servlet-mapping> </web-app>IntelliJ IDEA配置运行

配置:Add Configuration,选择:tomcat Server

idea的servlet项目怎么用,怎么在idea里导入servlet的包(5)

配置:选择tomcat,选择jdk

idea的servlet项目怎么用,怎么在idea里导入servlet的包(6)

部署当前项目

idea的servlet项目怎么用,怎么在idea里导入servlet的包(7)

选择当前项目的:war,并填写:Application Context名称

idea的servlet项目怎么用,怎么在idea里导入servlet的包(8)

上一页123下一页

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.