Skip to content

Commit 0932951

Browse files
buddhini81adamd1985
authored andcommitted
Modifications to the JavaEEAnnotationsSample application (eugenp#1522)
* Delete the class As it is not relevant for the example * Update AccountServlet.java Changes made in doPost method. * Update UploadCustomerDocumentsServlet.java Changes made in doPost method * README file added * Fix error - add missing import
1 parent 54615dd commit 0932951

4 files changed

Lines changed: 75 additions & 27 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
About the application
2+
---------------------
3+
This application demonstrates the usage of JavaEE Web Annotations.
4+
5+
6+
Contents of the application
7+
---------------------------
8+
1. AccountServlet.java - Demonstrates the @WebServlet and @ServletSecurity annotation.
9+
10+
NOTES: @WebServlet annotation designates the AccountServlet class as a Servlet component.
11+
The usage of its parameters 'urlPatterns' & 'initParams' can be observed.
12+
An initialization parameter 'type' is being set to denote the type of the bank account.
13+
14+
@ServletSecurity annotation imposes security constraints on the AccountServlet based on
15+
the tomcat-users.xml (this code assumes there is a role 'admin' in your tomcat-users.xml)
16+
17+
N.B : To see @ServletSecurity annotation in action, please uncomment the annotation code
18+
for @ServletSecurity.
19+
20+
21+
2. BankAppServletContextListener.java - Demonstrates the @WebListener annotation for denoting a class as a ServletContextListener.
22+
23+
NOTES: Sets a Servlet context attribute ATTR_DEFAULT_LANGUAGE to 'english' on web application start up,
24+
which can then be used throughout the application.
25+
26+
27+
3. LogInFilter.java - Demonstrates the @WebFilter annotation.
28+
29+
NOTES: @WebFilter annotation designates the LogInFilter class as a Filter component.
30+
It filters all requests to the bank account servlet and redirects them to
31+
the login page.
32+
33+
N.B : To see @WebFilter annotation in action, please uncomment the annotation code for @WebFilter.
34+
35+
36+
4. UploadCustomerDocumentsServlet.java - Demonstrates the @MultipartConfig annotation.
37+
38+
NOTES: @MultipartConfig anotation designates the UploadCustomerDocumentsServlet Servlet component,
39+
to handle multipart/form-data requests.
40+
To see it in action, deploy the web application an access the url: http://<your host>:<your port>/JavaEEAnnotationsSample/upload.jsp
41+
Once you upload a file from here, it will get uploaded to D:/custDocs (assuming such a folder exists).
42+
43+
44+
5. index.jsp - This is the welcome page.
45+
46+
NOTES: You can enter a deposit amount here and click on the 'Deposit' button to see the AccountServlet in action.
47+
48+
6. login.jsp - All requests to the AccountServlet are redirected to this page, if the LogInFilter is imposed.
49+
50+
7. upload.jsp - Demonstrates the usage of handling multipart/form-data requests by the UploadCustomerDocumentsServlet.
51+
52+
53+
Building and Running the application
54+
------------------------------------
55+
To build the application:
56+
57+
1. Open the project in eclipse
58+
2. Right click on it in eclispe and choose Run As > Maven build
59+
3. Give 'clean install' under Goals
60+
4. This should build the WAR file of the application
61+
62+
To run the application:
63+
64+
1. Right click on the project
65+
2. Run as > Run on Server
66+
3. This will start you Tomcat server and deploy the application (Provided that you have configured Tomcat in your eclipse)
67+
4. You should now be able to access the url : http://<your host>:<your port>/JavaEEAnnotationsSample

‎jee7/src/main/java/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/java/com/baeldung/javaeeannotations/AccountServlet.java‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,15 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
3636
}
3737

3838
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
39+
3940
double accountBalance = 1000d;
40-
double interestRate = Double.parseDouble(request.getAttribute("interest").toString());
41-
4241
String paramDepositAmt = request.getParameter("dep");
4342
double depositAmt = Double.parseDouble(paramDepositAmt);
4443

4544
accountBalance = accountBalance + depositAmt;
46-
45+
4746
PrintWriter writer = response.getWriter();
48-
writer.println("<html> Balance of " + accountType + " account is: " +
49-
accountBalance + "<br> This account bares an interest rate of " + interestRate +
50-
" % </html>");
47+
writer.println("<html> Balance of " + accountType + " account is: " + accountBalance + "</html>");
5148
writer.flush();
52-
5349
}
5450
}

‎jee7/src/main/java/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/java/com/baeldung/javaeeannotations/DepositRequestListener.java‎

Lines changed: 0 additions & 20 deletions
This file was deleted.

‎jee7/src/main/java/com/baeldung/javaeeannotations/JavaEEAnnotationsSample/src/main/java/com/baeldung/javaeeannotations/UploadCustomerDocumentsServlet.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.baeldung.javaeeannotations;
22

33
import java.io.IOException;
4+
import java.io.PrintWriter;
45

56
import javax.servlet.ServletException;
67
import javax.servlet.annotation.MultipartConfig;
@@ -24,6 +25,10 @@ protected void doPost(
2425
for (Part part : request.getParts()) {
2526
part.write("myFile");
2627
}
28+
29+
PrintWriter writer = response.getWriter();
30+
writer.println("<html>File uploaded successfully!</html>");
31+
writer.flush();
2732
}
2833

2934
}

0 commit comments

Comments
 (0)