First you have to create a HttpServlet Class (See following example). You development environment should you support to create this class.
public class MyServlet extends HttpServlet {
/**
* Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
out.println("[Some Text]");
} catch (Exception e) {
...
} finally {
out.close();
}
}
/**
* Handles the HTTP GET method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
}
In the next step we have to set up the correct entry in the web.xml file for the web application. Open the web.xml and add following part (i assume at this point that the Servlet lies in the package "at.servlets"
The entries shows Tomcat where the servlet is located in your application, the tells Tomcat on which URL pattern this servlet shall be called. The servlet name is connecting both entries. Brackets should be replaced by < and >
[web-app]
...
[servlet]
[servlet-name>MyServlet[/servlet-name]
[servlet-class>at.servlets.MyServlet[/servlet-class]
[/servlet]
[servlet-mapping]
[servlet-name>MyServlet[/servlet-name]
[url-pattern>/MyServlet[/url-pattern]
[/servlet-mapping]
...
[/web-app]
After this has been done and the server restarted. The servlet should be reachable by typing an URL like: http://www.mypage.at/MyServlet