Issue
I just started learning Java. I must create servlet "Test Servlet" which show "Hello Servlet" text in h1. But when I create New Dynamic Web Project and add some jakarta imports and
response.getWriter().println("<p>Hello Servlet</p>");
to toGet (I hope in the right place) I can not run it because when I choose "External Tools -> Run As" I do not have option "Run on Server" :/ I have only "(none applicable). Before I started I add Tomcat and I have folder "Servers" so I do not know how to fix this problem to complete my task. Please help :<
package testservlet;
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloServlet
*/
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public HelloServlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("<p>Hello Servlet</p>");
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Solution
Normally you'd right-click the project, Run As ... Run on Server. Assuming you have a Server defined in your Servers view it will give you the option to select it (or create one)
If this option is not visible, check you installed the "Eclipse IDE for Enterprise Java and Web Developers", not "Eclipse IDE for Java Developers" https://www.eclipse.org/downloads/packages/
Answered By - Kevin Hooke
Answer Checked By - Willingham (JavaFixing Volunteer)