├── .gitignore ├── LICENSE ├── ListDirectoryContentInGitFormat.md ├── README.md ├── Step01.md ├── Step02.md ├── Step03.md ├── Step04.md ├── Step05.md ├── Step06.md ├── Step07.md ├── Step08.md ├── Step09.md ├── Step10.md ├── Step11.md ├── Step13.md ├── Step14.md ├── Step15.md ├── Step17.md ├── Step20.md ├── Step24.md ├── Step25.md ├── Step27.md ├── pom.xml └── src └── main ├── java └── com │ └── in28minutes │ ├── filter │ └── LoginRequiredFilter.java │ ├── login │ ├── LoginService.java │ └── LoginServlet.java │ ├── logout │ └── LogoutServlet.java │ └── todo │ ├── AddTodoServlet.java │ ├── DeleteTodoServlet.java │ ├── ListTodoServlet.java │ ├── Todo.java │ └── TodoService.java └── webapp └── WEB-INF ├── common ├── footer.jspf ├── header.jspf └── navigation.jspf ├── views ├── add-todo.jsp ├── list-todos.jsp └── login.jsp └── web.xml /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | *.cmd 21 | *.classpath 22 | *.settings 23 | *.project 24 | *.mvn 25 | mvnw 26 | target 27 | *.DS_Store 28 | 29 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 30 | hs_err_pid* 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /ListDirectoryContentInGitFormat.md: -------------------------------------------------------------------------------- 1 | ListDirectoryContentInGitFormat 2 | ``` 3 | package test; 4 | import java.io.BufferedReader; 5 | import java.io.File; 6 | import java.io.FileNotFoundException; 7 | import java.io.FileReader; 8 | import java.io.IOException; 9 | 10 | public class ListDirectoryContentInGitFormat { 11 | 12 | public static final String DIRECTORY_WINDOWS = "C:\\Users\\rrkaranam\\workspace\\webapp\\"; 13 | 14 | public static void main(String[] args) throws FileNotFoundException, IOException { 15 | ListDirectoryContentInGitFormat listFilesUtil = new ListDirectoryContentInGitFormat(); 16 | 17 | listFilesUtil.listFilesAndFilesSubDirectories(DIRECTORY_WINDOWS); 18 | 19 | } 20 | 21 | /** 22 | * List all files from a directory and its subdirectories 23 | * 24 | * @param rootDirectory 25 | * to be listed 26 | * @throws IOException 27 | * @throws FileNotFoundException 28 | */ 29 | public void listFilesAndFilesSubDirectories(String rootDirectory) throws FileNotFoundException, IOException { 30 | File directory = new File(rootDirectory); 31 | // get all the files from a directory 32 | File[] fList = directory.listFiles(); 33 | for (File file : fList) { 34 | if (file.getName().startsWith(".") || file.getName().startsWith("target")) 35 | continue; 36 | 37 | if (file.isFile()) { 38 | printContent(file.getAbsolutePath()); 39 | } else if (file.isDirectory()) { 40 | listFilesAndFilesSubDirectories(file.getAbsolutePath()); 41 | } 42 | } 43 | } 44 | 45 | private void printContent(String absolutePath) throws FileNotFoundException, IOException { 46 | System.out.println(absolutePath.replace(DIRECTORY_WINDOWS, "")); 47 | System.out.println("```"); 48 | try (BufferedReader br = new BufferedReader(new FileReader(absolutePath))) { 49 | String line = null; 50 | while ((line = br.readLine()) != null) { 51 | System.out.println(line); 52 | } 53 | } 54 | System.out.println("```"); 55 | } 56 | 57 | } 58 | ``` 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java Web Application in 25 Easy Steps 2 | 3 | [![Image](https://www.springboottutorial.com/images/Course-Java-Servlets-and-JSP-BuildJavaEEAppIn25Steps.png "Java Servlets and JSP - Build Java EE(JEE) app in 25 Steps")](https://www.udemy.com/course/learn-java-servlets-and-jsp-web-application-in-25-steps/) 4 | 5 | ## Learn Java Web Application Development with JSPs and Servlets Easy! 6 | 7 | ### Installing Eclipse and Java 8 | 9 | https://github.com/in28minutes/SpringIn28Minutes/blob/master/InstallationGuide-JavaEclipseAndMaven_v2.pdf 10 | 11 | ### Running Examples 12 | - If you are downloading the zip file, unzip the file 13 | - Open Command Prompt and Change directory to folder containing pom.xml 14 | - Run command "mvn tomcat7:run" 15 | - For help : user our installation guide - https://github.com/in28minutes/SpringIn28Minutes/blob/master/InstallationGuide-JavaEclipseAndMaven_v2.pdf 16 | 17 | ### File wise Contents 18 | - Step01.md : Up and running with a web app in Tomcat 19 | - Step02.md : First JSP 20 | - Step03.md : Adding a GET Parameter name 21 | - Step04.md : Adding another Get Parameter Password 22 | - Step05.md : Lets add a form 23 | - Step06.md : New Form and doPost 24 | - Step07.md : Adding Password, validation of userid/password 25 | - Step08.md : Adding a TodoService and Todos to welcome.jsp 26 | - Step09.md : Bit of Refactoring - Packages 9 days ago 27 | - Step10.md : Redirect from One Servlet to another - New TodoServlet. 28 | - Step11.md : First JSTL Tag : Using a Loop around todos 29 | - Step12 : Difference between Session and Request Scopes 30 | - Step13.md : Add a New Todo 31 | - Step14.md : Delete Todo with equals and hashcode methods 32 | - Step15.md : Adding webjars for jquery and bootstrap 33 | - Step16 : Missing Step :) We want you to take a break. Nothing in here.. 34 | - Step17.md : updating Bootstrap to 3.3.6 35 | - Step 18 (Code file missing) : Refactoring: list-todos.do,list-todos.jsp,ListTodosServlet. Refer Next Step for code. 36 | - Step 19 (Code file missing) : Adding a Filter for More Security. Refer Next Step for code. 37 | - Step20.md : Logout 38 | - Step 21 : Theory : Understand Maven and Tomcat 39 | - Step 22 : Theory : Servlet LifeCycle 40 | - Step 23 : Theory : Model 1 and Model 2 MVC Architectures 41 | - Step24.md : Moving Add Functionality to a New Page. 42 | - Step25.md : Add Category Field 43 | - Step26(Code file missing) : Format the jsps better. Refer Next Step for code. 44 | - Step27.md : Jsp Fragments 45 | 46 | ### Troubleshooting 47 | - Refer our TroubleShooting Guide - https://github.com/in28minutes/in28minutes-initiatives/tree/master/The-in28Minutes-TroubleshootingGuide-And-FAQ 48 | 49 | ## Youtube Playlists - 500+ Videos 50 | 51 | [Click here - 30+ Playlists with 500+ Videos on Spring, Spring Boot, REST, Microservices and the Cloud](https://www.youtube.com/user/rithustutorials/playlists?view=1&sort=lad&flow=list) 52 | 53 | ## Keep Learning in28Minutes 54 | 55 | in28Minutes is creating amazing solutions for you to learn Spring Boot, Full Stack and the Cloud - Docker, Kubernetes, AWS, React, Angular etc. - [Check out all our courses here](https://github.com/in28minutes/learn) 56 | -------------------------------------------------------------------------------- /Step01.md: -------------------------------------------------------------------------------- 1 | \pom.xml 2 | ``` 3 | 5 | 4.0.0 6 | com.in28minutes 7 | in28Minutes-first-webapp 8 | 0.0.1-SNAPSHOT 9 | war 10 | 11 | 12 | 13 | javax 14 | javaee-web-api 15 | 6.0 16 | provided 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | org.apache.maven.plugins 25 | maven-compiler-plugin 26 | 3.2 27 | 28 | true 29 | 1.7 30 | 1.7 31 | true 32 | 33 | 34 | 35 | org.apache.tomcat.maven 36 | tomcat7-maven-plugin 37 | 2.2 38 | 39 | / 40 | true 41 | 42 | 43 | 44 | 45 | 46 | 47 | ``` 48 | \src\main\java\webapp\LoginServlet.java 49 | ``` 50 | package webapp; 51 | 52 | import java.io.IOException; 53 | import java.io.PrintWriter; 54 | 55 | import javax.servlet.annotation.WebServlet; 56 | import javax.servlet.http.HttpServlet; 57 | import javax.servlet.http.HttpServletRequest; 58 | import javax.servlet.http.HttpServletResponse; 59 | 60 | /* 61 | * Browser sends Http Request to Web Server 62 | * 63 | * Code in Web Server => Input:HttpRequest, Output: HttpResponse 64 | * JEE with Servlets 65 | * 66 | * Web Server responds with Http Response 67 | */ 68 | 69 | //Java Platform, Enterprise Edition (Java EE) JEE6 70 | 71 | //Servlet is a Java programming language class 72 | //used to extend the capabilities of servers 73 | //that host applications accessed by means of 74 | //a request-response programming model. 75 | 76 | //1. extends javax.servlet.http.HttpServlet 77 | //2. @WebServlet(urlPatterns = "/login.do") 78 | //3. doGet(HttpServletRequest request, HttpServletResponse response) 79 | //4. How is the response created? 80 | 81 | @WebServlet(urlPatterns = "/login.do") 82 | public class LoginServlet extends HttpServlet { 83 | 84 | @Override 85 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 86 | PrintWriter out = response.getWriter(); 87 | out.println(""); 88 | out.println(""); 89 | out.println("Yahoo!!!!!!!!"); 90 | out.println(""); 91 | out.println(""); 92 | out.println("My First Servlet"); 93 | out.println(""); 94 | out.println(""); 95 | 96 | } 97 | 98 | } 99 | ``` 100 | \src\main\webapp\WEB-INF\web.xml 101 | ``` 102 | 103 | 106 | 107 | To do List 108 | 109 | 110 | login.do 111 | 112 | 113 | 114 | ``` 115 | -------------------------------------------------------------------------------- /Step02.md: -------------------------------------------------------------------------------- 1 | ## What we will do? 2 | - Create LoginServlet again 3 | - Redirect to a view - JSP 4 | 5 | ## File Listing 6 | \pom.xml 7 | ``` 8 | 10 | 4.0.0 11 | com.in28minutes 12 | in28Minutes-first-webapp 13 | 0.0.1-SNAPSHOT 14 | war 15 | 16 | 17 | 18 | javax 19 | javaee-web-api 20 | 6.0 21 | provided 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | org.apache.maven.plugins 30 | maven-compiler-plugin 31 | 3.2 32 | 33 | true 34 | 1.7 35 | 1.7 36 | true 37 | 38 | 39 | 40 | org.apache.tomcat.maven 41 | tomcat7-maven-plugin 42 | 2.2 43 | 44 | / 45 | true 46 | 47 | 48 | 49 | 50 | 51 | 52 | ``` 53 | \src\main\java\webapp\LoginServlet.java 54 | ``` 55 | package webapp; 56 | 57 | import java.io.IOException; 58 | 59 | import javax.servlet.ServletException; 60 | import javax.servlet.annotation.WebServlet; 61 | import javax.servlet.http.HttpServlet; 62 | import javax.servlet.http.HttpServletRequest; 63 | import javax.servlet.http.HttpServletResponse; 64 | 65 | @WebServlet(urlPatterns = "/login.do") 66 | public class LoginServlet extends HttpServlet { 67 | 68 | @Override 69 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 70 | throws IOException, ServletException { 71 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 72 | } 73 | 74 | } 75 | ``` 76 | \src\main\webapp\WEB-INF\views\login.jsp 77 | ``` 78 | 79 | 80 | Yahoo!! 81 | 82 | 83 | My First JSP!!! 84 | 85 | 86 | ``` 87 | \src\main\webapp\WEB-INF\web.xml 88 | ``` 89 | 90 | 93 | 94 | To do List 95 | 96 | 97 | login.do 98 | 99 | 100 | 101 | ``` 102 | -------------------------------------------------------------------------------- /Step03.md: -------------------------------------------------------------------------------- 1 | ## What we will do? 2 | - Passing a Request Parameter Name 3 | 4 | ## File Listing 5 | \pom.xml 6 | ``` 7 | 9 | 4.0.0 10 | com.in28minutes 11 | in28Minutes-first-webapp 12 | 0.0.1-SNAPSHOT 13 | war 14 | 15 | 16 | 17 | javax 18 | javaee-web-api 19 | 6.0 20 | provided 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | org.apache.maven.plugins 29 | maven-compiler-plugin 30 | 3.2 31 | 32 | true 33 | 1.7 34 | 1.7 35 | true 36 | 37 | 38 | 39 | org.apache.tomcat.maven 40 | tomcat7-maven-plugin 41 | 2.2 42 | 43 | / 44 | true 45 | 46 | 47 | 48 | 49 | 50 | 51 | ``` 52 | \src\main\java\webapp\LoginServlet.java 53 | ``` 54 | package webapp; 55 | 56 | import java.io.IOException; 57 | 58 | import javax.servlet.ServletException; 59 | import javax.servlet.annotation.WebServlet; 60 | import javax.servlet.http.HttpServlet; 61 | import javax.servlet.http.HttpServletRequest; 62 | import javax.servlet.http.HttpServletResponse; 63 | 64 | @WebServlet(urlPatterns = "/login.do") 65 | public class LoginServlet extends HttpServlet { 66 | 67 | @Override 68 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 69 | throws IOException, ServletException { 70 | request.setAttribute("name", request.getParameter("name")); 71 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 72 | } 73 | 74 | } 75 | ``` 76 | \src\main\webapp\WEB-INF\views\login.jsp 77 | ``` 78 | 79 | 80 | Yahoo!! 81 | 82 | 83 | My First JSP!!! My name is ${name} 84 | 85 | 86 | ``` 87 | \src\main\webapp\WEB-INF\web.xml 88 | ``` 89 | 90 | 93 | 94 | To do List 95 | 96 | 97 | login.do 98 | 99 | 100 | 101 | ``` 102 | -------------------------------------------------------------------------------- /Step04.md: -------------------------------------------------------------------------------- 1 | \pom.xml 2 | ``` 3 | 5 | 4.0.0 6 | com.in28minutes 7 | in28Minutes-first-webapp 8 | 0.0.1-SNAPSHOT 9 | war 10 | 11 | 12 | 13 | javax 14 | javaee-web-api 15 | 6.0 16 | provided 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | org.apache.maven.plugins 25 | maven-compiler-plugin 26 | 3.2 27 | 28 | true 29 | 1.7 30 | 1.7 31 | true 32 | 33 | 34 | 35 | org.apache.tomcat.maven 36 | tomcat7-maven-plugin 37 | 2.2 38 | 39 | / 40 | true 41 | 42 | 43 | 44 | 45 | 46 | 47 | ``` 48 | \src\main\java\webapp\LoginServlet.java 49 | ``` 50 | package webapp; 51 | 52 | import java.io.IOException; 53 | 54 | import javax.servlet.ServletException; 55 | import javax.servlet.annotation.WebServlet; 56 | import javax.servlet.http.HttpServlet; 57 | import javax.servlet.http.HttpServletRequest; 58 | import javax.servlet.http.HttpServletResponse; 59 | 60 | @WebServlet(urlPatterns = "/login.do") 61 | public class LoginServlet extends HttpServlet { 62 | 63 | @Override 64 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 65 | throws IOException, ServletException { 66 | request.setAttribute("name", request.getParameter("name")); 67 | request.setAttribute("password", request.getParameter("password")); 68 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 69 | } 70 | 71 | } 72 | ``` 73 | \src\main\webapp\WEB-INF\views\login.jsp 74 | ``` 75 | 76 | 77 | Yahoo!! 78 | 79 | 80 | My First JSP!!! My name is ${name} and password is ${password} 81 | 82 | 83 | ``` 84 | \src\main\webapp\WEB-INF\web.xml 85 | ``` 86 | 87 | 90 | 91 | To do List 92 | 93 | 94 | login.do 95 | 96 | 97 | 98 | ``` 99 | -------------------------------------------------------------------------------- /Step05.md: -------------------------------------------------------------------------------- 1 | \pom.xml 2 | ``` 3 | 5 | 4.0.0 6 | com.in28minutes 7 | in28Minutes-first-webapp 8 | 0.0.1-SNAPSHOT 9 | war 10 | 11 | 12 | 13 | javax 14 | javaee-web-api 15 | 6.0 16 | provided 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | org.apache.maven.plugins 25 | maven-compiler-plugin 26 | 3.2 27 | 28 | true 29 | 1.7 30 | 1.7 31 | true 32 | 33 | 34 | 35 | org.apache.tomcat.maven 36 | tomcat7-maven-plugin 37 | 2.2 38 | 39 | / 40 | true 41 | 42 | 43 | 44 | 45 | 46 | 47 | ``` 48 | \src\main\java\webapp\LoginServlet.java 49 | ``` 50 | package webapp; 51 | 52 | import java.io.IOException; 53 | 54 | import javax.servlet.ServletException; 55 | import javax.servlet.annotation.WebServlet; 56 | import javax.servlet.http.HttpServlet; 57 | import javax.servlet.http.HttpServletRequest; 58 | import javax.servlet.http.HttpServletResponse; 59 | 60 | 61 | @WebServlet(urlPatterns = "/login.do") 62 | public class LoginServlet extends HttpServlet { 63 | 64 | 65 | @Override 66 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 67 | throws IOException, ServletException { 68 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 69 | } 70 | 71 | } 72 | ``` 73 | \\src\main\webapp\WEB-INF\views\login.jsp 74 | ``` 75 | 76 | 77 | Yahoo!! 78 | 79 | 80 |
81 | Name : 82 |
83 | 84 | 85 | ``` 86 | \src\main\webapp\WEB-INF\views\welcome.jsp 87 | ``` 88 | 89 | 90 | Yahoo!! 91 | 92 | 93 | Welcome ${name} 94 | 95 | 96 | ``` 97 | \src\main\webapp\WEB-INF\web.xml 98 | ``` 99 | 100 | 103 | 104 | To do List 105 | 106 | 107 | login.do 108 | 109 | 110 | 111 | ``` 112 | -------------------------------------------------------------------------------- /Step06.md: -------------------------------------------------------------------------------- 1 | \pom.xml 2 | ``` 3 | 5 | 4.0.0 6 | com.in28minutes 7 | in28Minutes-first-webapp 8 | 0.0.1-SNAPSHOT 9 | war 10 | 11 | 12 | 13 | javax 14 | javaee-web-api 15 | 6.0 16 | provided 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | org.apache.maven.plugins 25 | maven-compiler-plugin 26 | 3.2 27 | 28 | true 29 | 1.7 30 | 1.7 31 | true 32 | 33 | 34 | 35 | org.apache.tomcat.maven 36 | tomcat7-maven-plugin 37 | 2.2 38 | 39 | / 40 | true 41 | 42 | 43 | 44 | 45 | 46 | 47 | ``` 48 | \src\main\java\webapp\LoginServlet.java 49 | ``` 50 | package webapp; 51 | 52 | import java.io.IOException; 53 | 54 | import javax.servlet.ServletException; 55 | import javax.servlet.annotation.WebServlet; 56 | import javax.servlet.http.HttpServlet; 57 | import javax.servlet.http.HttpServletRequest; 58 | import javax.servlet.http.HttpServletResponse; 59 | 60 | 61 | @WebServlet(urlPatterns = "/login.do") 62 | public class LoginServlet extends HttpServlet { 63 | 64 | 65 | @Override 66 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 67 | throws IOException, ServletException { 68 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 69 | } 70 | 71 | @Override 72 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 73 | throws IOException, ServletException { 74 | request.setAttribute("name", request.getParameter("name")); 75 | request.getRequestDispatcher("/WEB-INF/views/welcome.jsp").forward(request, response); 76 | } 77 | 78 | } 79 | ``` 80 | \\src\main\webapp\WEB-INF\views\login.jsp 81 | ``` 82 | 83 | 84 | Yahoo!! 85 | 86 | 87 |
88 | Name : 89 |
90 | 91 | 92 | ``` 93 | \src\main\webapp\WEB-INF\views\welcome.jsp 94 | ``` 95 | 96 | 97 | Yahoo!! 98 | 99 | 100 | Welcome ${name} 101 | 102 | 103 | ``` 104 | \src\main\webapp\WEB-INF\web.xml 105 | ``` 106 | 107 | 110 | 111 | To do List 112 | 113 | 114 | login.do 115 | 116 | 117 | ``` 118 | -------------------------------------------------------------------------------- /Step07.md: -------------------------------------------------------------------------------- 1 | \pom.xml 2 | ``` 3 | 5 | 4.0.0 6 | com.in28minutes 7 | in28Minutes-first-webapp 8 | 0.0.1-SNAPSHOT 9 | war 10 | 11 | 12 | 13 | javax 14 | javaee-web-api 15 | 6.0 16 | provided 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | org.apache.maven.plugins 25 | maven-compiler-plugin 26 | 3.2 27 | 28 | true 29 | 1.7 30 | 1.7 31 | true 32 | 33 | 34 | 35 | org.apache.tomcat.maven 36 | tomcat7-maven-plugin 37 | 2.2 38 | 39 | / 40 | true 41 | 42 | 43 | 44 | 45 | 46 | 47 | ``` 48 | \src\main\java\webapp\LoginService.java 49 | ``` 50 | package webapp; 51 | 52 | public class LoginService { 53 | public boolean validateUser(String user, String password) { 54 | return user.equalsIgnoreCase("in28Minutes") && password.equals("dummy"); 55 | } 56 | 57 | } 58 | ``` 59 | \src\main\java\webapp\LoginServlet.java 60 | ``` 61 | package webapp; 62 | 63 | import java.io.IOException; 64 | 65 | import javax.servlet.ServletException; 66 | import javax.servlet.annotation.WebServlet; 67 | import javax.servlet.http.HttpServlet; 68 | import javax.servlet.http.HttpServletRequest; 69 | import javax.servlet.http.HttpServletResponse; 70 | 71 | 72 | @WebServlet(urlPatterns = "/login.do") 73 | public class LoginServlet extends HttpServlet { 74 | 75 | private LoginService service = new LoginService(); 76 | 77 | @Override 78 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 79 | throws IOException, ServletException { 80 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 81 | } 82 | 83 | @Override 84 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 85 | throws IOException, ServletException { 86 | String name = request.getParameter("name"); 87 | String password = request.getParameter("password"); 88 | 89 | boolean isValidUser = service.validateUser(name, password); 90 | 91 | if (isValidUser) { 92 | request.setAttribute("name", name); 93 | request.getRequestDispatcher("/WEB-INF/views/welcome.jsp").forward(request, response); 94 | } else { 95 | request.setAttribute("errorMessage", "Invalid Credentials!!"); 96 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 97 | } 98 | } 99 | 100 | } 101 | ``` 102 | \\src\main\webapp\WEB-INF\views\login.jsp 103 | ``` 104 | 105 | 106 | Yahoo!! 107 | 108 | 109 |

${errorMessage}

110 |
111 | Name : Password : 112 |
113 | 114 | 115 | ``` 116 | \src\main\webapp\WEB-INF\views\welcome.jsp 117 | ``` 118 | 119 | 120 | Yahoo!! 121 | 122 | 123 | Welcome ${name} 124 | 125 | 126 | ``` 127 | \src\main\webapp\WEB-INF\web.xml 128 | ``` 129 | 130 | 133 | 134 | To do List 135 | 136 | 137 | login.do 138 | 139 | 140 | ``` 141 | -------------------------------------------------------------------------------- /Step08.md: -------------------------------------------------------------------------------- 1 | \pom.xml 2 | ``` 3 | 5 | 4.0.0 6 | com.in28minutes 7 | in28Minutes-first-webapp 8 | 0.0.1-SNAPSHOT 9 | war 10 | 11 | 12 | 13 | javax 14 | javaee-web-api 15 | 6.0 16 | provided 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | org.apache.maven.plugins 25 | maven-compiler-plugin 26 | 3.2 27 | 28 | true 29 | 1.7 30 | 1.7 31 | true 32 | 33 | 34 | 35 | org.apache.tomcat.maven 36 | tomcat7-maven-plugin 37 | 2.2 38 | 39 | / 40 | true 41 | 42 | 43 | 44 | 45 | 46 | 47 | ``` 48 | \src\main\java\webapp\LoginService.java 49 | ``` 50 | package webapp; 51 | 52 | public class LoginService { 53 | public boolean validateUser(String user, String password) { 54 | return user.equalsIgnoreCase("in28Minutes") && password.equals("dummy"); 55 | } 56 | 57 | } 58 | ``` 59 | \src\main\java\webapp\LoginServlet.java 60 | ``` 61 | package webapp; 62 | 63 | import java.io.IOException; 64 | 65 | import javax.servlet.ServletException; 66 | import javax.servlet.annotation.WebServlet; 67 | import javax.servlet.http.HttpServlet; 68 | import javax.servlet.http.HttpServletRequest; 69 | import javax.servlet.http.HttpServletResponse; 70 | 71 | 72 | import webapp.todo.TodoService; 73 | 74 | @WebServlet(urlPatterns = "/login.do") 75 | public class LoginServlet extends HttpServlet { 76 | 77 | private LoginService service = new LoginService(); 78 | private TodoService todoService = new TodoService(); 79 | 80 | @Override 81 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 82 | throws IOException, ServletException { 83 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 84 | } 85 | 86 | @Override 87 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 88 | throws IOException, ServletException { 89 | String name = request.getParameter("name"); 90 | String password = request.getParameter("password"); 91 | 92 | boolean isValidUser = service.validateUser(name, password); 93 | 94 | if (isValidUser) { 95 | request.setAttribute("name", name); 96 | request.setAttribute("todos", todoService.retrieveTodos()); 97 | request.getRequestDispatcher("/WEB-INF/views/welcome.jsp").forward(request, response); 98 | } else { 99 | request.setAttribute("errorMessage", "Invalid Credentials!!"); 100 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 101 | } 102 | } 103 | 104 | } 105 | ``` 106 | \src\main\java\webapp\todo\Todo.java 107 | ``` 108 | package webapp.todo; 109 | 110 | public class Todo { 111 | 112 | public Todo(String name) { 113 | super(); 114 | this.name = name; 115 | } 116 | 117 | private String name; 118 | 119 | public String getName() { 120 | return name; 121 | } 122 | 123 | public void setName(String name) { 124 | this.name = name; 125 | } 126 | 127 | @Override 128 | public String toString() { 129 | return "Todo [name=" + name + "]"; 130 | } 131 | } 132 | ``` 133 | \src\main\java\webapp\todo\TodoService.java 134 | ``` 135 | package webapp.todo; 136 | 137 | import java.util.ArrayList; 138 | import java.util.List; 139 | 140 | public class TodoService { 141 | private static List todos = new ArrayList(); 142 | 143 | static { 144 | todos.add(new Todo("Learn Web Application")); 145 | todos.add(new Todo("Learn Spring")); 146 | todos.add(new Todo("Learn Spring MVC")); 147 | } 148 | 149 | public List retrieveTodos() { 150 | return todos; 151 | } 152 | } 153 | ``` 154 | \\src\main\webapp\WEB-INF\views\login.jsp 155 | ``` 156 | 157 | 158 | Yahoo!! 159 | 160 | 161 |

${errorMessage}

162 |
163 | Name : Password : 164 |
165 | 166 | 167 | ``` 168 | \src\main\webapp\WEB-INF\views\welcome.jsp 169 | ``` 170 | 171 | 172 | Yahoo!! 173 | 174 | 175 |

Welcome ${name}

176 |
177 | Your Todos are 178 | ${todos} 179 |
180 | 181 | 182 | ``` 183 | \src\main\webapp\WEB-INF\web.xml 184 | ``` 185 | 186 | 189 | 190 | To do List 191 | 192 | 193 | login.do 194 | 195 | 196 | ``` 197 | -------------------------------------------------------------------------------- /Step09.md: -------------------------------------------------------------------------------- 1 | pom.xml 2 | ``` 3 | 5 | 4.0.0 6 | com.in28minutes 7 | in28Minutes-first-webapp 8 | 0.0.1-SNAPSHOT 9 | war 10 | 11 | 12 | 13 | javax 14 | javaee-web-api 15 | 6.0 16 | provided 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | org.apache.maven.plugins 25 | maven-compiler-plugin 26 | 3.2 27 | 28 | true 29 | 1.7 30 | 1.7 31 | true 32 | 33 | 34 | 35 | org.apache.tomcat.maven 36 | tomcat7-maven-plugin 37 | 2.2 38 | 39 | / 40 | true 41 | 42 | 43 | 44 | 45 | 46 | 47 | ``` 48 | src\main\java\in28minutes\login\LoginService.java 49 | ``` 50 | package in28minutes.login; 51 | 52 | public class LoginService { 53 | public boolean validateUser(String user, String password) { 54 | return user.equalsIgnoreCase("in28Minutes") && password.equals("dummy"); 55 | } 56 | 57 | } 58 | ``` 59 | src\main\java\in28minutes\login\LoginServlet.java 60 | ``` 61 | package in28minutes.login; 62 | 63 | import java.io.IOException; 64 | 65 | import javax.servlet.ServletException; 66 | import javax.servlet.annotation.WebServlet; 67 | import javax.servlet.http.HttpServlet; 68 | import javax.servlet.http.HttpServletRequest; 69 | import javax.servlet.http.HttpServletResponse; 70 | 71 | 72 | import in28minutes.todo.TodoService; 73 | 74 | @WebServlet(urlPatterns = "/login.do") 75 | public class LoginServlet extends HttpServlet { 76 | 77 | private static final long serialVersionUID = 1L; 78 | private LoginService service = new LoginService(); 79 | private TodoService todoService = new TodoService(); 80 | 81 | @Override 82 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 83 | throws IOException, ServletException { 84 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 85 | } 86 | 87 | @Override 88 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 89 | throws IOException, ServletException { 90 | String name = request.getParameter("name"); 91 | String password = request.getParameter("password"); 92 | 93 | boolean isValidUser = service.validateUser(name, password); 94 | 95 | if (isValidUser) { 96 | request.setAttribute("name", name); 97 | request.setAttribute("todos", todoService.retrieveTodos()); 98 | request.getRequestDispatcher("/WEB-INF/views/welcome.jsp").forward(request, response); 99 | } else { 100 | request.setAttribute("errorMessage", "Invalid Credentials!!"); 101 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 102 | } 103 | } 104 | 105 | } 106 | ``` 107 | src\main\java\in28minutes\todo\Todo.java 108 | ``` 109 | package in28minutes.todo; 110 | 111 | public class Todo { 112 | 113 | public Todo(String name) { 114 | super(); 115 | this.name = name; 116 | } 117 | 118 | private String name; 119 | 120 | public String getName() { 121 | return name; 122 | } 123 | 124 | public void setName(String name) { 125 | this.name = name; 126 | } 127 | 128 | @Override 129 | public String toString() { 130 | return "Todo [name=" + name + "]"; 131 | } 132 | } 133 | ``` 134 | src\main\java\in28minutes\todo\TodoService.java 135 | ``` 136 | package in28minutes.todo; 137 | 138 | import java.util.ArrayList; 139 | import java.util.List; 140 | 141 | public class TodoService { 142 | private static List todos = new ArrayList(); 143 | 144 | static { 145 | todos.add(new Todo("Learn Web Application")); 146 | todos.add(new Todo("Learn Spring")); 147 | todos.add(new Todo("Learn Spring MVC")); 148 | } 149 | 150 | public List retrieveTodos() { 151 | return todos; 152 | } 153 | } 154 | ``` 155 | src\main\webapp\WEB-INF\views\login.jsp 156 | ``` 157 | 158 | 159 | Yahoo!! 160 | 161 | 162 |

${errorMessage}

163 |
164 | Name : Password : 165 |
166 | 167 | 168 | ``` 169 | src\main\webapp\WEB-INF\views\welcome.jsp 170 | ``` 171 | 172 | 173 | Yahoo!! 174 | 175 | 176 |

Welcome ${name}

177 |
178 | Your Todos are 179 | ${todos} 180 |
181 | 182 | 183 | ``` 184 | src\main\webapp\WEB-INF\web.xml 185 | ``` 186 | 187 | 190 | 191 | To do List 192 | 193 | 194 | login.do 195 | 196 | 197 | ``` 198 | -------------------------------------------------------------------------------- /Step10.md: -------------------------------------------------------------------------------- 1 | pom.xml 2 | ``` 3 | 5 | 4.0.0 6 | com.in28minutes 7 | in28Minutes-first-webapp 8 | 0.0.1-SNAPSHOT 9 | war 10 | 11 | 12 | 13 | javax 14 | javaee-web-api 15 | 6.0 16 | provided 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | org.apache.maven.plugins 25 | maven-compiler-plugin 26 | 3.2 27 | 28 | true 29 | 1.7 30 | 1.7 31 | true 32 | 33 | 34 | 35 | org.apache.tomcat.maven 36 | tomcat7-maven-plugin 37 | 2.2 38 | 39 | / 40 | true 41 | 42 | 43 | 44 | 45 | 46 | 47 | ``` 48 | src\main\java\in28minutes\login\LoginService.java 49 | ``` 50 | package in28minutes.login; 51 | 52 | public class LoginService { 53 | public boolean validateUser(String user, String password) { 54 | return user.equalsIgnoreCase("in28Minutes") && password.equals("dummy"); 55 | } 56 | 57 | } 58 | ``` 59 | src\main\java\in28minutes\login\LoginServlet.java 60 | ``` 61 | package in28minutes.login; 62 | 63 | import java.io.IOException; 64 | 65 | import javax.servlet.ServletException; 66 | import javax.servlet.annotation.WebServlet; 67 | import javax.servlet.http.HttpServlet; 68 | import javax.servlet.http.HttpServletRequest; 69 | import javax.servlet.http.HttpServletResponse; 70 | 71 | 72 | @WebServlet(urlPatterns = "/login.do") 73 | public class LoginServlet extends HttpServlet { 74 | 75 | private static final long serialVersionUID = 1L; 76 | private LoginService service = new LoginService(); 77 | 78 | @Override 79 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 80 | throws IOException, ServletException { 81 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 82 | } 83 | 84 | @Override 85 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 86 | throws IOException, ServletException { 87 | String name = request.getParameter("name"); 88 | String password = request.getParameter("password"); 89 | 90 | boolean isValidUser = service.validateUser(name, password); 91 | 92 | if (isValidUser) { 93 | response.sendRedirect("/todo.do"); 94 | } else { 95 | request.setAttribute("errorMessage", "Invalid Credentials!!"); 96 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 97 | } 98 | } 99 | 100 | } 101 | ``` 102 | src\main\java\in28minutes\todo\Todo.java 103 | ``` 104 | package in28minutes.todo; 105 | 106 | public class Todo { 107 | 108 | public Todo(String name) { 109 | super(); 110 | this.name = name; 111 | } 112 | 113 | private String name; 114 | 115 | public String getName() { 116 | return name; 117 | } 118 | 119 | public void setName(String name) { 120 | this.name = name; 121 | } 122 | 123 | @Override 124 | public String toString() { 125 | return "Todo [name=" + name + "]"; 126 | } 127 | } 128 | ``` 129 | src\main\java\in28minutes\todo\TodoService.java 130 | ``` 131 | package in28minutes.todo; 132 | 133 | import java.util.ArrayList; 134 | import java.util.List; 135 | 136 | public class TodoService { 137 | private static List todos = new ArrayList(); 138 | 139 | static { 140 | todos.add(new Todo("Learn Web Application")); 141 | todos.add(new Todo("Learn Spring")); 142 | todos.add(new Todo("Learn Spring MVC")); 143 | } 144 | 145 | public List retrieveTodos() { 146 | return todos; 147 | } 148 | } 149 | ``` 150 | src\main\java\in28minutes\todo\TodoServlet.java 151 | ``` 152 | package in28minutes.todo; 153 | 154 | import java.io.IOException; 155 | 156 | import javax.servlet.ServletException; 157 | import javax.servlet.annotation.WebServlet; 158 | import javax.servlet.http.HttpServlet; 159 | import javax.servlet.http.HttpServletRequest; 160 | import javax.servlet.http.HttpServletResponse; 161 | 162 | @WebServlet(urlPatterns = "/todo.do") 163 | public class TodoServlet extends HttpServlet { 164 | 165 | private static final long serialVersionUID = 1L; 166 | private TodoService todoService = new TodoService(); 167 | 168 | @Override 169 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 170 | throws IOException, ServletException { 171 | request.setAttribute("todos", todoService.retrieveTodos()); 172 | request.getRequestDispatcher("/WEB-INF/views/todo.jsp").forward(request, response); 173 | } 174 | } 175 | ``` 176 | src\main\webapp\WEB-INF\views\login.jsp 177 | ``` 178 | 179 | 180 | Yahoo!! 181 | 182 | 183 |

${errorMessage}

184 |
185 | Name : Password : 186 |
187 | 188 | 189 | ``` 190 | src\main\webapp\WEB-INF\views\todo.jsp 191 | ``` 192 | 193 | 194 | Yahoo!! 195 | 196 | 197 |

Welcome ${name}

198 |
199 | Your Todos are 200 | ${todos} 201 |
202 | 203 | 204 | ``` 205 | src\main\webapp\WEB-INF\web.xml 206 | ``` 207 | 208 | 211 | 212 | To do List 213 | 214 | 215 | login.do 216 | 217 | 218 | ``` 219 | -------------------------------------------------------------------------------- /Step11.md: -------------------------------------------------------------------------------- 1 | pom.xml 2 | ``` 3 | 5 | 4.0.0 6 | com.in28minutes 7 | in28Minutes-first-webapp 8 | 0.0.1-SNAPSHOT 9 | war 10 | 11 | 12 | 13 | javax 14 | javaee-web-api 15 | 6.0 16 | provided 17 | 18 | 19 | javax.servlet 20 | jstl 21 | 1.2 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | org.apache.maven.plugins 31 | maven-compiler-plugin 32 | 3.2 33 | 34 | true 35 | 1.7 36 | 1.7 37 | true 38 | 39 | 40 | 41 | org.apache.tomcat.maven 42 | tomcat7-maven-plugin 43 | 2.2 44 | 45 | / 46 | true 47 | 48 | 49 | 50 | 51 | 52 | 53 | ``` 54 | src\main\java\in28minutes\login\LoginService.java 55 | ``` 56 | package in28minutes.login; 57 | 58 | public class LoginService { 59 | public boolean validateUser(String user, String password) { 60 | return user.equalsIgnoreCase("in28Minutes") && password.equals("dummy"); 61 | } 62 | 63 | } 64 | ``` 65 | src\main\java\in28minutes\login\LoginServlet.java 66 | ``` 67 | package in28minutes.login; 68 | 69 | import java.io.IOException; 70 | 71 | import javax.servlet.ServletException; 72 | import javax.servlet.annotation.WebServlet; 73 | import javax.servlet.http.HttpServlet; 74 | import javax.servlet.http.HttpServletRequest; 75 | import javax.servlet.http.HttpServletResponse; 76 | 77 | 78 | @WebServlet(urlPatterns = "/login.do") 79 | public class LoginServlet extends HttpServlet { 80 | 81 | private static final long serialVersionUID = 1L; 82 | private LoginService service = new LoginService(); 83 | 84 | @Override 85 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 86 | throws IOException, ServletException { 87 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 88 | } 89 | 90 | @Override 91 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 92 | throws IOException, ServletException { 93 | String name = request.getParameter("name"); 94 | String password = request.getParameter("password"); 95 | 96 | boolean isValidUser = service.validateUser(name, password); 97 | 98 | if (isValidUser) { 99 | response.sendRedirect("/todo.do"); 100 | } else { 101 | request.setAttribute("errorMessage", "Invalid Credentials!!"); 102 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 103 | } 104 | } 105 | 106 | } 107 | ``` 108 | src\main\java\in28minutes\todo\Todo.java 109 | ``` 110 | package in28minutes.todo; 111 | 112 | public class Todo { 113 | 114 | public Todo(String name) { 115 | super(); 116 | this.name = name; 117 | } 118 | 119 | private String name; 120 | 121 | public String getName() { 122 | return name; 123 | } 124 | 125 | public void setName(String name) { 126 | this.name = name; 127 | } 128 | 129 | @Override 130 | public String toString() { 131 | return "Todo [name=" + name + "]"; 132 | } 133 | } 134 | ``` 135 | src\main\java\in28minutes\todo\TodoService.java 136 | ``` 137 | package in28minutes.todo; 138 | 139 | import java.util.ArrayList; 140 | import java.util.List; 141 | 142 | public class TodoService { 143 | private static List todos = new ArrayList(); 144 | 145 | static { 146 | todos.add(new Todo("Learn Web Application")); 147 | todos.add(new Todo("Learn Spring")); 148 | todos.add(new Todo("Learn Spring MVC")); 149 | } 150 | 151 | public List retrieveTodos() { 152 | return todos; 153 | } 154 | } 155 | ``` 156 | src\main\java\in28minutes\todo\TodoServlet.java 157 | ``` 158 | package in28minutes.todo; 159 | 160 | import java.io.IOException; 161 | 162 | import javax.servlet.ServletException; 163 | import javax.servlet.annotation.WebServlet; 164 | import javax.servlet.http.HttpServlet; 165 | import javax.servlet.http.HttpServletRequest; 166 | import javax.servlet.http.HttpServletResponse; 167 | 168 | @WebServlet(urlPatterns = "/todo.do") 169 | public class TodoServlet extends HttpServlet { 170 | 171 | private static final long serialVersionUID = 1L; 172 | private TodoService todoService = new TodoService(); 173 | 174 | @Override 175 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 176 | throws IOException, ServletException { 177 | request.setAttribute("todos", todoService.retrieveTodos()); 178 | request.getRequestDispatcher("/WEB-INF/views/todo.jsp").forward(request, response); 179 | } 180 | } 181 | ``` 182 | src\main\webapp\WEB-INF\views\login.jsp 183 | ``` 184 | 185 | 186 | Yahoo!! 187 | 188 | 189 |

${errorMessage}

190 |
191 | Name : Password : 192 |
193 | 194 | 195 | ``` 196 | src\main\webapp\WEB-INF\views\todo.jsp 197 | ``` 198 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 199 | 200 | 201 | Yahoo!! 202 | 203 | 204 |

Welcome ${name}

205 |
206 | Your Todos are 207 |
    208 | 209 |
  1. ${todo.name}
  2. 210 |
    211 |
212 |
213 | 214 | 215 | ``` 216 | src\main\webapp\WEB-INF\web.xml 217 | ``` 218 | 219 | 222 | 223 | To do List 224 | 225 | 226 | login.do 227 | 228 | 229 | ``` 230 | -------------------------------------------------------------------------------- /Step13.md: -------------------------------------------------------------------------------- 1 | pom.xml 2 | ``` 3 | 5 | 4.0.0 6 | com.in28minutes 7 | in28Minutes-first-webapp 8 | 0.0.1-SNAPSHOT 9 | war 10 | 11 | 12 | 13 | javax 14 | javaee-web-api 15 | 6.0 16 | provided 17 | 18 | 19 | javax.servlet 20 | jstl 21 | 1.2 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | org.apache.maven.plugins 31 | maven-compiler-plugin 32 | 3.2 33 | 34 | true 35 | 1.7 36 | 1.7 37 | true 38 | 39 | 40 | 41 | org.apache.tomcat.maven 42 | tomcat7-maven-plugin 43 | 2.2 44 | 45 | / 46 | true 47 | 48 | 49 | 50 | 51 | 52 | 53 | ``` 54 | src\main\java\in28minutes\login\LoginService.java 55 | ``` 56 | package in28minutes.login; 57 | 58 | public class LoginService { 59 | public boolean validateUser(String user, String password) { 60 | return user.equalsIgnoreCase("in28Minutes") && password.equals("dummy"); 61 | } 62 | 63 | } 64 | ``` 65 | src\main\java\in28minutes\login\LoginServlet.java 66 | ``` 67 | package in28minutes.login; 68 | 69 | import java.io.IOException; 70 | 71 | import javax.servlet.ServletException; 72 | import javax.servlet.annotation.WebServlet; 73 | import javax.servlet.http.HttpServlet; 74 | import javax.servlet.http.HttpServletRequest; 75 | import javax.servlet.http.HttpServletResponse; 76 | 77 | 78 | @WebServlet(urlPatterns = "/login.do") 79 | public class LoginServlet extends HttpServlet { 80 | 81 | private static final long serialVersionUID = 1L; 82 | private LoginService service = new LoginService(); 83 | 84 | @Override 85 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 86 | throws IOException, ServletException { 87 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 88 | } 89 | 90 | @Override 91 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 92 | throws IOException, ServletException { 93 | String name = request.getParameter("name"); 94 | String password = request.getParameter("password"); 95 | 96 | boolean isValidUser = service.validateUser(name, password); 97 | 98 | if (isValidUser) { 99 | response.sendRedirect("/todo.do"); 100 | } else { 101 | request.setAttribute("errorMessage", "Invalid Credentials!!"); 102 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 103 | } 104 | } 105 | 106 | } 107 | ``` 108 | src\main\java\in28minutes\todo\Todo.java 109 | ``` 110 | package in28minutes.todo; 111 | 112 | public class Todo { 113 | 114 | public Todo(String name) { 115 | super(); 116 | this.name = name; 117 | } 118 | 119 | private String name; 120 | 121 | public String getName() { 122 | return name; 123 | } 124 | 125 | public void setName(String name) { 126 | this.name = name; 127 | } 128 | 129 | @Override 130 | public String toString() { 131 | return "Todo [name=" + name + "]"; 132 | } 133 | } 134 | ``` 135 | src\main\java\in28minutes\todo\TodoService.java 136 | ``` 137 | package in28minutes.todo; 138 | 139 | import java.util.ArrayList; 140 | import java.util.List; 141 | 142 | public class TodoService { 143 | private static List todos = new ArrayList(); 144 | 145 | static { 146 | todos.add(new Todo("Learn Web Application")); 147 | todos.add(new Todo("Learn Spring")); 148 | todos.add(new Todo("Learn Spring MVC")); 149 | } 150 | 151 | public List retrieveTodos() { 152 | return todos; 153 | } 154 | 155 | public void addTodo(String todo) { 156 | todos.add(new Todo(todo)); 157 | } 158 | } 159 | ``` 160 | src\main\java\in28minutes\todo\TodoServlet.java 161 | ``` 162 | package in28minutes.todo; 163 | 164 | import java.io.IOException; 165 | 166 | import javax.servlet.ServletException; 167 | import javax.servlet.annotation.WebServlet; 168 | import javax.servlet.http.HttpServlet; 169 | import javax.servlet.http.HttpServletRequest; 170 | import javax.servlet.http.HttpServletResponse; 171 | 172 | @WebServlet(urlPatterns = "/todo.do") 173 | public class TodoServlet extends HttpServlet { 174 | 175 | private static final long serialVersionUID = 1L; 176 | private TodoService todoService = new TodoService(); 177 | 178 | @Override 179 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 180 | throws IOException, ServletException { 181 | request.setAttribute("todos", todoService.retrieveTodos()); 182 | request.getRequestDispatcher("/WEB-INF/views/todo.jsp").forward(request, response); 183 | } 184 | 185 | @Override 186 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 187 | throws IOException, ServletException { 188 | String todo = request.getParameter("todo"); 189 | if ("".equals(todo)) { 190 | request.setAttribute("errorMessage", "Enter a valid todo"); 191 | } else { 192 | todoService.addTodo(todo); 193 | } 194 | request.setAttribute("todos", todoService.retrieveTodos()); 195 | request.getRequestDispatcher("/WEB-INF/views/todo.jsp").forward(request, response); 196 | } 197 | 198 | } 199 | ``` 200 | src\main\webapp\WEB-INF\views\login.jsp 201 | ``` 202 | 203 | 204 | Yahoo!! 205 | 206 | 207 |

${errorMessage}

208 |
209 | Name : Password : 210 |
211 | 212 | 213 | ``` 214 | src\main\webapp\WEB-INF\views\todo.jsp 215 | ``` 216 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 217 | 218 | 219 | Yahoo!! 220 | 221 | 222 |

Welcome ${name}

223 |
224 | Your Todos are 225 |
    226 | 227 |
  1. ${todo.name}
  2. 228 |
    229 |
230 | 231 |

${errorMessage}

232 |
233 | New Todo : 234 |
235 | 236 |
237 | 238 | 239 | ``` 240 | src\main\webapp\WEB-INF\web.xml 241 | ``` 242 | 243 | 246 | 247 | To do List 248 | 249 | 250 | login.do 251 | 252 | 253 | ``` 254 | -------------------------------------------------------------------------------- /Step14.md: -------------------------------------------------------------------------------- 1 | pom.xml 2 | ``` 3 | 5 | 4.0.0 6 | com.in28minutes 7 | in28Minutes-first-webapp 8 | 0.0.1-SNAPSHOT 9 | war 10 | 11 | 12 | 13 | javax 14 | javaee-web-api 15 | 6.0 16 | provided 17 | 18 | 19 | javax.servlet 20 | jstl 21 | 1.2 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | org.apache.maven.plugins 31 | maven-compiler-plugin 32 | 3.2 33 | 34 | true 35 | 1.7 36 | 1.7 37 | true 38 | 39 | 40 | 41 | org.apache.tomcat.maven 42 | tomcat7-maven-plugin 43 | 2.2 44 | 45 | / 46 | true 47 | 48 | 49 | 50 | 51 | 52 | 53 | ``` 54 | src\main\java\in28minutes\login\LoginService.java 55 | ``` 56 | package in28minutes.login; 57 | 58 | public class LoginService { 59 | public boolean validateUser(String user, String password) { 60 | return user.equalsIgnoreCase("in28Minutes") && password.equals("dummy"); 61 | } 62 | 63 | } 64 | ``` 65 | src\main\java\in28minutes\login\LoginServlet.java 66 | ``` 67 | package in28minutes.login; 68 | 69 | import java.io.IOException; 70 | 71 | import javax.servlet.ServletException; 72 | import javax.servlet.annotation.WebServlet; 73 | import javax.servlet.http.HttpServlet; 74 | import javax.servlet.http.HttpServletRequest; 75 | import javax.servlet.http.HttpServletResponse; 76 | 77 | 78 | @WebServlet(urlPatterns = "/login.do") 79 | public class LoginServlet extends HttpServlet { 80 | 81 | private static final long serialVersionUID = 1L; 82 | private LoginService service = new LoginService(); 83 | 84 | @Override 85 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 86 | throws IOException, ServletException { 87 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 88 | } 89 | 90 | @Override 91 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 92 | throws IOException, ServletException { 93 | String name = request.getParameter("name"); 94 | String password = request.getParameter("password"); 95 | 96 | boolean isValidUser = service.validateUser(name, password); 97 | 98 | if (isValidUser) { 99 | request.getSession().setAttribute("name", name); 100 | response.sendRedirect("/todo.do"); 101 | } else { 102 | request.setAttribute("errorMessage", "Invalid Credentials!!"); 103 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 104 | } 105 | } 106 | 107 | } 108 | ``` 109 | src\main\java\in28minutes\todo\DeleteTodoServlet.java 110 | ``` 111 | package in28minutes.todo; 112 | 113 | import java.io.IOException; 114 | 115 | import javax.servlet.ServletException; 116 | import javax.servlet.annotation.WebServlet; 117 | import javax.servlet.http.HttpServlet; 118 | import javax.servlet.http.HttpServletRequest; 119 | import javax.servlet.http.HttpServletResponse; 120 | 121 | @WebServlet(urlPatterns = "/delete-todo.do") 122 | public class DeleteTodoServlet extends HttpServlet { 123 | 124 | private static final long serialVersionUID = 1L; 125 | private TodoService todoService = new TodoService(); 126 | 127 | @Override 128 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 129 | throws IOException, ServletException { 130 | todoService.deleteTodo(request.getParameter("todo")); 131 | response.sendRedirect("/todo.do"); 132 | } 133 | } 134 | ``` 135 | src\main\java\in28minutes\todo\ListTodoServlet.java 136 | ``` 137 | package in28minutes.todo; 138 | 139 | import java.io.IOException; 140 | 141 | import javax.servlet.ServletException; 142 | import javax.servlet.annotation.WebServlet; 143 | import javax.servlet.http.HttpServlet; 144 | import javax.servlet.http.HttpServletRequest; 145 | import javax.servlet.http.HttpServletResponse; 146 | 147 | @WebServlet(urlPatterns = "/todo.do") 148 | public class ListTodoServlet extends HttpServlet { 149 | 150 | private static final long serialVersionUID = 1L; 151 | private TodoService todoService = new TodoService(); 152 | 153 | @Override 154 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 155 | throws IOException, ServletException { 156 | request.setAttribute("todos", todoService.retrieveTodos()); 157 | request.getRequestDispatcher("/WEB-INF/views/todo.jsp").forward(request, response); 158 | } 159 | 160 | @Override 161 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 162 | throws IOException, ServletException { 163 | String todo = request.getParameter("todo"); 164 | if ("".equals(todo)) { 165 | request.setAttribute("errorMessage", "Enter a valid Todo"); 166 | } else { 167 | todoService.addTodo(todo); 168 | } 169 | request.setAttribute("todos", todoService.retrieveTodos()); 170 | request.getRequestDispatcher("/WEB-INF/views/todo.jsp").forward(request, response); 171 | } 172 | 173 | } 174 | ``` 175 | src\main\java\in28minutes\todo\Todo.java 176 | ``` 177 | package in28minutes.todo; 178 | 179 | public class Todo { 180 | 181 | public Todo(String name) { 182 | super(); 183 | this.name = name; 184 | } 185 | 186 | private String name; 187 | 188 | public String getName() { 189 | return name; 190 | } 191 | 192 | public void setName(String name) { 193 | this.name = name; 194 | } 195 | 196 | @Override 197 | public String toString() { 198 | return "Todo [name=" + name + "]"; 199 | } 200 | 201 | @Override 202 | public int hashCode() { 203 | final int prime = 31; 204 | int result = 1; 205 | result = prime * result + ((name == null) ? 0 : name.hashCode()); 206 | return result; 207 | } 208 | 209 | @Override 210 | public boolean equals(Object obj) { 211 | if (this == obj) 212 | return true; 213 | if (obj == null) 214 | return false; 215 | if (getClass() != obj.getClass()) 216 | return false; 217 | Todo other = (Todo) obj; 218 | if (name == null) { 219 | if (other.name != null) 220 | return false; 221 | } else if (!name.equals(other.name)) 222 | return false; 223 | return true; 224 | } 225 | 226 | } 227 | ``` 228 | src\main\java\in28minutes\todo\TodoService.java 229 | ``` 230 | package in28minutes.todo; 231 | 232 | import java.util.ArrayList; 233 | import java.util.List; 234 | 235 | public class TodoService { 236 | private static List todos = new ArrayList(); 237 | 238 | static { 239 | todos.add(new Todo("Learn Web Application")); 240 | todos.add(new Todo("Learn Spring")); 241 | todos.add(new Todo("Learn Spring MVC")); 242 | } 243 | 244 | public List retrieveTodos() { 245 | return todos; 246 | } 247 | 248 | public void addTodo(String todo) { 249 | todos.add(new Todo(todo)); 250 | } 251 | 252 | public void deleteTodo(String todo) { 253 | todos.remove(new Todo(todo)); 254 | } 255 | } 256 | ``` 257 | src\main\webapp\WEB-INF\views\login.jsp 258 | ``` 259 | 260 | 261 | Yahoo!! 262 | 263 | 264 |

${errorMessage}

265 |
266 | Name : Password : 267 |
268 | 269 | 270 | ``` 271 | src\main\webapp\WEB-INF\views\todo.jsp 272 | ``` 273 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 274 | 275 | 276 | Yahoo!! 277 | 278 | 279 |

Welcome ${name}

280 |
281 | Your Todos are 282 |
    283 | 284 |
  1. ${todo.name} Delete
  2. 285 |
    286 |
287 | 288 |

${errorMessage}

289 |
290 | New Todo : 291 |
292 |
293 | 294 | 295 | ``` 296 | src\main\webapp\WEB-INF\web.xml 297 | ``` 298 | 299 | 302 | 303 | To do List 304 | 305 | 306 | login.do 307 | 308 | 309 | ``` 310 | -------------------------------------------------------------------------------- /Step15.md: -------------------------------------------------------------------------------- 1 | pom.xml 2 | ``` 3 | 5 | 4.0.0 6 | com.in28minutes 7 | in28Minutes-first-webapp 8 | 0.0.1-SNAPSHOT 9 | war 10 | 11 | 12 | 13 | javax 14 | javaee-web-api 15 | 6.0 16 | provided 17 | 18 | 19 | javax.servlet 20 | jstl 21 | 1.2 22 | 23 | 24 | 25 | 26 | org.webjars 27 | bootstrap 28 | 3.3.6 29 | 30 | 31 | org.webjars 32 | jquery 33 | 1.9.1 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.apache.maven.plugins 43 | maven-compiler-plugin 44 | 3.2 45 | 46 | true 47 | 1.7 48 | 1.7 49 | true 50 | 51 | 52 | 53 | org.apache.tomcat.maven 54 | tomcat7-maven-plugin 55 | 2.2 56 | 57 | / 58 | true 59 | 60 | 61 | 62 | 63 | 64 | 65 | ``` 66 | src\main\java\in28minutes\login\LoginService.java 67 | ``` 68 | package in28minutes.login; 69 | 70 | public class LoginService { 71 | public boolean validateUser(String user, String password) { 72 | return user.equalsIgnoreCase("in28Minutes") && password.equals("dummy"); 73 | } 74 | 75 | } 76 | ``` 77 | src\main\java\in28minutes\login\LoginServlet.java 78 | ``` 79 | package in28minutes.login; 80 | 81 | import java.io.IOException; 82 | 83 | import javax.servlet.ServletException; 84 | import javax.servlet.annotation.WebServlet; 85 | import javax.servlet.http.HttpServlet; 86 | import javax.servlet.http.HttpServletRequest; 87 | import javax.servlet.http.HttpServletResponse; 88 | 89 | 90 | @WebServlet(urlPatterns = "/login.do") 91 | public class LoginServlet extends HttpServlet { 92 | 93 | private static final long serialVersionUID = 1L; 94 | private LoginService service = new LoginService(); 95 | 96 | @Override 97 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 98 | throws IOException, ServletException { 99 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 100 | } 101 | 102 | @Override 103 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 104 | throws IOException, ServletException { 105 | String name = request.getParameter("name"); 106 | String password = request.getParameter("password"); 107 | 108 | boolean isValidUser = service.validateUser(name, password); 109 | 110 | if (isValidUser) { 111 | request.getSession().setAttribute("name", name); 112 | response.sendRedirect("/todo.do"); 113 | } else { 114 | request.setAttribute("errorMessage", "Invalid Credentials!!"); 115 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 116 | } 117 | } 118 | 119 | } 120 | ``` 121 | src\main\java\in28minutes\todo\DeleteTodoServlet.java 122 | ``` 123 | package in28minutes.todo; 124 | 125 | import java.io.IOException; 126 | 127 | import javax.servlet.ServletException; 128 | import javax.servlet.annotation.WebServlet; 129 | import javax.servlet.http.HttpServlet; 130 | import javax.servlet.http.HttpServletRequest; 131 | import javax.servlet.http.HttpServletResponse; 132 | 133 | @WebServlet(urlPatterns = "/delete-todo.do") 134 | public class DeleteTodoServlet extends HttpServlet { 135 | 136 | private static final long serialVersionUID = 1L; 137 | private TodoService todoService = new TodoService(); 138 | 139 | @Override 140 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 141 | throws IOException, ServletException { 142 | todoService.deleteTodo(request.getParameter("todo")); 143 | response.sendRedirect("/todo.do"); 144 | } 145 | } 146 | ``` 147 | src\main\java\in28minutes\todo\ListTodoServlet.java 148 | ``` 149 | package in28minutes.todo; 150 | 151 | import java.io.IOException; 152 | 153 | import javax.servlet.ServletException; 154 | import javax.servlet.annotation.WebServlet; 155 | import javax.servlet.http.HttpServlet; 156 | import javax.servlet.http.HttpServletRequest; 157 | import javax.servlet.http.HttpServletResponse; 158 | 159 | @WebServlet(urlPatterns = "/todo.do") 160 | public class ListTodoServlet extends HttpServlet { 161 | 162 | private static final long serialVersionUID = 1L; 163 | private TodoService todoService = new TodoService(); 164 | 165 | @Override 166 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 167 | throws IOException, ServletException { 168 | request.setAttribute("todos", todoService.retrieveTodos()); 169 | request.getRequestDispatcher("/WEB-INF/views/todo.jsp").forward(request, response); 170 | } 171 | 172 | @Override 173 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 174 | throws IOException, ServletException { 175 | String todo = request.getParameter("todo"); 176 | if ("".equals(todo)) { 177 | request.setAttribute("errorMessage", "Enter a valid Todo"); 178 | } else { 179 | todoService.addTodo(todo); 180 | } 181 | request.setAttribute("todos", todoService.retrieveTodos()); 182 | request.getRequestDispatcher("/WEB-INF/views/todo.jsp").forward(request, response); 183 | } 184 | 185 | } 186 | ``` 187 | src\main\java\in28minutes\todo\Todo.java 188 | ``` 189 | package in28minutes.todo; 190 | 191 | public class Todo { 192 | 193 | public Todo(String name) { 194 | super(); 195 | this.name = name; 196 | } 197 | 198 | private String name; 199 | 200 | public String getName() { 201 | return name; 202 | } 203 | 204 | public void setName(String name) { 205 | this.name = name; 206 | } 207 | 208 | @Override 209 | public String toString() { 210 | return "Todo [name=" + name + "]"; 211 | } 212 | 213 | @Override 214 | public int hashCode() { 215 | final int prime = 31; 216 | int result = 1; 217 | result = prime * result + ((name == null) ? 0 : name.hashCode()); 218 | return result; 219 | } 220 | 221 | @Override 222 | public boolean equals(Object obj) { 223 | if (this == obj) 224 | return true; 225 | if (obj == null) 226 | return false; 227 | if (getClass() != obj.getClass()) 228 | return false; 229 | Todo other = (Todo) obj; 230 | if (name == null) { 231 | if (other.name != null) 232 | return false; 233 | } else if (!name.equals(other.name)) 234 | return false; 235 | return true; 236 | } 237 | 238 | } 239 | ``` 240 | src\main\java\in28minutes\todo\TodoService.java 241 | ``` 242 | package in28minutes.todo; 243 | 244 | import java.util.ArrayList; 245 | import java.util.List; 246 | 247 | public class TodoService { 248 | private static List todos = new ArrayList(); 249 | 250 | static { 251 | todos.add(new Todo("Learn Web Application")); 252 | todos.add(new Todo("Learn Spring")); 253 | todos.add(new Todo("Learn Spring MVC")); 254 | } 255 | 256 | public List retrieveTodos() { 257 | return todos; 258 | } 259 | 260 | public void addTodo(String todo) { 261 | todos.add(new Todo(todo)); 262 | } 263 | 264 | public void deleteTodo(String todo) { 265 | todos.remove(new Todo(todo)); 266 | } 267 | } 268 | ``` 269 | src\main\webapp\WEB-INF\views\login.jsp 270 | ``` 271 | 272 | 273 | Yahoo!! 274 | 275 | 276 |

${errorMessage}

277 |
278 | Name : Password : 279 |
280 | 281 | 282 | ``` 283 | src\main\webapp\WEB-INF\views\todo.jsp 284 | ``` 285 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 286 | 287 | 288 | 289 | Yahoo!! 290 | 291 | 293 | 294 | 295 |

Welcome ${name}

296 |
297 | Your Todos are 298 |
    299 | 300 |
  1. ${todo.name} Delete
  2. 301 |
    302 |
303 | 304 |

305 | ${errorMessage} 306 |

307 |
308 | New Todo : 310 |
311 |
312 | 313 | 314 | 315 | 316 | ``` 317 | src\main\webapp\WEB-INF\web.xml 318 | ``` 319 | 320 | 323 | 324 | To do List 325 | 326 | 327 | login.do 328 | 329 | 330 | ``` 331 | -------------------------------------------------------------------------------- /Step17.md: -------------------------------------------------------------------------------- 1 | Bootstrap Sample Page 2 | ``` 3 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 | 5 | 6 | 7 | Todos 8 | 10 | 11 | 20 | 21 | 22 | 23 | 24 | 39 | 40 |
41 |

Heading

42 | Body of the Page 43 |
44 | 45 |
46 |

footer content

47 |
48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | ``` 56 | 57 | pom.xml 58 | ``` 59 | 61 | 4.0.0 62 | com.in28minutes 63 | in28Minutes-first-webapp 64 | 0.0.1-SNAPSHOT 65 | war 66 | 67 | 68 | 69 | javax 70 | javaee-web-api 71 | 6.0 72 | provided 73 | 74 | 75 | javax.servlet 76 | jstl 77 | 1.2 78 | 79 | 80 | 81 | 82 | org.webjars 83 | bootstrap 84 | 3.3.6 85 | 86 | 87 | org.webjars 88 | jquery 89 | 1.9.1 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | org.apache.maven.plugins 99 | maven-compiler-plugin 100 | 3.2 101 | 102 | true 103 | 1.7 104 | 1.7 105 | true 106 | 107 | 108 | 109 | org.apache.tomcat.maven 110 | tomcat7-maven-plugin 111 | 2.2 112 | 113 | / 114 | true 115 | 116 | 117 | 118 | 119 | 120 | 121 | ``` 122 | src\main\java\in28minutes\login\LoginService.java 123 | ``` 124 | package in28minutes.login; 125 | 126 | public class LoginService { 127 | public boolean validateUser(String user, String password) { 128 | return user.equalsIgnoreCase("in28Minutes") && password.equals("dummy"); 129 | } 130 | 131 | } 132 | ``` 133 | src\main\java\in28minutes\login\LoginServlet.java 134 | ``` 135 | package in28minutes.login; 136 | 137 | import java.io.IOException; 138 | 139 | import javax.servlet.ServletException; 140 | import javax.servlet.annotation.WebServlet; 141 | import javax.servlet.http.HttpServlet; 142 | import javax.servlet.http.HttpServletRequest; 143 | import javax.servlet.http.HttpServletResponse; 144 | 145 | 146 | @WebServlet(urlPatterns = "/login.do") 147 | public class LoginServlet extends HttpServlet { 148 | 149 | private static final long serialVersionUID = 1L; 150 | private LoginService service = new LoginService(); 151 | 152 | @Override 153 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 154 | throws IOException, ServletException { 155 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 156 | } 157 | 158 | @Override 159 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 160 | throws IOException, ServletException { 161 | String name = request.getParameter("name"); 162 | String password = request.getParameter("password"); 163 | 164 | boolean isValidUser = service.validateUser(name, password); 165 | 166 | if (isValidUser) { 167 | request.getSession().setAttribute("name", name); 168 | response.sendRedirect("/todo.do"); 169 | } else { 170 | request.setAttribute("errorMessage", "Invalid Credentials!!"); 171 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response); 172 | } 173 | } 174 | 175 | } 176 | ``` 177 | src\main\java\in28minutes\todo\DeleteTodoServlet.java 178 | ``` 179 | package in28minutes.todo; 180 | 181 | import java.io.IOException; 182 | 183 | import javax.servlet.ServletException; 184 | import javax.servlet.annotation.WebServlet; 185 | import javax.servlet.http.HttpServlet; 186 | import javax.servlet.http.HttpServletRequest; 187 | import javax.servlet.http.HttpServletResponse; 188 | 189 | @WebServlet(urlPatterns = "/delete-todo.do") 190 | public class DeleteTodoServlet extends HttpServlet { 191 | 192 | private static final long serialVersionUID = 1L; 193 | private TodoService todoService = new TodoService(); 194 | 195 | @Override 196 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 197 | throws IOException, ServletException { 198 | todoService.deleteTodo(request.getParameter("todo")); 199 | response.sendRedirect("/todo.do"); 200 | } 201 | } 202 | ``` 203 | src\main\java\in28minutes\todo\ListTodoServlet.java 204 | ``` 205 | package in28minutes.todo; 206 | 207 | import java.io.IOException; 208 | 209 | import javax.servlet.ServletException; 210 | import javax.servlet.annotation.WebServlet; 211 | import javax.servlet.http.HttpServlet; 212 | import javax.servlet.http.HttpServletRequest; 213 | import javax.servlet.http.HttpServletResponse; 214 | 215 | @WebServlet(urlPatterns = "/todo.do") 216 | public class ListTodoServlet extends HttpServlet { 217 | 218 | private static final long serialVersionUID = 1L; 219 | private TodoService todoService = new TodoService(); 220 | 221 | @Override 222 | protected void doGet(HttpServletRequest request, HttpServletResponse response) 223 | throws IOException, ServletException { 224 | request.setAttribute("todos", todoService.retrieveTodos()); 225 | request.getRequestDispatcher("/WEB-INF/views/todo.jsp").forward(request, response); 226 | } 227 | 228 | @Override 229 | protected void doPost(HttpServletRequest request, HttpServletResponse response) 230 | throws IOException, ServletException { 231 | String todo = request.getParameter("todo"); 232 | if ("".equals(todo)) { 233 | request.setAttribute("errorMessage", "Enter a valid Todo"); 234 | } else { 235 | todoService.addTodo(todo); 236 | } 237 | request.setAttribute("todos", todoService.retrieveTodos()); 238 | request.getRequestDispatcher("/WEB-INF/views/todo.jsp").forward(request, response); 239 | } 240 | 241 | } 242 | ``` 243 | src\main\java\in28minutes\todo\Todo.java 244 | ``` 245 | package in28minutes.todo; 246 | 247 | public class Todo { 248 | 249 | public Todo(String name) { 250 | super(); 251 | this.name = name; 252 | } 253 | 254 | private String name; 255 | 256 | public String getName() { 257 | return name; 258 | } 259 | 260 | public void setName(String name) { 261 | this.name = name; 262 | } 263 | 264 | @Override 265 | public String toString() { 266 | return "Todo [name=" + name + "]"; 267 | } 268 | 269 | @Override 270 | public int hashCode() { 271 | final int prime = 31; 272 | int result = 1; 273 | result = prime * result + ((name == null) ? 0 : name.hashCode()); 274 | return result; 275 | } 276 | 277 | @Override 278 | public boolean equals(Object obj) { 279 | if (this == obj) 280 | return true; 281 | if (obj == null) 282 | return false; 283 | if (getClass() != obj.getClass()) 284 | return false; 285 | Todo other = (Todo) obj; 286 | if (name == null) { 287 | if (other.name != null) 288 | return false; 289 | } else if (!name.equals(other.name)) 290 | return false; 291 | return true; 292 | } 293 | 294 | } 295 | ``` 296 | src\main\java\in28minutes\todo\TodoService.java 297 | ``` 298 | package in28minutes.todo; 299 | 300 | import java.util.ArrayList; 301 | import java.util.List; 302 | 303 | public class TodoService { 304 | private static List todos = new ArrayList(); 305 | 306 | static { 307 | todos.add(new Todo("Learn Web Application")); 308 | todos.add(new Todo("Learn Spring")); 309 | todos.add(new Todo("Learn Spring MVC")); 310 | } 311 | 312 | public List retrieveTodos() { 313 | return todos; 314 | } 315 | 316 | public void addTodo(String todo) { 317 | todos.add(new Todo(todo)); 318 | } 319 | 320 | public void deleteTodo(String todo) { 321 | todos.remove(new Todo(todo)); 322 | } 323 | } 324 | ``` 325 | src\main\webapp\WEB-INF\views\login.jsp 326 | ``` 327 | 328 | 329 | Yahoo!! 330 | 331 | 332 |

${errorMessage}

333 |
334 | Name : Password : 335 |
336 | 337 | 338 | ``` 339 | src\main\webapp\WEB-INF\views\todo.jsp 340 | ``` 341 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 342 | 343 | 344 | 345 | 346 | 347 | Yahoo!! 348 | 349 | 351 | 352 | 367 | 368 | 369 | 370 | 371 | 389 | 390 |
391 |

Welcome ${name}

392 | 393 | Your Todos are 394 |
    395 | 396 |
  1. ${todo.name} Delete
  2. 398 |
    399 |
400 | 401 |

402 | ${errorMessage} 403 |

404 |
405 | New Todo : 407 |
408 |
409 | 410 |
411 |
412 |

footer content

413 |
414 |
415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | ``` 423 | src\main\webapp\WEB-INF\web.xml 424 | ``` 425 | 426 | 429 | 430 | To do List 431 | 432 | 433 | login.do 434 | 435 | 436 | ``` 437 | -------------------------------------------------------------------------------- /Step20.md: -------------------------------------------------------------------------------- 1 | ## Files List 2 | ### pom.xml 3 | ``` 4 | 6 | 4.0.0 7 | com.in28minutes 8 | in28Minutes-first-webapp 9 | 0.0.1-SNAPSHOT 10 | war 11 | 12 | 13 | 14 | javax 15 | javaee-web-api 16 | 6.0 17 | provided 18 | 19 | 20 | javax.servlet 21 | jstl 22 | 1.2 23 | 24 | 25 | 26 | org.webjars 27 | bootstrap 28 | 3.3.6 29 | 30 | 31 | org.webjars 32 | jquery 33 | 1.9.1 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | org.apache.maven.plugins 42 | maven-compiler-plugin 43 | 3.2 44 | 45 | true 46 | 1.7 47 | 1.7 48 | true 49 | 50 | 51 | 52 | org.apache.tomcat.maven 53 | tomcat7-maven-plugin 54 | 2.2 55 | 56 | / 57 | true 58 | 59 | 60 | 61 | 62 | 63 | 64 | ``` 65 | ### src/main/java/com/in28minutes/filter/LoginRequiredFilter.java 66 | ``` 67 | package com.in28minutes.filter; 68 | 69 | import java.io.IOException; 70 | 71 | import javax.servlet.Filter; 72 | import javax.servlet.FilterChain; 73 | import javax.servlet.FilterConfig; 74 | import javax.servlet.ServletException; 75 | import javax.servlet.ServletRequest; 76 | import javax.servlet.ServletResponse; 77 | import javax.servlet.annotation.WebFilter; 78 | import javax.servlet.http.HttpServletRequest; 79 | 80 | @WebFilter(urlPatterns = "*.do") 81 | public class LoginRequiredFilter implements Filter { 82 | 83 | @Override 84 | public void destroy() { 85 | 86 | } 87 | 88 | @Override 89 | public void doFilter(ServletRequest servletRequest, 90 | ServletResponse servletResponse, FilterChain chain) 91 | throws IOException, ServletException { 92 | HttpServletRequest request = (HttpServletRequest) servletRequest; 93 | 94 | if (request.getSession().getAttribute("name") != null) { 95 | chain.doFilter(servletRequest, servletResponse); 96 | } else { 97 | request.getRequestDispatcher("/login.do").forward(servletRequest, 98 | servletResponse); 99 | } 100 | } 101 | 102 | @Override 103 | public void init(FilterConfig arg0) throws ServletException { 104 | } 105 | 106 | } 107 | ``` 108 | ### src/main/java/com/in28minutes/login/LoginService.java 109 | ``` 110 | package com.in28minutes.login; 111 | 112 | public class LoginService { 113 | 114 | public boolean isUserValid(String user, String password) { 115 | if (user.equals("in28Minutes") && password.equals("dummy")) 116 | return true; 117 | 118 | return false; 119 | } 120 | 121 | } 122 | ``` 123 | ### src/main/java/com/in28minutes/login/LoginServlet.java 124 | ``` 125 | package com.in28minutes.login; 126 | 127 | import java.io.IOException; 128 | 129 | import javax.servlet.ServletException; 130 | import javax.servlet.annotation.WebServlet; 131 | import javax.servlet.http.HttpServlet; 132 | import javax.servlet.http.HttpServletRequest; 133 | import javax.servlet.http.HttpServletResponse; 134 | 135 | import com.in28minutes.todo.TodoService; 136 | 137 | @WebServlet(urlPatterns = "/login.do") 138 | public class LoginServlet extends HttpServlet { 139 | 140 | private LoginService userValidationService = new LoginService(); 141 | private TodoService todoService = new TodoService(); 142 | 143 | protected void doGet(HttpServletRequest request, 144 | HttpServletResponse response) throws ServletException, IOException { 145 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 146 | request, response); 147 | } 148 | 149 | protected void doPost(HttpServletRequest request, 150 | HttpServletResponse response) throws ServletException, IOException { 151 | String name = request.getParameter("name"); 152 | String password = request.getParameter("password"); 153 | 154 | boolean isUserValid = userValidationService.isUserValid(name, password); 155 | 156 | if (isUserValid) { 157 | request.getSession().setAttribute("name", name); 158 | response.sendRedirect("/todo.do"); 159 | } else { 160 | request.setAttribute("errorMessage", "Invalid Credentials!"); 161 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 162 | request, response); 163 | } 164 | } 165 | 166 | } 167 | ``` 168 | ### src/main/java/com/in28minutes/logout/LogoutServlet.java 169 | ``` 170 | package com.in28minutes.logout; 171 | 172 | import java.io.IOException; 173 | 174 | import javax.servlet.ServletException; 175 | import javax.servlet.annotation.WebServlet; 176 | import javax.servlet.http.HttpServlet; 177 | import javax.servlet.http.HttpServletRequest; 178 | import javax.servlet.http.HttpServletResponse; 179 | 180 | @WebServlet(urlPatterns = "/logout.do") 181 | public class LogoutServlet extends HttpServlet { 182 | protected void doGet(HttpServletRequest request, 183 | HttpServletResponse response) throws ServletException, IOException { 184 | request.getSession().invalidate(); 185 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 186 | request, response); 187 | } 188 | } 189 | ``` 190 | ### src/main/java/com/in28minutes/todo/AddTodoServlet.java 191 | ``` 192 | package com.in28minutes.todo; 193 | 194 | import java.io.IOException; 195 | 196 | import javax.servlet.ServletException; 197 | import javax.servlet.annotation.WebServlet; 198 | import javax.servlet.http.HttpServlet; 199 | import javax.servlet.http.HttpServletRequest; 200 | import javax.servlet.http.HttpServletResponse; 201 | 202 | @WebServlet(urlPatterns = "/add-todo.do") 203 | public class AddTodoServlet extends HttpServlet { 204 | 205 | private TodoService todoService = new TodoService(); 206 | 207 | protected void doPost(HttpServletRequest request, 208 | HttpServletResponse response) throws ServletException, IOException { 209 | String newTodo = request.getParameter("todo"); 210 | todoService.addTodo(new Todo(newTodo)); 211 | response.sendRedirect("/todo.do"); 212 | } 213 | } 214 | ``` 215 | ### src/main/java/com/in28minutes/todo/DeleteTodoServlet.java 216 | ``` 217 | package com.in28minutes.todo; 218 | 219 | import java.io.IOException; 220 | 221 | import javax.servlet.ServletException; 222 | import javax.servlet.annotation.WebServlet; 223 | import javax.servlet.http.HttpServlet; 224 | import javax.servlet.http.HttpServletRequest; 225 | import javax.servlet.http.HttpServletResponse; 226 | 227 | @WebServlet(urlPatterns = "/delete-todo.do") 228 | public class DeleteTodoServlet extends HttpServlet { 229 | 230 | private TodoService todoService = new TodoService(); 231 | 232 | protected void doGet(HttpServletRequest request, 233 | HttpServletResponse response) throws ServletException, IOException { 234 | todoService.deleteTodo(new Todo(request.getParameter("todo"))); 235 | response.sendRedirect("/todo.do"); 236 | } 237 | } 238 | ``` 239 | ### src/main/java/com/in28minutes/todo/ListTodoServlet.java 240 | ``` 241 | package com.in28minutes.todo; 242 | 243 | import java.io.IOException; 244 | 245 | import javax.servlet.ServletException; 246 | import javax.servlet.annotation.WebServlet; 247 | import javax.servlet.http.HttpServlet; 248 | import javax.servlet.http.HttpServletRequest; 249 | import javax.servlet.http.HttpServletResponse; 250 | 251 | @WebServlet(urlPatterns = "/todo.do") 252 | public class ListTodoServlet extends HttpServlet { 253 | 254 | private TodoService todoService = new TodoService(); 255 | 256 | protected void doGet(HttpServletRequest request, 257 | HttpServletResponse response) throws ServletException, IOException { 258 | request.setAttribute("todos", todoService.retrieveTodos()); 259 | request.getRequestDispatcher("/WEB-INF/views/todo.jsp").forward( 260 | request, response); 261 | } 262 | } 263 | ``` 264 | ### src/main/java/com/in28minutes/todo/Todo.java 265 | ``` 266 | package com.in28minutes.todo; 267 | 268 | public class Todo { 269 | private String name; 270 | 271 | public Todo(String name) { 272 | super(); 273 | this.name = name; 274 | } 275 | 276 | public String getName() { 277 | return name; 278 | } 279 | 280 | public void setName(String name) { 281 | this.name = name; 282 | } 283 | 284 | @Override 285 | public String toString() { 286 | return String.format("Todo [name=%s]", name); 287 | } 288 | 289 | @Override 290 | public int hashCode() { 291 | final int prime = 31; 292 | int result = 1; 293 | result = prime * result + ((name == null) ? 0 : name.hashCode()); 294 | return result; 295 | } 296 | 297 | @Override 298 | public boolean equals(Object obj) { 299 | if (this == obj) 300 | return true; 301 | if (obj == null) 302 | return false; 303 | if (getClass() != obj.getClass()) 304 | return false; 305 | Todo other = (Todo) obj; 306 | if (name == null) { 307 | if (other.name != null) 308 | return false; 309 | } else if (!name.equals(other.name)) 310 | return false; 311 | return true; 312 | } 313 | 314 | } 315 | ``` 316 | ### src/main/java/com/in28minutes/todo/TodoService.java 317 | ``` 318 | package com.in28minutes.todo; 319 | 320 | import java.util.ArrayList; 321 | import java.util.List; 322 | 323 | public class TodoService { 324 | private static List todos = new ArrayList(); 325 | static { 326 | todos.add(new Todo("Learn Web Application Development")); 327 | todos.add(new Todo("Learn Spring MVC")); 328 | todos.add(new Todo("Learn Spring Rest Services")); 329 | } 330 | 331 | public List retrieveTodos() { 332 | return todos; 333 | } 334 | 335 | public void addTodo(Todo todo) { 336 | todos.add(todo); 337 | } 338 | 339 | public void deleteTodo(Todo todo) { 340 | todos.remove(todo); 341 | } 342 | 343 | } 344 | ``` 345 | ### src/main/webapp/WEB-INF/views/login.jsp 346 | ``` 347 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 348 | 349 | 350 | 351 | Todos 352 | 354 | 355 | 364 | 365 | 366 | 367 | 368 | 383 | 384 |
385 |
386 |

