建立Web app项目
新建项目
选择 Java Enterprise,然后注意ApplicationServer的选项,其他不用选,点击下一步。
选择JavaEE Web Module 模版,点击Next
对项目命名,点击finish。
点击运行后可以看到运行的JSP页面了
增加Servlet类
在src目录下增加一个Java 类FirstServlet。
public class FirstServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置响应内容类型
resp.setContentType("text/html");
//设置逻辑实现
PrintWriter out = resp.getWriter();
out.println("
" + "hello kw 2" + "
");}
}
修改web.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
HelloWorld
com.kw.FirstServlet
HelloWorld
/firstservlet
运行,在url后面加上firstservlet,结果:
此时整个项目的目录结构如下: