EchoServlet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package org.magnum.mobilecloud.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * An example of a very simple servlet that looks for a single request * parameter and echos the parameter value back to the client using * the "text/plain" content type. * * @author jules * */ public class EchoServlet extends HttpServlet // Servlets should inherit from HttpServlet { /** * All HTTP GET requests that are routed to the servlet are handled by * this method. All of the routing information about which requests * should be sent to this servlet are handled in the web.xml file * (see: src/main/webapp/WEB-INF/web.xml) * */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Set the content type header that is going to be returned in the // HTTP response so that the client will know how to display the // result. resp.setContentType("text/plain"); // Look inside of the HTTP request for either a query parameter or // a url encoded form parameter in the body that is named "msg" String msg = req.getParameter("msg"); // http://foo.bar?msg=asdf // Echo a response back to the client with the msg that was sent resp.getWriter().write("Echo:"+ msg); } /* * The servlet doesn't override the doPost(), doPut(), etc. methods * in the parent class, so this servlet can't handle the corresponding * HTTP methods. If you need to support POST requests, you would override * doPost(). * */ } |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?xml version="1.0" encoding="ISO-8859-1" ?> <!-- The web.xml file provides basic information to the web container on how to load your servlets into the container and which requests should be routed to each servlet. --> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>Echo Application</display-name> <description> This is an application that will echo any message received via the msg parameter back to the client. </description> <!-- This set of directives tells the web application container about our servlet class so that it is loaded by the container --> <servlet> <servlet-name>EchoServlet</servlet-name> <servlet-class>org.magnum.mobilecloud.servlet.EchoServlet</servlet-class> </servlet> <!-- this set of directives tells the web application container which requests should be routed to our servlet. In this case, any request to the path '/echo' will be routed to the servlet. You can use wildcards for path matching. For example, '/echo/**' would match any sub path of '/echo/', such as '/echo/foo' and '/echo/bar' --> <servlet-mapping> <servlet-name>EchoServlet</servlet-name> <url-pattern>/echo</url-pattern> <!-- http://localhost:8080/echo --> </servlet-mapping> </web-app> |