{"id":22611196,"url":"https://github.com/bzdgn/simpleservletwebapp","last_synced_at":"2026-05-03T04:37:00.400Z","repository":{"id":22654470,"uuid":"25997596","full_name":"bzdgn/SimpleServletWebApp","owner":"bzdgn","description":null,"archived":false,"fork":false,"pushed_at":"2014-11-07T14:26:48.000Z","size":276,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-03T09:47:46.713Z","etag":null,"topics":["java","servlet","webapp"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bzdgn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-10-31T01:48:33.000Z","updated_at":"2017-05-17T09:49:20.000Z","dependencies_parsed_at":"2022-08-21T08:31:14.971Z","dependency_job_id":null,"html_url":"https://github.com/bzdgn/SimpleServletWebApp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bzdgn%2FSimpleServletWebApp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bzdgn%2FSimpleServletWebApp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bzdgn%2FSimpleServletWebApp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bzdgn%2FSimpleServletWebApp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bzdgn","download_url":"https://codeload.github.com/bzdgn/SimpleServletWebApp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246112658,"owners_count":20725301,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","servlet","webapp"],"created_at":"2024-12-08T16:09:45.117Z","updated_at":"2026-05-03T04:36:55.364Z","avatar_url":"https://github.com/bzdgn.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"This is a simple project to learn how servlet's are working. Here are the notes\r\nthat I've taken while studying java servlet technology.\r\n\r\n#mvn archetype:generate -DgroupId=com.levent -DartifactId=SimpleServletWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false\r\n#http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api/3.1.0\r\n\u003cdependency\u003e\r\n\t\u003cgroupId\u003ejavax.servlet\u003c/groupId\u003e\r\n\t\u003cartifactId\u003ejavax.servlet-api\u003c/artifactId\u003e\r\n\t\u003cversion\u003e3.1.0\u003c/version\u003e\r\n\u003c/dependency\u003e\r\n\r\n#NOT: WebServlet annotation is included with Servlet 3.0 technology !\r\n\r\n1. Generic Servlet Class\r\n2. HTTP Servlet Class\r\n\r\nEvery servlet implements a base interface called 'Servlet'\r\n\r\nServlet Intefacse is about;\r\n\t- How to configure a servlet\r\n\t- LifeCycle of servlet\r\n\t\t. Init\t\t\t : Initialization of the Servelt\r\n\t\t. Service method : Takes 2 parameters, ServletRequest and ServletResponse objects\r\n\t\t. Destroy\t\t : Destruction of the Servlet\r\n\r\nThe most important method in here is the service (Servlet.service) method because\r\nmost of this lesson is about the service.\r\n\r\nServletRequest object \t: Details of the incoming request, request headers, any request parameters\r\nServletResponse object \t: genereate the outgoing response\r\n\r\nServlet Interface\r\n*****************\r\n\r\npublic interface Servlet\r\n{\r\n\tpublic void init(ServletConfig);\r\n\tpublic void service(ServletRequest req, Servlet Response resp);\r\n\tpublic void destroy();\r\n\tpublic ServletConfig getServletConfig();\r\n\tpublic String getServletInfo();\r\n}\r\n\r\nServlet LifeCycle\r\n*****************\r\nA servlet follows a certain life cycle. The servlet life cycle is managed by the servlet container.\r\nThe life cycle contains the following steps:\r\n\r\n\t1. Load Servlet Class.\r\n\t2. Create Instance of Servlet.\r\n\t3. Call the servlets init() method.\r\n\t4. Call the servlets service() method.\r\n\t5. Call the servlets destroy() method.\r\n\r\nStep 1, 2 and 3 are executed only once, when the servlet is initially loaded.\r\nBy default the servlet is not loaded until the first request is received for it.\r\nYou can force the container to load the servlet when the container starts up though.\r\nSee web.xml Servlet Configuration for more details about that.\r\n\r\nStep 4 is executed multiple times - once for every HTTP request to the servlet.\r\nStep 5 is executed when the servlet container unloads the servlet.\r\n\r\n\r\nIMPLEMENTING SERVLET INTERFACE\r\n******************************\r\nWe should not tend to implement Servlet Interface directly, the library provides couple of\r\nhelper classes.\r\n\r\n\t1. GenericServlet Class\r\n\t2. HttpServlet Class\r\n\t\r\nGenericServlet Class\r\n********************\r\nGenericServlet is protocol agnostic, it's not protocol based.\r\n\r\npublic abstract class GenericServlet implements Servlet, \r\n\t\t\t\t\t\t\t\t\t\t\t\tServletConfig, \r\n\t\t\t\t\t\t\t\t\t\t\t\tjava.io.Serializable\r\n{\r\n}\r\n\r\nHttpServlet Class\r\n*****************\r\nProtocol based (protocol specific class), extends GenericServlet.\r\n\r\npublic class HttpServlet extends GenericServlet\r\n{\r\n\tpublic void service(ServletRequest req, ServletResponse resp)\r\n\t{\r\n\t\tservice( (HttpServletRequest) req, (HttpServletResponse) resp );\r\n\t}\r\n\t\r\n\tpublic void service(HttpServletRequest req, HttpServletResponse resp)\r\n\t{\r\n\t\tString verb = req.getMethod();\r\n\t\t\r\n\t\tif (verb.equals(\"GET\"))\r\n\t\t\tdoGet (req, resp);\r\n\t\telse if (verb.equals(\"POST\"))\r\n\t\t\tdoPost (req, resp);\r\n\t\telse...\r\n\t}\r\n\t\r\n\tpublic void doGet(HttpServletRequest req, HttpServletResponse resp)\r\n\t{\r\n\t\t...\r\n\t}\r\n\t\r\n\tpublic void doPost(HttpServletRequest req, HttpServletResponse resp)\r\n\t{\r\n\t\t...\r\n\t}\r\n}\r\n\r\nURL-CLASS MAPPING\r\n*****************\r\n*****************\r\nOk, we want to map 2 different class to 2 paths one by one;\r\n\r\ncom.levent.ServletHelloWorld\t-\u003e\t/hello\r\ncom.levent.SimpleServlet\t\t-\u003e\t/home\r\n\r\n\t1) Via WEB-INF/Web.xml\r\n\t**********************\r\n\t\u003cweb-app xmlns=\"http://java.sun.com/xml/ns/javaee\"\r\n\t\t\t  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n\t\t\t  xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee \r\n\t\t\t  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\"\r\n\t\t\t  version=\"3.0\"\u003e\r\n\t\t\r\n\t\t\u003c!-- SimpleServlet is mapped to servlet1 --\u003e\r\n\t\t\u003cservlet\u003e\r\n\t\t\t\u003cservlet-name\u003eservlet1\u003c/servlet-name\u003e\r\n\t\t\t\u003cservlet-class\u003ecom.levent.SimpleServlet\u003c/servlet-class\u003e\r\n\t\t\u003c/servlet\u003e\r\n\t\t\r\n\t\t\u003c!-- servlet1 is mapped to /home --\u003e\r\n\t\t\u003cservlet-mapping\u003e\r\n\t\t\t\u003cservlet-name\u003eservlet1\u003c/servlet-name\u003e\r\n\t\t\t\u003curl-pattern\u003e/home\u003c/url-pattern\u003e\r\n\t\t\u003c/servlet-mapping\u003e\r\n\t\t\r\n\t\t\r\n\t\t\u003c!-- ServletHelloWorld is mapped to servlet2 --\u003e\r\n\t\t\u003cservlet\u003e\r\n\t\t\t\u003cservlet-name\u003eservlet2\u003c/servlet-name\u003e\r\n\t\t\t\u003cservlet-class\u003ecom.levent.ServletHelloWorld\u003c/servlet-class\u003e\r\n\t\t\u003c/servlet\u003e\r\n\t\t\r\n\t\t\u003c!-- servlet2 is mapped to /hello --\u003e\r\n\t\t\u003cservlet-mapping\u003e\r\n\t\t\t\u003cservlet-name\u003eservlet2\u003c/servlet-name\u003e\r\n\t\t\t\u003curl-pattern\u003e/hello\u003c/url-pattern\u003e\r\n\t\t\u003c/servlet-mapping\u003e \r\n\t\t\r\n\t\u003c/web-app\u003e\r\n\r\n\t2) With annotations;\r\n\t********************\r\n\tJust leave WEB-INF/web.xml empty as below;\r\n\r\n\t\u003cweb-app xmlns=\"http://java.sun.com/xml/ns/javaee\"\r\n\t\t\t  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n\t\t\t  xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee \r\n\t\t\t  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\"\r\n\t\t\t  version=\"3.0\"\u003e\r\n\t\t\t  \r\n\t\u003c/web-app\u003e\r\n\r\n\tOn each of the classes add the annotation on just top of the class definition as below;\r\n\t@WebServlet(\"/home\")\r\n\tpublic class SimpleServlet extends HttpServlet\r\n\r\n\t@WebServlet(\"/hello\")\r\n\tpublic class ServletHelloWorld extends HttpServlet\r\n\r\nNow you can reach the classes with;\r\n\r\nhttp://localhost:8080/SimpleServletWebApp/hello\r\nhttp://localhost:8080/SimpleServletWebApp/home\r\n\r\nAnd please notify that (from the WebServlet class;\r\n/**\r\n * Annotation used to declare a servlet.\r\n *\r\n * \u003cp\u003eThis annotation is processed by the container at deployment time,\r\n * and the corresponding servlet made available at the specified URL\r\n * patterns.\r\n * \r\n * @see javax.servlet.Servlet\r\n *\r\n * @since Servlet 3.0\r\n */\r\n\r\n# project link: https://github.com/bzdgn/SimpleServletWebApp\r\n# ssh link: \tgit@github.com:bzdgn/SimpleServletWebApp.git\r\n# link: \t\thttp://localhost:8080/SimpleServletWebApp/Simple/home","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbzdgn%2Fsimpleservletwebapp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbzdgn%2Fsimpleservletwebapp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbzdgn%2Fsimpleservletwebapp/lists"}