387 | ${errorMessage} 388 |

389 | Name: Password: 392 |
393 | 394 |
395 | 396 |
397 |
footer content
398 |
399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | ``` 407 | ### src/main/webapp/WEB-INF/views/todo.jsp 408 | ``` 409 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 410 | 411 | 412 | 413 | Todos 414 | 416 | 417 | 426 | 427 | 428 | 429 | 430 | 445 | 446 |
447 |

Welcome ${name}

448 | 449 | Your Todos are 450 |
    451 | 452 |
  1. ${todo.name} Delete
  2. 454 |
    455 |
456 | 457 |

458 | ${errorMessage} 459 |

460 |
461 | New Todo : 463 |
464 |
465 | 466 |
467 |
footer content
468 |
469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | ``` 477 | ### src/main/webapp/WEB-INF/web.xml 478 | ``` 479 | 482 | 483 | To do List 484 | 485 | 486 | login.do 487 | 488 | 489 | 490 | ``` 491 | -------------------------------------------------------------------------------- /Step24.md: -------------------------------------------------------------------------------- 1 | ## Files List 2 | ### pom.xml 3 | ``` 4 | 6 | 4.0.0 7 | com.in28minutes 8 | in28Minutes-first-webapp 9 | 0.0.1-SNAPSHOT 10 | war 11 | 12 | 13 | 14 | javax 15 | javaee-web-api 16 | 6.0 17 | provided 18 | 19 | 20 | javax.servlet 21 | jstl 22 | 1.2 23 | 24 | 25 | 26 | org.webjars 27 | bootstrap 28 | 3.3.6 29 | 30 | 31 | org.webjars 32 | jquery 33 | 1.9.1 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | org.apache.maven.plugins 42 | maven-compiler-plugin 43 | 3.2 44 | 45 | true 46 | 1.7 47 | 1.7 48 | true 49 | 50 | 51 | 52 | org.apache.tomcat.maven 53 | tomcat7-maven-plugin 54 | 2.2 55 | 56 | / 57 | true 58 | 59 | 60 | 61 | 62 | 63 | 64 | ``` 65 | ### src/main/java/com/in28minutes/filter/LoginRequiredFilter.java 66 | ``` 67 | package com.in28minutes.filter; 68 | 69 | import java.io.IOException; 70 | 71 | import javax.servlet.Filter; 72 | import javax.servlet.FilterChain; 73 | import javax.servlet.FilterConfig; 74 | import javax.servlet.ServletException; 75 | import javax.servlet.ServletRequest; 76 | import javax.servlet.ServletResponse; 77 | import javax.servlet.annotation.WebFilter; 78 | import javax.servlet.http.HttpServletRequest; 79 | 80 | @WebFilter(urlPatterns = "*.do") 81 | public class LoginRequiredFilter implements Filter { 82 | 83 | @Override 84 | public void destroy() { 85 | 86 | } 87 | 88 | @Override 89 | public void doFilter(ServletRequest servletRequest, 90 | ServletResponse servletResponse, FilterChain chain) 91 | throws IOException, ServletException { 92 | HttpServletRequest request = (HttpServletRequest) servletRequest; 93 | 94 | if (request.getSession().getAttribute("name") != null) { 95 | chain.doFilter(servletRequest, servletResponse); 96 | } else { 97 | request.getRequestDispatcher("/login.do").forward(servletRequest, 98 | servletResponse); 99 | } 100 | } 101 | 102 | @Override 103 | public void init(FilterConfig arg0) throws ServletException { 104 | } 105 | 106 | } 107 | ``` 108 | ### src/main/java/com/in28minutes/login/LoginService.java 109 | ``` 110 | package com.in28minutes.login; 111 | 112 | public class LoginService { 113 | 114 | public boolean isUserValid(String user, String password) { 115 | if (user.equals("in28Minutes") && password.equals("dummy")) 116 | return true; 117 | 118 | return false; 119 | } 120 | 121 | } 122 | ``` 123 | ### src/main/java/com/in28minutes/login/LoginServlet.java 124 | ``` 125 | package com.in28minutes.login; 126 | 127 | import java.io.IOException; 128 | 129 | import javax.servlet.ServletException; 130 | import javax.servlet.annotation.WebServlet; 131 | import javax.servlet.http.HttpServlet; 132 | import javax.servlet.http.HttpServletRequest; 133 | import javax.servlet.http.HttpServletResponse; 134 | 135 | import com.in28minutes.todo.TodoService; 136 | 137 | @WebServlet(urlPatterns = "/login.do") 138 | public class LoginServlet extends HttpServlet { 139 | 140 | private LoginService userValidationService = new LoginService(); 141 | private TodoService todoService = new TodoService(); 142 | 143 | protected void doGet(HttpServletRequest request, 144 | HttpServletResponse response) throws ServletException, IOException { 145 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 146 | request, response); 147 | } 148 | 149 | protected void doPost(HttpServletRequest request, 150 | HttpServletResponse response) throws ServletException, IOException { 151 | String name = request.getParameter("name"); 152 | String password = request.getParameter("password"); 153 | 154 | boolean isUserValid = userValidationService.isUserValid(name, password); 155 | 156 | if (isUserValid) { 157 | request.getSession().setAttribute("name", name); 158 | response.sendRedirect("/list-todos.do"); 159 | } else { 160 | request.setAttribute("errorMessage", "Invalid Credentials!"); 161 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 162 | request, response); 163 | } 164 | } 165 | 166 | } 167 | ``` 168 | ### src/main/java/com/in28minutes/logout/LogoutServlet.java 169 | ``` 170 | package com.in28minutes.logout; 171 | 172 | import java.io.IOException; 173 | 174 | import javax.servlet.ServletException; 175 | import javax.servlet.annotation.WebServlet; 176 | import javax.servlet.http.HttpServlet; 177 | import javax.servlet.http.HttpServletRequest; 178 | import javax.servlet.http.HttpServletResponse; 179 | 180 | @WebServlet(urlPatterns = "/logout.do") 181 | public class LogoutServlet extends HttpServlet { 182 | protected void doGet(HttpServletRequest request, 183 | HttpServletResponse response) throws ServletException, IOException { 184 | request.getSession().invalidate(); 185 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 186 | request, response); 187 | } 188 | } 189 | ``` 190 | ### src/main/java/com/in28minutes/todo/AddTodoServlet.java 191 | ``` 192 | package com.in28minutes.todo; 193 | 194 | import java.io.IOException; 195 | 196 | import javax.servlet.ServletException; 197 | import javax.servlet.annotation.WebServlet; 198 | import javax.servlet.http.HttpServlet; 199 | import javax.servlet.http.HttpServletRequest; 200 | import javax.servlet.http.HttpServletResponse; 201 | 202 | @WebServlet(urlPatterns = "/add-todo.do") 203 | public class AddTodoServlet extends HttpServlet { 204 | 205 | private TodoService todoService = new TodoService(); 206 | 207 | protected void doGet(HttpServletRequest request, 208 | HttpServletResponse response) throws ServletException, IOException { 209 | request.getRequestDispatcher("/WEB-INF/views/add-todo.jsp").forward( 210 | request, response); 211 | } 212 | 213 | protected void doPost(HttpServletRequest request, 214 | HttpServletResponse response) throws ServletException, IOException { 215 | String newTodo = request.getParameter("todo"); 216 | todoService.addTodo(new Todo(newTodo)); 217 | response.sendRedirect("/list-todos.do"); 218 | } 219 | } 220 | ``` 221 | ### src/main/java/com/in28minutes/todo/DeleteTodoServlet.java 222 | ``` 223 | package com.in28minutes.todo; 224 | 225 | import java.io.IOException; 226 | 227 | import javax.servlet.ServletException; 228 | import javax.servlet.annotation.WebServlet; 229 | import javax.servlet.http.HttpServlet; 230 | import javax.servlet.http.HttpServletRequest; 231 | import javax.servlet.http.HttpServletResponse; 232 | 233 | @WebServlet(urlPatterns = "/delete-todo.do") 234 | public class DeleteTodoServlet extends HttpServlet { 235 | 236 | private TodoService todoService = new TodoService(); 237 | 238 | protected void doGet(HttpServletRequest request, 239 | HttpServletResponse response) throws ServletException, IOException { 240 | todoService.deleteTodo(new Todo(request.getParameter("todo"))); 241 | response.sendRedirect("/list-todos.do"); 242 | } 243 | } 244 | ``` 245 | ### src/main/java/com/in28minutes/todo/ListTodoServlet.java 246 | ``` 247 | package com.in28minutes.todo; 248 | 249 | import java.io.IOException; 250 | 251 | import javax.servlet.ServletException; 252 | import javax.servlet.annotation.WebServlet; 253 | import javax.servlet.http.HttpServlet; 254 | import javax.servlet.http.HttpServletRequest; 255 | import javax.servlet.http.HttpServletResponse; 256 | 257 | @WebServlet(urlPatterns = "/list-todos.do") 258 | public class ListTodoServlet extends HttpServlet { 259 | 260 | private TodoService todoService = new TodoService(); 261 | 262 | protected void doGet(HttpServletRequest request, 263 | HttpServletResponse response) throws ServletException, IOException { 264 | request.setAttribute("todos", todoService.retrieveTodos()); 265 | request.getRequestDispatcher("/WEB-INF/views/list-todos.jsp").forward( 266 | request, response); 267 | } 268 | } 269 | ``` 270 | ### src/main/java/com/in28minutes/todo/Todo.java 271 | ``` 272 | package com.in28minutes.todo; 273 | 274 | public class Todo { 275 | private String name; 276 | 277 | public Todo(String name) { 278 | super(); 279 | this.name = name; 280 | } 281 | 282 | public String getName() { 283 | return name; 284 | } 285 | 286 | public void setName(String name) { 287 | this.name = name; 288 | } 289 | 290 | @Override 291 | public String toString() { 292 | return String.format("Todo [name=%s]", name); 293 | } 294 | 295 | @Override 296 | public int hashCode() { 297 | final int prime = 31; 298 | int result = 1; 299 | result = prime * result + ((name == null) ? 0 : name.hashCode()); 300 | return result; 301 | } 302 | 303 | @Override 304 | public boolean equals(Object obj) { 305 | if (this == obj) 306 | return true; 307 | if (obj == null) 308 | return false; 309 | if (getClass() != obj.getClass()) 310 | return false; 311 | Todo other = (Todo) obj; 312 | if (name == null) { 313 | if (other.name != null) 314 | return false; 315 | } else if (!name.equals(other.name)) 316 | return false; 317 | return true; 318 | } 319 | 320 | } 321 | ``` 322 | ### src/main/java/com/in28minutes/todo/TodoService.java 323 | ``` 324 | package com.in28minutes.todo; 325 | 326 | import java.util.ArrayList; 327 | import java.util.List; 328 | 329 | public class TodoService { 330 | private static List todos = new ArrayList(); 331 | static { 332 | todos.add(new Todo("Learn Web Application Development")); 333 | todos.add(new Todo("Learn Spring MVC")); 334 | todos.add(new Todo("Learn Spring Rest Services")); 335 | } 336 | 337 | public List retrieveTodos() { 338 | return todos; 339 | } 340 | 341 | public void addTodo(Todo todo) { 342 | todos.add(todo); 343 | } 344 | 345 | public void deleteTodo(Todo todo) { 346 | todos.remove(todo); 347 | } 348 | 349 | } 350 | ``` 351 | ### src/main/webapp/WEB-INF/views/add-todo.jsp 352 | ``` 353 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 354 | 355 | 356 | 357 | Todos 358 | 360 | 361 | 370 | 371 | 372 | 373 | 374 | 389 | 390 |
391 | Your New Action Item: 392 |
393 | New Todo : 395 |
396 |
397 | 398 |
399 |
footer content
400 |
401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | ``` 409 | ### src/main/webapp/WEB-INF/views/list-todos.jsp 410 | ``` 411 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 412 | 413 | 414 | 415 | Todos 416 | 418 | 419 | 428 | 429 | 430 | 431 | 432 | 447 | 448 |
449 |

