赞
踩
选择Tomcat下的servlet-api包。
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { private String message; public void init() throws ServletException { // 初始化 message = "Hello, First Servlet!"; } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置:响应内容类型 response.setContentType("text/html"); // 输出文本 PrintWriter out = response.getWriter(); out.write("<h1> " + message + " </h1>"); } }
<?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">
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
</web-app>
详见Mac IDEA开发JavaWeb一文中为项目配置Tomcat
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。