创建一个新的 JavaWeb 项目,新建测试类 HelloServlet
,该类需要继承 HttpServlet
,所以首先需要导入 Servlet
依赖
<!--Servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
按照 Tomcat 自带的例子,重写 doGet
和 doPost
方法
package com.jiuxiao.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet测试
*
* @author WuDaoJiuXiao
* @since 1.0.0
*/
public class HelloServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet测试</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet测试</h1>");
out.println("</body>");
out.println("</html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
标签:1.16,Servlet,println,import,servlet,javax,out
From: https://www.cnblogs.com/lml66/p/18042035