标签:自己 mdm tis pattern Oday pat BMI odi wls
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import java.io.IOException; import java.io.PrintWriter; /** * 自定义Servlet. * * @author jialin.li * @date 2019-12-15 20:46 */public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("开始处理get请求"); PrintWriter writer = resp.getWriter(); resp.setContentType("text/html;charset=utf-8"); writer.println("<h1>hello Servlet</h1>"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("开始处理post请求"); PrintWriter writer = resp.getWriter(); resp.setContentType("text/html;charset=utf-8"); writer.println("<h1>hello Servlet</h1>"); } }
javac -cp ./servlet-api.jar MyServlet.java
MyWebApp/WEB-INF/web.xml MyWebApp/WEB-INF/classes/MyServlet.class
<?xml version="1.0" encoding="UTF-8"?> <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"> <description> Servlet Example. </description> <display-name> MyServlet Example </display-name> <request-character-encoding>UTF-8</request-character-encoding> <servlet> <servlet-name>myServlet</servlet-name> <servlet-class>MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myServlet</servlet-name> <url-pattern>/myservlet</url-pattern> </servlet-mapping> </web-app>
sh startup.sh
hello Servlet
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.annotation.WebServlet; import java.io.IOException; import java.io.PrintWriter; /** * 自定义Servlet. * * @author jialin.li * @date 2019-12-15 20:46 */ @WebServlet("/myAnnotationServlet") public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("开始处理get请求"); PrintWriter writer = resp.getWriter(); resp.setContentType("text/html;charset=utf-8"); writer.println("<h1>hello Servlet</h1>"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("开始处理post请求"); PrintWriter writer = resp.getWriter(); resp.setContentType("text/html;charset=utf-8"); writer.println("<h1>hello Servlet</h1>"); } }
hello Servlet
标签:自己 mdm tis pattern Oday pat BMI odi wls
原文地址:https://www.cnblogs.com/nedulee/p/12063894.html