├── .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 | [ 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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 | ${todo.name}
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 |
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 | ${todo.name}
228 |
229 |
230 |
231 |
${errorMessage}
232 |
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 |
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 | ${todo.name} Delete
285 |
286 |
287 |
288 |
${errorMessage}
289 |
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 |
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 | ${todo.name} Delete
301 |
302 |
303 |
304 |
305 | ${errorMessage}
306 |
307 |
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 |
25 |
26 | Brand
27 |
28 |
33 |
34 |
37 |
38 |
39 |
40 |
41 |
Heading
42 | Body of the Page
43 |
44 |
45 |
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 |
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 |
372 |
373 |
376 |
377 |
387 |
388 |
389 |
390 |
391 |
Welcome ${name}
392 |
393 | Your Todos are
394 |
395 |
396 | ${todo.name} Delete
398 |
399 |
400 |
401 |
402 | ${errorMessage}
403 |
404 |
408 |
409 |
410 |
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 |
369 |
370 | Brand
371 |
372 |
377 |
378 |
381 |
382 |
383 |
384 |
395 |
396 |
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 |
431 |
432 | Brand
433 |
434 |
439 |
440 |
443 |
444 |
445 |
446 |
447 |
Welcome ${name}
448 |
449 | Your Todos are
450 |
451 |
452 | ${todo.name} Delete
454 |
455 |
456 |
457 |
458 | ${errorMessage}
459 |
460 |
464 |
465 |
466 |
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 |
375 |
376 | Brand
377 |
378 |
383 |
384 |
387 |
388 |
389 |
390 |
391 | Your New Action Item:
392 |
396 |
397 |
398 |
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 |
433 |
434 | Brand
435 |
436 |
441 |
442 |
445 |
446 |
447 |
448 |
449 |
Welcome ${name}
450 |
451 | Your Todos are
452 |
453 |
454 | ${todo.name} Delete
456 |
457 |
458 |
459 |
460 | ${errorMessage}
461 |
462 |
Add New Todo
464 |
465 |
466 |
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 |
501 |
502 | Brand
503 |
504 |
509 |
510 |
513 |
514 |
515 |
516 |
527 |
528 |
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 |
387 |
388 | Brand
389 |
390 |
395 |
396 |
399 |
400 |
401 |
402 |
403 | Your New Action Item:
404 |
410 |
411 |
412 |
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 |
447 |
448 | Brand
449 |
450 |
455 |
456 |
459 |
460 |
461 |
462 |
463 |
Welcome ${name}
464 |
465 | Your Todos are
466 |
467 |
468 | ${todo.name} ${todo.category} Delete
470 |
471 |
472 |
473 |
474 | ${errorMessage}
475 |
476 |
Add New Todo
478 |
479 |
480 |
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 |
515 |
516 | Brand
517 |
518 |
523 |
524 |
527 |
528 |
529 |
530 |
541 |
542 |
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 |
369 |
370 |
371 |
372 |
373 |