Modifications to the JavaEEAnnotationsSample application (#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
This commit is contained in:
buddhini81 2017-04-03 02:04:46 +05:30 committed by adamd1985
parent 54615ddd11
commit 09329512cd
4 changed files with 75 additions and 27 deletions

View File

@ -0,0 +1,67 @@
About the application
---------------------
This application demonstrates the usage of JavaEE Web Annotations.
Contents of the application
---------------------------
1. AccountServlet.java - Demonstrates the @WebServlet and @ServletSecurity annotation.
NOTES: @WebServlet annotation designates the AccountServlet class as a Servlet component.
The usage of its parameters 'urlPatterns' & 'initParams' can be observed.
An initialization parameter 'type' is being set to denote the type of the bank account.
@ServletSecurity annotation imposes security constraints on the AccountServlet based on
the tomcat-users.xml (this code assumes there is a role 'admin' in your tomcat-users.xml)
N.B : To see @ServletSecurity annotation in action, please uncomment the annotation code
for @ServletSecurity.
2. BankAppServletContextListener.java - Demonstrates the @WebListener annotation for denoting a class as a ServletContextListener.
NOTES: Sets a Servlet context attribute ATTR_DEFAULT_LANGUAGE to 'english' on web application start up,
which can then be used throughout the application.
3. LogInFilter.java - Demonstrates the @WebFilter annotation.
NOTES: @WebFilter annotation designates the LogInFilter class as a Filter component.
It filters all requests to the bank account servlet and redirects them to
the login page.
N.B : To see @WebFilter annotation in action, please uncomment the annotation code for @WebFilter.
4. UploadCustomerDocumentsServlet.java - Demonstrates the @MultipartConfig annotation.
NOTES: @MultipartConfig anotation designates the UploadCustomerDocumentsServlet Servlet component,
to handle multipart/form-data requests.
To see it in action, deploy the web application an access the url: http://<your host>:<your port>/JavaEEAnnotationsSample/upload.jsp
Once you upload a file from here, it will get uploaded to D:/custDocs (assuming such a folder exists).
5. index.jsp - This is the welcome page.
NOTES: You can enter a deposit amount here and click on the 'Deposit' button to see the AccountServlet in action.
6. login.jsp - All requests to the AccountServlet are redirected to this page, if the LogInFilter is imposed.
7. upload.jsp - Demonstrates the usage of handling multipart/form-data requests by the UploadCustomerDocumentsServlet.
Building and Running the application
------------------------------------
To build the application:
1. Open the project in eclipse
2. Right click on it in eclispe and choose Run As > Maven build
3. Give 'clean install' under Goals
4. This should build the WAR file of the application
To run the application:
1. Right click on the project
2. Run as > Run on Server
3. This will start you Tomcat server and deploy the application (Provided that you have configured Tomcat in your eclipse)
4. You should now be able to access the url : http://<your host>:<your port>/JavaEEAnnotationsSample

View File

@ -36,19 +36,15 @@ public class AccountServlet extends javax.servlet.http.HttpServlet {
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
double accountBalance = 1000d;
double interestRate = Double.parseDouble(request.getAttribute("interest").toString());
String paramDepositAmt = request.getParameter("dep");
double depositAmt = Double.parseDouble(paramDepositAmt);
accountBalance = accountBalance + depositAmt;
PrintWriter writer = response.getWriter();
writer.println("<html> Balance of " + accountType + " account is: " +
accountBalance + "<br> This account bares an interest rate of " + interestRate +
" % </html>");
writer.println("<html> Balance of " + accountType + " account is: " + accountBalance + "</html>");
writer.flush();
}
}

View File

@ -1,20 +0,0 @@
package com.baeldung.javaeeannotations;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
@WebListener
public class DepositRequestListener implements ServletRequestListener {
public void requestDestroyed(ServletRequestEvent event) {
}
public void requestInitialized(ServletRequestEvent evet) {
HttpServletRequest req = (HttpServletRequest)evet.getServletRequest();
req.setAttribute("interest", new Double(10));
}
}

View File

@ -1,6 +1,7 @@
package com.baeldung.javaeeannotations;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
@ -24,6 +25,10 @@ public class UploadCustomerDocumentsServlet extends HttpServlet {
for (Part part : request.getParts()) {
part.write("myFile");
}
PrintWriter writer = response.getWriter();
writer.println("<html>File uploaded successfully!</html>");
writer.flush();
}
}