Welcome ${name}

450 | 451 | Your Todos are 452 |
    453 | 454 |
  1. ${todo.name} Delete
  2. 456 |
    457 |
458 | 459 |

460 | ${errorMessage} 461 |

462 | Add New Todo 464 |
465 | 466 |
467 |
footer content
468 |
469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | ``` 477 | ### src/main/webapp/WEB-INF/views/login.jsp 478 | ``` 479 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 480 | 481 | 482 | 483 | Todos 484 | 486 | 487 | 496 | 497 | 498 | 499 | 500 | 515 | 516 |
517 |
518 |

519 | ${errorMessage} 520 |

521 | Name: Password: 524 |
525 | 526 |
527 | 528 |
529 |
footer content
530 |
531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | ``` 539 | ### src/main/webapp/WEB-INF/web.xml 540 | ``` 541 | 544 | 545 | To do List 546 | 547 | 548 | login.do 549 | 550 | 551 | 552 | ``` 553 | -------------------------------------------------------------------------------- /Step25.md: -------------------------------------------------------------------------------- 1 | ## Files List 2 | ### pom.xml 3 | ``` 4 | 6 | 4.0.0 7 | com.in28minutes 8 | in28Minutes-first-webapp 9 | 0.0.1-SNAPSHOT 10 | war 11 | 12 | 13 | 14 | javax 15 | javaee-web-api 16 | 6.0 17 | provided 18 | 19 | 20 | javax.servlet 21 | jstl 22 | 1.2 23 | 24 | 25 | 26 | org.webjars 27 | bootstrap 28 | 3.3.6 29 | 30 | 31 | org.webjars 32 | jquery 33 | 1.9.1 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | org.apache.maven.plugins 42 | maven-compiler-plugin 43 | 3.2 44 | 45 | true 46 | 1.7 47 | 1.7 48 | true 49 | 50 | 51 | 52 | org.apache.tomcat.maven 53 | tomcat7-maven-plugin 54 | 2.2 55 | 56 | / 57 | true 58 | 59 | 60 | 61 | 62 | 63 | 64 | ``` 65 | ### src/main/java/com/in28minutes/filter/LoginRequiredFilter.java 66 | ``` 67 | package com.in28minutes.filter; 68 | 69 | import java.io.IOException; 70 | 71 | import javax.servlet.Filter; 72 | import javax.servlet.FilterChain; 73 | import javax.servlet.FilterConfig; 74 | import javax.servlet.ServletException; 75 | import javax.servlet.ServletRequest; 76 | import javax.servlet.ServletResponse; 77 | import javax.servlet.annotation.WebFilter; 78 | import javax.servlet.http.HttpServletRequest; 79 | 80 | @WebFilter(urlPatterns = "*.do") 81 | public class LoginRequiredFilter implements Filter { 82 | 83 | @Override 84 | public void destroy() { 85 | 86 | } 87 | 88 | @Override 89 | public void doFilter(ServletRequest servletRequest, 90 | ServletResponse servletResponse, FilterChain chain) 91 | throws IOException, ServletException { 92 | HttpServletRequest request = (HttpServletRequest) servletRequest; 93 | 94 | if (request.getSession().getAttribute("name") != null) { 95 | chain.doFilter(servletRequest, servletResponse); 96 | } else { 97 | request.getRequestDispatcher("/login.do").forward(servletRequest, 98 | servletResponse); 99 | } 100 | } 101 | 102 | @Override 103 | public void init(FilterConfig arg0) throws ServletException { 104 | } 105 | 106 | } 107 | ``` 108 | ### src/main/java/com/in28minutes/login/LoginService.java 109 | ``` 110 | package com.in28minutes.login; 111 | 112 | public class LoginService { 113 | 114 | public boolean isUserValid(String user, String password) { 115 | if (user.equals("in28Minutes") && password.equals("dummy")) 116 | return true; 117 | 118 | return false; 119 | } 120 | 121 | } 122 | ``` 123 | ### src/main/java/com/in28minutes/login/LoginServlet.java 124 | ``` 125 | package com.in28minutes.login; 126 | 127 | import java.io.IOException; 128 | 129 | import javax.servlet.ServletException; 130 | import javax.servlet.annotation.WebServlet; 131 | import javax.servlet.http.HttpServlet; 132 | import javax.servlet.http.HttpServletRequest; 133 | import javax.servlet.http.HttpServletResponse; 134 | 135 | import com.in28minutes.todo.TodoService; 136 | 137 | @WebServlet(urlPatterns = "/login.do") 138 | public class LoginServlet extends HttpServlet { 139 | 140 | private LoginService userValidationService = new LoginService(); 141 | private TodoService todoService = new TodoService(); 142 | 143 | protected void doGet(HttpServletRequest request, 144 | HttpServletResponse response) throws ServletException, IOException { 145 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 146 | request, response); 147 | } 148 | 149 | protected void doPost(HttpServletRequest request, 150 | HttpServletResponse response) throws ServletException, IOException { 151 | String name = request.getParameter("name"); 152 | String password = request.getParameter("password"); 153 | 154 | boolean isUserValid = userValidationService.isUserValid(name, password); 155 | 156 | if (isUserValid) { 157 | request.getSession().setAttribute("name", name); 158 | response.sendRedirect("/list-todos.do"); 159 | } else { 160 | request.setAttribute("errorMessage", "Invalid Credentials!"); 161 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 162 | request, response); 163 | } 164 | } 165 | 166 | } 167 | ``` 168 | ### src/main/java/com/in28minutes/logout/LogoutServlet.java 169 | ``` 170 | package com.in28minutes.logout; 171 | 172 | import java.io.IOException; 173 | 174 | import javax.servlet.ServletException; 175 | import javax.servlet.annotation.WebServlet; 176 | import javax.servlet.http.HttpServlet; 177 | import javax.servlet.http.HttpServletRequest; 178 | import javax.servlet.http.HttpServletResponse; 179 | 180 | @WebServlet(urlPatterns = "/logout.do") 181 | public class LogoutServlet extends HttpServlet { 182 | protected void doGet(HttpServletRequest request, 183 | HttpServletResponse response) throws ServletException, IOException { 184 | request.getSession().invalidate(); 185 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 186 | request, response); 187 | } 188 | } 189 | ``` 190 | ### src/main/java/com/in28minutes/todo/AddTodoServlet.java 191 | ``` 192 | package com.in28minutes.todo; 193 | 194 | import java.io.IOException; 195 | 196 | import javax.servlet.ServletException; 197 | import javax.servlet.annotation.WebServlet; 198 | import javax.servlet.http.HttpServlet; 199 | import javax.servlet.http.HttpServletRequest; 200 | import javax.servlet.http.HttpServletResponse; 201 | 202 | @WebServlet(urlPatterns = "/add-todo.do") 203 | public class AddTodoServlet extends HttpServlet { 204 | 205 | private TodoService todoService = new TodoService(); 206 | 207 | protected void doGet(HttpServletRequest request, 208 | HttpServletResponse response) throws ServletException, IOException { 209 | request.getRequestDispatcher("/WEB-INF/views/add-todo.jsp").forward( 210 | request, response); 211 | } 212 | 213 | protected void doPost(HttpServletRequest request, 214 | HttpServletResponse response) throws ServletException, IOException { 215 | String newTodo = request.getParameter("todo"); 216 | String category = request.getParameter("category"); 217 | todoService.addTodo(new Todo(newTodo, category)); 218 | response.sendRedirect("/list-todos.do"); 219 | } 220 | } 221 | ``` 222 | ### src/main/java/com/in28minutes/todo/DeleteTodoServlet.java 223 | ``` 224 | package com.in28minutes.todo; 225 | 226 | import java.io.IOException; 227 | 228 | import javax.servlet.ServletException; 229 | import javax.servlet.annotation.WebServlet; 230 | import javax.servlet.http.HttpServlet; 231 | import javax.servlet.http.HttpServletRequest; 232 | import javax.servlet.http.HttpServletResponse; 233 | 234 | @WebServlet(urlPatterns = "/delete-todo.do") 235 | public class DeleteTodoServlet extends HttpServlet { 236 | 237 | private TodoService todoService = new TodoService(); 238 | 239 | protected void doGet(HttpServletRequest request, 240 | HttpServletResponse response) throws ServletException, IOException { 241 | todoService.deleteTodo(new Todo(request.getParameter("todo"), request 242 | .getParameter("category"))); 243 | response.sendRedirect("/list-todos.do"); 244 | } 245 | } 246 | ``` 247 | ### src/main/java/com/in28minutes/todo/ListTodoServlet.java 248 | ``` 249 | package com.in28minutes.todo; 250 | 251 | import java.io.IOException; 252 | 253 | import javax.servlet.ServletException; 254 | import javax.servlet.annotation.WebServlet; 255 | import javax.servlet.http.HttpServlet; 256 | import javax.servlet.http.HttpServletRequest; 257 | import javax.servlet.http.HttpServletResponse; 258 | 259 | @WebServlet(urlPatterns = "/list-todos.do") 260 | public class ListTodoServlet extends HttpServlet { 261 | 262 | private TodoService todoService = new TodoService(); 263 | 264 | protected void doGet(HttpServletRequest request, 265 | HttpServletResponse response) throws ServletException, IOException { 266 | request.setAttribute("todos", todoService.retrieveTodos()); 267 | request.getRequestDispatcher("/WEB-INF/views/list-todos.jsp").forward( 268 | request, response); 269 | } 270 | } 271 | ``` 272 | ### src/main/java/com/in28minutes/todo/Todo.java 273 | ``` 274 | package com.in28minutes.todo; 275 | 276 | public class Todo { 277 | private String name; 278 | private String category; 279 | 280 | public Todo(String name, String category) { 281 | super(); 282 | this.name = name; 283 | this.category = category; 284 | } 285 | 286 | public String getName() { 287 | return name; 288 | } 289 | 290 | public void setName(String name) { 291 | this.name = name; 292 | } 293 | 294 | public String getCategory() { 295 | return category; 296 | } 297 | 298 | public void setCategory(String category) { 299 | this.category = category; 300 | } 301 | 302 | @Override 303 | public String toString() { 304 | return String.format("Todo [name=%s, category=%s]", name, category); 305 | } 306 | 307 | @Override 308 | public int hashCode() { 309 | final int prime = 31; 310 | int result = 1; 311 | result = prime * result + ((name == null) ? 0 : name.hashCode()); 312 | return result; 313 | } 314 | 315 | @Override 316 | public boolean equals(Object obj) { 317 | if (this == obj) 318 | return true; 319 | if (obj == null) 320 | return false; 321 | if (getClass() != obj.getClass()) 322 | return false; 323 | Todo other = (Todo) obj; 324 | if (name == null) { 325 | if (other.name != null) 326 | return false; 327 | } else if (!name.equals(other.name)) 328 | return false; 329 | return true; 330 | } 331 | 332 | } 333 | ``` 334 | ### src/main/java/com/in28minutes/todo/TodoService.java 335 | ``` 336 | package com.in28minutes.todo; 337 | 338 | import java.util.ArrayList; 339 | import java.util.List; 340 | 341 | public class TodoService { 342 | private static List todos = new ArrayList(); 343 | static { 344 | todos.add(new Todo("Learn Web Application Development", "Study")); 345 | todos.add(new Todo("Learn Spring MVC", "Study")); 346 | todos.add(new Todo("Learn Spring Rest Services", "Study")); 347 | } 348 | 349 | public List retrieveTodos() { 350 | return todos; 351 | } 352 | 353 | public void addTodo(Todo todo) { 354 | todos.add(todo); 355 | } 356 | 357 | public void deleteTodo(Todo todo) { 358 | todos.remove(todo); 359 | } 360 | 361 | } 362 | ``` 363 | ### src/main/webapp/WEB-INF/views/add-todo.jsp 364 | ``` 365 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 366 | 367 | 368 | 369 | Todos 370 | 372 | 373 | 382 | 383 | 384 | 385 | 386 | 401 | 402 |
403 | Your New Action Item: 404 |
405 | Description :
406 | Category :
407 | 409 |
410 |
411 | 412 |
413 |
footer content
414 |
415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | ``` 423 | ### src/main/webapp/WEB-INF/views/list-todos.jsp 424 | ``` 425 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 426 | 427 | 428 | 429 | Todos 430 | 432 | 433 | 442 | 443 | 444 | 445 | 446 | 461 | 462 |
463 |

