1 简单Servlet项目测试
-
创建一个普通的Maven项目(作为父项目)
-
删除src目录,并在跟目录 new moudle一个模板Maven项目(作为子项目),如图结构,servlet-01作为子项目
-
配置父项目添加依赖
-
子项目中编写java代码
重写doget和dopost方法
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;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("你好");
PrintWriter writer = resp.getWriter();
writer.print("hello,servlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
由于在夫项目已经添加依赖,子项目会自动继承依赖
- 配置Tomcat,将子工程添加到项目中
- 配置子项目web.xml文件
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.khp.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
- 运行Tomcat测试结果
访问s1及其下的hello