如何激活HttpServlet类?基于Heroku部署的GreetServer类运行与GET/POST请求处理配置咨询
Hey there! Let's break this down for you—first off, a critical point: you don't need a public static void main method for your HttpServlet subclass. Servlets are managed components, not standalone Java programs. Their entire lifecycle (instantiation, initialization, handling requests, destruction) is handled by a Servlet container like Tomcat or Jetty. Your GreetServer code looks solid for handling GET/POST requests—now let's get it activated and deployed to Heroku.
GreetServer Servlet There are two standard ways to tell the Servlet container about your class:
1. Annotation-Based Configuration (Servlet 3.0+ Recommended)
This is the simplest approach. Just add the @WebServlet annotation to your GreetServer class to define its access path:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.WebServlet; // Don't forget this import! @WebServlet("/greet") // Clients will access this at http://your-domain/greet public class GreetServer extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().print("hello client"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
2. XML-Based Configuration (Traditional Approach)
If you're using an older Servlet version or prefer XML, add a web.xml file in your WEB-INF directory with these entries:
<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>GreetServer</servlet-name> <servlet-class>your.package.name.GreetServer</servlet-class> <!-- Replace with your full class path --> </servlet> <servlet-mapping> <servlet-name>GreetServer</servlet-name> <url-pattern>/greet</url-pattern> <!-- Match the access path you want --> </servlet-mapping> </web-app>
To get this app running on Heroku, follow these steps:
1. Project Structure
Make sure your project follows the standard Java Web structure:
your-project/ ├── src/ │ └── main/ │ ├── java/ │ │ └── your/ │ │ └── package/ │ │ └── GreetServer.java │ └── webapp/ │ └── WEB-INF/ │ └── web.xml (only if using XML config) ├── pom.xml (Maven) or build.gradle (Gradle) └── Procfile
2. Maven Configuration (Example)
If using Maven, update your pom.xml to package as a WAR file and include necessary dependencies:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>your.package.name</groupId> <artifactId>greet-servlet-app</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <!-- Critical: packages as a WAR for web deployment --> <dependencies> <!-- Servlet API: Provided by the Heroku container, so only needed for compilation --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <!-- Maven WAR Plugin to build the WAR file --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.2</version> </plugin> <!-- Heroku Maven Plugin for deployment --> <plugin> <groupId>com.heroku.sdk</groupId> <artifactId>heroku-maven-plugin</artifactId> <version>3.0.0</version> <configuration> <appName>your-heroku-app-name</appName> <!-- Replace with your app's name --> </configuration> </plugin> </plugins> </build> </project>
3. Create a Procfile
In your project root, make a file named Procfile (no file extension) to tell Heroku how to start the web container. Use one of these commands:
- For Jetty (lightweight, popular for Heroku):
web: java -jar target/dependency/webapp-runner.jar target/*.war - For Tomcat:
web: java -jar target/dependency/tomcat-runner.jar target/*.war
Note: If using Maven, you'll need to download the runner first: run
mvn dependency:copy -Dartifact=com.github.jsimone:webapp-runner:9.0.70.1:jar -DoutputDirectory=target/dependencyto get the Jetty runner.
4. Deploy to Heroku
- Install the Heroku CLI, then log in:
heroku login - Initialize a Git repo if you haven't:
git init,git add .,git commit -m "Initial servlet app setup" - Create your Heroku app:
heroku create your-app-name(or make one via the Heroku web dashboard) - Push your code to Heroku:
git push heroku main
Once deployed, visit https://your-app-name.herokuapp.com/greet—you should see your "hello client" message for both GET and POST requests!
内容的提问来源于stack exchange,提问作者איתן רבינוביץ