Welcome ${name}

464 | 465 | Your Todos are 466 |
    467 | 468 |
  1. ${todo.name} ${todo.category} Delete
  2. 470 |
    471 |
472 | 473 |

474 | ${errorMessage} 475 |

476 | Add New Todo 478 |
479 | 480 |
481 |
footer content
482 |
483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | ``` 491 | ### src/main/webapp/WEB-INF/views/login.jsp 492 | ``` 493 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 494 | 495 | 496 | 497 | Todos 498 | 500 | 501 | 510 | 511 | 512 | 513 | 514 | 529 | 530 |
531 |
532 |

533 | ${errorMessage} 534 |

535 | Name: Password: 538 |
539 | 540 |
541 | 542 |
543 |
footer content
544 |
545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | ``` 553 | ### src/main/webapp/WEB-INF/web.xml 554 | ``` 555 | 558 | 559 | To do List 560 | 561 | 562 | login.do 563 | 564 | 565 | 566 | ``` 567 | -------------------------------------------------------------------------------- /Step27.md: -------------------------------------------------------------------------------- 1 | ## Files List 2 | ### pom.xml 3 | ``` 4 | 6 | 4.0.0 7 | com.in28minutes 8 | in28Minutes-first-webapp 9 | 0.0.1-SNAPSHOT 10 | war 11 | 12 | 13 | 14 | javax 15 | javaee-web-api 16 | 6.0 17 | provided 18 | 19 | 20 | javax.servlet 21 | jstl 22 | 1.2 23 | 24 | 25 | 26 | org.webjars 27 | bootstrap 28 | 3.3.6 29 | 30 | 31 | org.webjars 32 | jquery 33 | 1.9.1 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | org.apache.maven.plugins 42 | maven-compiler-plugin 43 | 3.2 44 | 45 | true 46 | 1.7 47 | 1.7 48 | true 49 | 50 | 51 | 52 | org.apache.tomcat.maven 53 | tomcat7-maven-plugin 54 | 2.2 55 | 56 | / 57 | true 58 | 59 | 60 | 61 | 62 | 63 | 64 | ``` 65 | ### src/main/java/com/in28minutes/filter/LoginRequiredFilter.java 66 | ``` 67 | package com.in28minutes.filter; 68 | 69 | import java.io.IOException; 70 | 71 | import javax.servlet.Filter; 72 | import javax.servlet.FilterChain; 73 | import javax.servlet.FilterConfig; 74 | import javax.servlet.ServletException; 75 | import javax.servlet.ServletRequest; 76 | import javax.servlet.ServletResponse; 77 | import javax.servlet.annotation.WebFilter; 78 | import javax.servlet.http.HttpServletRequest; 79 | 80 | @WebFilter(urlPatterns = "*.do") 81 | public class LoginRequiredFilter implements Filter { 82 | 83 | @Override 84 | public void destroy() { 85 | 86 | } 87 | 88 | @Override 89 | public void doFilter(ServletRequest servletRequest, 90 | ServletResponse servletResponse, FilterChain chain) 91 | throws IOException, ServletException { 92 | HttpServletRequest request = (HttpServletRequest) servletRequest; 93 | 94 | if (request.getSession().getAttribute("name") != null) { 95 | chain.doFilter(servletRequest, servletResponse); 96 | } else { 97 | request.getRequestDispatcher("/login.do").forward(servletRequest, 98 | servletResponse); 99 | } 100 | } 101 | 102 | @Override 103 | public void init(FilterConfig arg0) throws ServletException { 104 | } 105 | 106 | } 107 | ``` 108 | ### src/main/java/com/in28minutes/login/LoginService.java 109 | ``` 110 | package com.in28minutes.login; 111 | 112 | public class LoginService { 113 | 114 | public boolean isUserValid(String user, String password) { 115 | if (user.equals("in28Minutes") && password.equals("dummy")) 116 | return true; 117 | 118 | return false; 119 | } 120 | 121 | } 122 | ``` 123 | ### src/main/java/com/in28minutes/login/LoginServlet.java 124 | ``` 125 | package com.in28minutes.login; 126 | 127 | import java.io.IOException; 128 | 129 | import javax.servlet.ServletException; 130 | import javax.servlet.annotation.WebServlet; 131 | import javax.servlet.http.HttpServlet; 132 | import javax.servlet.http.HttpServletRequest; 133 | import javax.servlet.http.HttpServletResponse; 134 | 135 | import com.in28minutes.todo.TodoService; 136 | 137 | @WebServlet(urlPatterns = "/login.do") 138 | public class LoginServlet extends HttpServlet { 139 | 140 | private LoginService userValidationService = new LoginService(); 141 | private TodoService todoService = new TodoService(); 142 | 143 | protected void doGet(HttpServletRequest request, 144 | HttpServletResponse response) throws ServletException, IOException { 145 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 146 | request, response); 147 | } 148 | 149 | protected void doPost(HttpServletRequest request, 150 | HttpServletResponse response) throws ServletException, IOException { 151 | String name = request.getParameter("name"); 152 | String password = request.getParameter("password"); 153 | 154 | boolean isUserValid = userValidationService.isUserValid(name, password); 155 | 156 | if (isUserValid) { 157 | request.getSession().setAttribute("name", name); 158 | response.sendRedirect("/list-todos.do"); 159 | } else { 160 | request.setAttribute("errorMessage", "Invalid Credentials!"); 161 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 162 | request, response); 163 | } 164 | } 165 | 166 | } 167 | ``` 168 | ### src/main/java/com/in28minutes/logout/LogoutServlet.java 169 | ``` 170 | package com.in28minutes.logout; 171 | 172 | import java.io.IOException; 173 | 174 | import javax.servlet.ServletException; 175 | import javax.servlet.annotation.WebServlet; 176 | import javax.servlet.http.HttpServlet; 177 | import javax.servlet.http.HttpServletRequest; 178 | import javax.servlet.http.HttpServletResponse; 179 | 180 | @WebServlet(urlPatterns = "/logout.do") 181 | public class LogoutServlet extends HttpServlet { 182 | protected void doGet(HttpServletRequest request, 183 | HttpServletResponse response) throws ServletException, IOException { 184 | request.getSession().invalidate(); 185 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 186 | request, response); 187 | } 188 | } 189 | ``` 190 | ### src/main/java/com/in28minutes/todo/AddTodoServlet.java 191 | ``` 192 | package com.in28minutes.todo; 193 | 194 | import java.io.IOException; 195 | 196 | import javax.servlet.ServletException; 197 | import javax.servlet.annotation.WebServlet; 198 | import javax.servlet.http.HttpServlet; 199 | import javax.servlet.http.HttpServletRequest; 200 | import javax.servlet.http.HttpServletResponse; 201 | 202 | @WebServlet(urlPatterns = "/add-todo.do") 203 | public class AddTodoServlet extends HttpServlet { 204 | 205 | private TodoService todoService = new TodoService(); 206 | 207 | protected void doGet(HttpServletRequest request, 208 | HttpServletResponse response) throws ServletException, IOException { 209 | request.getRequestDispatcher("/WEB-INF/views/add-todo.jsp").forward( 210 | request, response); 211 | } 212 | 213 | protected void doPost(HttpServletRequest request, 214 | HttpServletResponse response) throws ServletException, IOException { 215 | String newTodo = request.getParameter("todo"); 216 | String category = request.getParameter("category"); 217 | todoService.addTodo(new Todo(newTodo, category)); 218 | response.sendRedirect("/list-todos.do"); 219 | } 220 | } 221 | ``` 222 | ### src/main/java/com/in28minutes/todo/DeleteTodoServlet.java 223 | ``` 224 | package com.in28minutes.todo; 225 | 226 | import java.io.IOException; 227 | 228 | import javax.servlet.ServletException; 229 | import javax.servlet.annotation.WebServlet; 230 | import javax.servlet.http.HttpServlet; 231 | import javax.servlet.http.HttpServletRequest; 232 | import javax.servlet.http.HttpServletResponse; 233 | 234 | @WebServlet(urlPatterns = "/delete-todo.do") 235 | public class DeleteTodoServlet extends HttpServlet { 236 | 237 | private TodoService todoService = new TodoService(); 238 | 239 | protected void doGet(HttpServletRequest request, 240 | HttpServletResponse response) throws ServletException, IOException { 241 | todoService.deleteTodo(new Todo(request.getParameter("todo"), request 242 | .getParameter("category"))); 243 | response.sendRedirect("/list-todos.do"); 244 | } 245 | } 246 | ``` 247 | ### src/main/java/com/in28minutes/todo/ListTodoServlet.java 248 | ``` 249 | package com.in28minutes.todo; 250 | 251 | import java.io.IOException; 252 | 253 | import javax.servlet.ServletException; 254 | import javax.servlet.annotation.WebServlet; 255 | import javax.servlet.http.HttpServlet; 256 | import javax.servlet.http.HttpServletRequest; 257 | import javax.servlet.http.HttpServletResponse; 258 | 259 | @WebServlet(urlPatterns = "/list-todos.do") 260 | public class ListTodoServlet extends HttpServlet { 261 | 262 | private TodoService todoService = new TodoService(); 263 | 264 | protected void doGet(HttpServletRequest request, 265 | HttpServletResponse response) throws ServletException, IOException { 266 | request.setAttribute("todos", todoService.retrieveTodos()); 267 | request.getRequestDispatcher("/WEB-INF/views/list-todos.jsp").forward( 268 | request, response); 269 | } 270 | } 271 | ``` 272 | ### src/main/java/com/in28minutes/todo/Todo.java 273 | ``` 274 | package com.in28minutes.todo; 275 | 276 | public class Todo { 277 | private String name; 278 | private String category; 279 | 280 | public Todo(String name, String category) { 281 | super(); 282 | this.name = name; 283 | this.category = category; 284 | } 285 | 286 | public String getName() { 287 | return name; 288 | } 289 | 290 | public void setName(String name) { 291 | this.name = name; 292 | } 293 | 294 | public String getCategory() { 295 | return category; 296 | } 297 | 298 | public void setCategory(String category) { 299 | this.category = category; 300 | } 301 | 302 | @Override 303 | public String toString() { 304 | return String.format("Todo [name=%s, category=%s]", name, category); 305 | } 306 | 307 | @Override 308 | public int hashCode() { 309 | final int prime = 31; 310 | int result = 1; 311 | result = prime * result + ((name == null) ? 0 : name.hashCode()); 312 | return result; 313 | } 314 | 315 | @Override 316 | public boolean equals(Object obj) { 317 | if (this == obj) 318 | return true; 319 | if (obj == null) 320 | return false; 321 | if (getClass() != obj.getClass()) 322 | return false; 323 | Todo other = (Todo) obj; 324 | if (name == null) { 325 | if (other.name != null) 326 | return false; 327 | } else if (!name.equals(other.name)) 328 | return false; 329 | return true; 330 | } 331 | 332 | } 333 | ``` 334 | ### src/main/java/com/in28minutes/todo/TodoService.java 335 | ``` 336 | package com.in28minutes.todo; 337 | 338 | import java.util.ArrayList; 339 | import java.util.List; 340 | 341 | public class TodoService { 342 | private static List todos = new ArrayList(); 343 | static { 344 | todos.add(new Todo("Learn Web Application Development", "Study")); 345 | todos.add(new Todo("Learn Spring MVC", "Study")); 346 | todos.add(new Todo("Learn Spring Rest Services", "Study")); 347 | } 348 | 349 | public List retrieveTodos() { 350 | return todos; 351 | } 352 | 353 | public void addTodo(Todo todo) { 354 | todos.add(todo); 355 | } 356 | 357 | public void deleteTodo(Todo todo) { 358 | todos.remove(todo); 359 | } 360 | 361 | } 362 | ``` 363 | ### src/main/webapp/WEB-INF/common/footer.jspf 364 | ``` 365 | 366 |
367 |
footer content
368 |
369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | ``` 377 | ### src/main/webapp/WEB-INF/common/header.jspf 378 | ``` 379 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 380 | 381 | 382 | 383 | Todos 384 | 386 | 387 | 396 | 397 | 398 | 399 | ``` 400 | ### src/main/webapp/WEB-INF/common/navigation.jspf 401 | ``` 402 | 417 | ``` 418 | ### src/main/webapp/WEB-INF/views/add-todo.jsp 419 | ``` 420 | <%@ include file="../common/header.jspf"%> 421 | <%@ include file="../common/navigation.jspf"%> 422 | 423 | 424 |
425 | Your New Action Item: 426 |
427 |
428 |
430 |
431 |
432 |
434 |
435 | 436 |
437 |
438 | 439 | <%@ include file="../common/footer.jspf"%> 440 | ``` 441 | ### src/main/webapp/WEB-INF/views/list-todos.jsp 442 | ``` 443 | <%@ include file="../common/header.jspf"%> 444 | <%@ include file="../common/navigation.jspf"%> 445 | 446 |
447 |

Welcome ${name}

448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 464 | 465 | 466 | 467 |
Your Todos are
DescriptionCategoryActions
${todo.name}${todo.category}  Delete
468 | 469 |

470 | ${errorMessage} 471 |

472 | Add New Todo 473 |
474 | 475 | <%@ include file="../common/footer.jspf"%> 476 | ``` 477 | ### src/main/webapp/WEB-INF/views/login.jsp 478 | ``` 479 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 480 | 481 | 482 | 483 | Todos 484 | 486 | 487 | 496 | 497 | 498 | 499 | 500 | 515 | 516 |
517 |
518 |

519 | ${errorMessage} 520 |

521 | Name: Password: 524 |
525 | 526 |
527 | 528 |
529 |
footer content
530 |
531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | ``` 539 | ### src/main/webapp/WEB-INF/web.xml 540 | ``` 541 | 544 | 545 | To do List 546 | 547 | 548 | login.do 549 | 550 | 551 | 552 | ``` 553 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.in28minutes 5 | in28Minutes-first-webapp 6 | 0.0.1-SNAPSHOT 7 | war 8 | 9 | 10 | 11 | javax 12 | javaee-web-api 13 | 6.0 14 | provided 15 | 16 | 17 | javax.servlet 18 | jstl 19 | 1.2 20 | 21 | 22 | 23 | org.webjars 24 | bootstrap 25 | 3.3.6 26 | 27 | 28 | org.webjars 29 | jquery 30 | 1.9.1 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.apache.maven.plugins 39 | maven-compiler-plugin 40 | 3.2 41 | 42 | true 43 | 1.7 44 | 1.7 45 | true 46 | 47 | 48 | 49 | org.apache.tomcat.maven 50 | tomcat7-maven-plugin 51 | 2.2 52 | 53 | / 54 | true 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/filter/LoginRequiredFilter.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.filter; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.Filter; 6 | import javax.servlet.FilterChain; 7 | import javax.servlet.FilterConfig; 8 | import javax.servlet.ServletException; 9 | import javax.servlet.ServletRequest; 10 | import javax.servlet.ServletResponse; 11 | import javax.servlet.annotation.WebFilter; 12 | import javax.servlet.http.HttpServletRequest; 13 | 14 | @WebFilter(urlPatterns = "*.do") 15 | public class LoginRequiredFilter implements Filter { 16 | 17 | @Override 18 | public void destroy() { 19 | 20 | } 21 | 22 | @Override 23 | public void doFilter(ServletRequest servletRequest, 24 | ServletResponse servletResponse, FilterChain chain) 25 | throws IOException, ServletException { 26 | HttpServletRequest request = (HttpServletRequest) servletRequest; 27 | 28 | if (request.getSession().getAttribute("name") != null) { 29 | chain.doFilter(servletRequest, servletResponse); 30 | } else { 31 | request.getRequestDispatcher("/login.do").forward(servletRequest, 32 | servletResponse); 33 | } 34 | } 35 | 36 | @Override 37 | public void init(FilterConfig arg0) throws ServletException { 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/login/LoginService.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.login; 2 | 3 | public class LoginService { 4 | 5 | public boolean isUserValid(String user, String password) { 6 | if (user.equals("in28Minutes") && password.equals("dummy")) 7 | return true; 8 | 9 | return false; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/login/LoginServlet.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.login; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.annotation.WebServlet; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | import com.in28minutes.todo.TodoService; 12 | 13 | /* 14 | 15 | // Method descriptor #15 ()V 16 | public void init() throws javax.servlet.ServletException; 17 | 18 | // Method descriptor #37 (Ljavax/servlet/ServletRequest;Ljavax/servlet/ServletResponse;)V 19 | public void service(javax.servlet.ServletRequest arg0, javax.servlet.ServletResponse arg1) throws javax.servlet.ServletException, java.io.IOException; 20 | 21 | // Method descriptor #15 ()V 22 | public void destroy(); 23 | 24 | */ 25 | @WebServlet(urlPatterns = "/login.do") 26 | public class LoginServlet extends HttpServlet { 27 | 28 | private LoginService userValidationService = new LoginService(); 29 | private TodoService todoService = new TodoService(); 30 | 31 | protected void doGet(HttpServletRequest request, 32 | HttpServletResponse response) throws ServletException, IOException { 33 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 34 | request, response); 35 | } 36 | 37 | protected void doPost(HttpServletRequest request, 38 | HttpServletResponse response) throws ServletException, IOException { 39 | String name = request.getParameter("name"); 40 | String password = request.getParameter("password"); 41 | 42 | boolean isUserValid = userValidationService.isUserValid(name, password); 43 | 44 | if (isUserValid) { 45 | request.getSession().setAttribute("name", name); 46 | response.sendRedirect("/list-todos.do"); 47 | } else { 48 | request.setAttribute("errorMessage", "Invalid Credentials!"); 49 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 50 | request, response); 51 | } 52 | } 53 | 54 | } -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/logout/LogoutServlet.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.logout; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.annotation.WebServlet; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | @WebServlet(urlPatterns = "/logout.do") 12 | public class LogoutServlet extends HttpServlet { 13 | protected void doGet(HttpServletRequest request, 14 | HttpServletResponse response) throws ServletException, IOException { 15 | request.getSession().invalidate(); 16 | request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward( 17 | request, response); 18 | } 19 | } -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/todo/AddTodoServlet.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.todo; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.annotation.WebServlet; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | //INITIALIZATION OF JSP 12 | //CREATE add_002dtodo_jsp.java from add-todo.jsp 13 | //compile add_002dtodo_jsp.java to add_002dtodo_jsp.class 14 | //call _jspInit 15 | 16 | //SERVICE -> _jspService 17 | 18 | //DESTROY -> _jspDestroy 19 | 20 | @WebServlet(urlPatterns = "/add-todo.do") 21 | public class AddTodoServlet extends HttpServlet { 22 | 23 | private TodoService todoService = new TodoService(); 24 | 25 | protected void doGet(HttpServletRequest request, 26 | HttpServletResponse response) throws ServletException, IOException { 27 | request.getRequestDispatcher("/WEB-INF/views/add-todo.jsp").forward( 28 | request, response); 29 | } 30 | 31 | protected void doPost(HttpServletRequest request, 32 | HttpServletResponse response) throws ServletException, IOException { 33 | String newTodo = request.getParameter("todo"); 34 | String category = request.getParameter("category"); 35 | todoService.addTodo(new Todo(newTodo, category)); 36 | response.sendRedirect("/list-todos.do"); 37 | } 38 | } -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/todo/DeleteTodoServlet.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.todo; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.annotation.WebServlet; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | @WebServlet(urlPatterns = "/delete-todo.do") 12 | public class DeleteTodoServlet extends HttpServlet { 13 | 14 | private TodoService todoService = new TodoService(); 15 | 16 | protected void doGet(HttpServletRequest request, 17 | HttpServletResponse response) throws ServletException, IOException { 18 | todoService.deleteTodo(new Todo(request.getParameter("todo"), request 19 | .getParameter("category"))); 20 | response.sendRedirect("/list-todos.do"); 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/todo/ListTodoServlet.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.todo; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.annotation.WebServlet; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | @WebServlet(urlPatterns = "/list-todos.do") 12 | public class ListTodoServlet extends HttpServlet { 13 | 14 | private TodoService todoService = new TodoService(); 15 | 16 | protected void doGet(HttpServletRequest request, 17 | HttpServletResponse response) throws ServletException, IOException { 18 | request.setAttribute("todos", todoService.retrieveTodos()); 19 | request.getRequestDispatcher("/WEB-INF/views/list-todos.jsp").forward( 20 | request, response); 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/todo/Todo.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.todo; 2 | 3 | public class Todo { 4 | private String name; 5 | private String category; 6 | 7 | public Todo(String name, String category) { 8 | super(); 9 | this.name = name; 10 | this.category = category; 11 | } 12 | 13 | public String getName() { 14 | return name; 15 | } 16 | 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | 21 | public String getCategory() { 22 | return category; 23 | } 24 | 25 | public void setCategory(String category) { 26 | this.category = category; 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return String.format("Todo [name=%s, category=%s]", name, category); 32 | } 33 | 34 | @Override 35 | public int hashCode() { 36 | final int prime = 31; 37 | int result = 1; 38 | result = prime * result + ((name == null) ? 0 : name.hashCode()); 39 | return result; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object obj) { 44 | if (this == obj) 45 | return true; 46 | if (obj == null) 47 | return false; 48 | if (getClass() != obj.getClass()) 49 | return false; 50 | Todo other = (Todo) obj; 51 | if (name == null) { 52 | if (other.name != null) 53 | return false; 54 | } else if (!name.equals(other.name)) 55 | return false; 56 | return true; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/in28minutes/todo/TodoService.java: -------------------------------------------------------------------------------- 1 | package com.in28minutes.todo; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class TodoService { 7 | private static List todos = new ArrayList(); 8 | static { 9 | todos.add(new Todo("Learn Web Application Development", "Study")); 10 | todos.add(new Todo("Learn Spring MVC", "Study")); 11 | todos.add(new Todo("Learn Spring Rest Services", "Study")); 12 | } 13 | 14 | public List retrieveTodos() { 15 | return todos; 16 | } 17 | 18 | public void addTodo(Todo todo) { 19 | todos.add(todo); 20 | } 21 | 22 | public void deleteTodo(Todo todo) { 23 | todos.remove(todo); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/common/footer.jspf: -------------------------------------------------------------------------------- 1 | 2 |
3 |
footer content
4 |
5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/common/header.jspf: -------------------------------------------------------------------------------- 1 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 2 | 3 | 4 | 5 | Todos 6 | 8 | 9 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/common/navigation.jspf: -------------------------------------------------------------------------------- 1 | 16 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/views/add-todo.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="../common/header.jspf"%> 2 | <%@ include file="../common/navigation.jspf"%> 3 | 4 | 5 |
6 | Your New Action Item: 7 |
8 |
9 |
11 |
12 |
13 |
15 |
16 | 17 |
18 |
19 | 20 | <%@ include file="../common/footer.jspf"%> -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/views/list-todos.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="../common/header.jspf"%> 2 | <%@ include file="../common/navigation.jspf"%> 3 | 4 |
5 |

Welcome ${name}

6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 22 | 23 | 24 | 25 |
Your Todos are
DescriptionCategoryActions
${todo.name}${todo.category}  Delete
26 | 27 |

28 | ${errorMessage} 29 |

30 | Add New Todo 31 |
32 | 33 | <%@ include file="../common/footer.jspf"%> -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/views/login.jsp: -------------------------------------------------------------------------------- 1 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 2 | 3 | 4 | 5 | Todos 6 | 8 | 9 | 18 | 19 | 20 | 21 | 22 | 37 | 38 |
39 |
40 |

41 | ${errorMessage} 42 |

43 | Name: Password: 46 |
47 | 48 |
49 | 50 |
51 |
footer content
52 |
53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | To do List 6 | 7 | 8 | login.do 9 | 10 | 11 | --------------------------------------------------------------------------------