Review
We have just completed our application. In the previous sections, we have discussed the functional specs, created the Java classes, declared the configuration files, and wrote the HTML form. In this section, we will build and run the application using Maven, and show how to import the project in Eclipse.Table of Contents
Part 1: Introduction and Functional SpecsPart 2: Java classes
Part 3: XML configuration
Part 4: HTML form
Part 5: Running the Application
Running the Application
Access the source code
To download the source code, please visit the project's Github repository (click here)Building with Maven
- Ensure Maven is installed
- Open a command window (Windows) or a terminal (Linux/Mac)
- Run the following command:
mvn tomcat:run
- You should see the following output:
[INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'tomcat'. [INFO] artifact org.codehaus.mojo:tomcat-maven-plugin: checking for updates from central [INFO] artifact org.codehaus.mojo:tomcat-maven-plugin: checking for updates from snapshots [INFO] ------------------------------------------ [INFO] Building spring-emailupload-tutorial Maven Webapp [INFO] task-segment: [tomcat:run] [INFO] ------------------------------------------ [INFO] Preparing tomcat:run [INFO] [apt:process {execution: default}] [INFO] [resources:resources {execution: default-resources}] [INFO] [tomcat:run {execution: default-cli}] [INFO] Running war on http://localhost:8080/spring-emailupload-tutorial Jul 8, 2012 7:22:24 PM org.apache.catalina.startup.Embedded start INFO: Starting tomcat server Jul 8, 2012 7:22:25 PM org.apache.catalina.core.StandardEngine start INFO: Starting Servlet Engine: Apache Tomcat/6.0.29 Jul 8, 2012 7:22:25 PM org.apache.catalina.core.ApplicationContext log INFO: Initializing Spring root WebApplicationContext Jul 8, 2012 7:22:27 PM org.apache.coyote.http11.Http11Protocol init INFO: Initializing Coyote HTTP/1.1 on http-8080 Jul 8, 2012 7:22:27 PM org.apache.coyote.http11.Http11Protocol start INFO: Starting Coyote HTTP/1.1 on http-8080
Note: If the project will not build due to missing repositories, please enable the repositories section in the pom.xml!
Access the Entry page
- Follow the steps with Building with Maven
- Open a browser
- Enter the following URL (8080 is the default port for Tomcat):
http://localhost:8080/spring-emailupload-tutorial/email
Compose a message
- Fill-in the To fields
- Fill-in the Cc field
- Fill-in the From fields
- Fill-in the Subject field
- Fill-in the Message field
Attach a file
- Click on "Add a file"
- Browse for a file and add it. Check the logs and you should see something similar to the following:
[DEBUG] [http-8080-4 07:29:06] (TraceInterceptor.java:writeToLog:21) Entering EmailController.upload(org.springframework.web.multipart.commons.CommonsMultipartFile@1b7a72b) [DEBUG] [http-8080-4 07:29:06] (EmailController.java:upload:49) Dummy1.jpg - 15009 [DEBUG] [http-8080-4 07:29:06] (TraceInterceptor.java:writeToLog:21) Leaving EmailController.upload(): [UploadedFile [name=Dummy1.jpg, size=15009, url=null, thumbnail_url=null, delete_url=null, delete_type=null]]
Attach multiple files
- Click on "Add another file"
- Browse for files and add them. Check the logs and you should see something similar to the following:
[DEBUG] [http-8080-4 07:29:43] (TraceInterceptor.java:writeToLog:21) Entering EmailController.upload(org.springframework.web.multipart.commons.CommonsMultipartFile@ba2b6b) [DEBUG] [http-8080-4 07:29:43] (EmailController.java:upload:49) Dummy2.jpg - 30364 [DEBUG] [http-8080-4 07:29:43] (TraceInterceptor.java:writeToLog:21) Leaving EmailController.upload(): [UploadedFile [name=Dummy2.jpg, size=30364, url=null, thumbnail_url=null, delete_url=null, delete_type=null]] [DEBUG] [http-8080-2 07:29:43] (TraceInterceptor.java:writeToLog:21) Entering EmailController.upload(org.springframework.web.multipart.commons.CommonsMultipartFile@788a7b) [DEBUG] [http-8080-2 07:29:43] (EmailController.java:upload:49) Dummy3.jpg - 17677 [DEBUG] [http-8080-2 07:29:43] (TraceInterceptor.java:writeToLog:21) Leaving EmailController.upload(): [UploadedFile [name=Dummy3.jpg, size=17677, url=null, thumbnail_url=null, delete_url=null, delete_type=null]]
Send the email
- Click on "Send" to send the email. Check the logs and you should see something similar to the following:
[DEBUG] [http-8080-1 07:32:09] (TraceInterceptor.java:writeToLog:21) Entering EmailController.send(Message [senderName=, senderEmail=krams915@yahoo.com, ccEmail=, subject=Test, body=Test, receiverName=, receiverEmail=flamark_915@yahoo.com, filename=Dummy1.jpg,Dummy2.jpg,Dummy3.jpg]) [DEBUG] [http-8080-1 07:32:09] (TraceInterceptor.java:writeToLog:21) Entering SendGridEmailService.send(Message [senderName=, senderEmail=krams915@yahoo.com, ccEmail=, subject=Test, body=Test, receiverName=, receiverEmail=flamark_915@yahoo.com, filename=Dummy1.jpg,Dummy2.jpg,Dummy3.jpg]) [DEBUG] [http-8080-1 07:32:20] (TraceInterceptor.java:writeToLog:21) Leaving SendGridEmailService.send(): StatusResponse [success=true, message=Message sent, ] [DEBUG] [http-8080-1 07:32:20] (TraceInterceptor.java:writeToLog:21) Leaving EmailController.send(): StatusResponse [success=true, message=Message sent, ]
- Also check your email account for the actual email message
Note:
When adding a file via the "Add a file" link, the file is automatically uploaded to the backend. The backend responds with an object containing the filename (and other file details). When you click on the "Upload" button, the form data is sent along with the filename. The file itself is never uploaded in this second step because it's already in the server!Import the project in Eclipse
- Ensure Maven is installed
- Open a command window (Windows) or a terminal (Linux/Mac)
- Run the following command:
mvn eclipse:eclipse -Dwtpversion=2.0
- You should see the following output:
[INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'eclipse'. [INFO] org.apache.maven.plugins: checking for updates from central [INFO] org.apache.maven.plugins: checking for updates from snapshots [INFO] org.codehaus.mojo: checking for updates from central [INFO] org.codehaus.mojo: checking for updates from snapshots [INFO] artifact org.apache.maven.plugins:maven-eclipse-plugin: checking for updates from central [INFO] artifact org.apache.maven.plugins:maven-eclipse-plugin: checking for updates from snapshots [INFO] ----------------------------------------- [INFO] Building spring-emailupload-tutorial Maven Webapp [INFO] task-segment: [eclipse:eclipse] [INFO] ----------------------------------------- [INFO] Preparing eclipse:eclipse [INFO] No goals needed for project - skipping [INFO] [eclipse:eclipse {execution: default-cli}] [INFO] Adding support for WTP version 2.0. [INFO] ----------------------------------------- [INFO] BUILD SUCCESSFUL [INFO] -----------------------------------------
This command will add the following files to your project:
.classpath .project .settings target
You may have to enable "show hidden files" in your file explorer to view them
- Open Eclipse and import the project
Conclusion
That's it! We've have successfully completed our email application with Spring, SendGrid, and jQuery-File-Upload. We've also learned how to setup an HTML form for attaching multiple files.I hope you've enjoyed this tutorial. Don't forget to check my other tutorials at the Tutorials section.
Revision History
Revision | Date | Description |
---|---|---|
1 | July 8 2012 | Uploaded tutorial |
Share the joy:
|
Subscribe by reader Subscribe by email Share
Krams, thanks for your tutorial. File successfully written to disk, but i receive Exception in thread "http-bio-8084-exec-5" java.lang.OutOfMemoryError: PermGen space
ReplyDeletewhen try to load file to server.
I also try put next code to catalina.sh file, but it dont help me.
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1536m
-Xmx1536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m
-XX:MaxPermSize=256m -XX:+DisableExplicitGC"
i successfully resolve my trouble by replacing in catalina.sh file JAVA_OPTS to CATALINA_OPTS.
ReplyDeleteBut i all the same not understand why recieve java.lang.OutOfMemoryError: PermGen during file loading by size around 100 Kbytes.
If i try to load file with no english name i get the file on disk and on success window with name like Фото.
ReplyDeleteHow to solve this trouble?
I got to solve this problem.
DeleteLook at https://issues.springsource.org/browse/SPR-6443 .
This comment has been removed by the author.
ReplyDeletecan you tell how to save these feilds in database mysql?
ReplyDeleteI have read your blog its very attractive and impressive. I like it your blog.
ReplyDeleteSpring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training
spring training in chennai spring hibernate training in chennai
Great post, please keep on sharing amazing article like this! It makes me happy reading your post. Celebrity net worth is a website which reports estimates of the total assets and financial activities of celebrities. Read it for more information.
ReplyDeleteI enjoyed your blog Thanks for sharing such an informative post. We are also providing the best services click on below links to visit our website.
ReplyDeletedigital marketing company in nagercoil
digital marketing services in nagercoil
digital marketing agency in nagercoil
best marketing services in nagercoil
SEO company in nagercoil
SEO services in nagercoil
social media marketing in nagercoil
social media company in nagercoil
PPC services in nagercoil
digital marketing company in velachery
digital marketing company in velachery
digital marketing services in velachery
digital marketing agency in velachery
SEO company in velachery
SEO services in velachery
social media marketing in velachery
social media company in velachery
PPC services in velachery
online advertisement services in velachery
online advertisement services in nagercoil
web design company in nagercoil
web development company in nagercoil
website design company in nagercoil
website development company in nagercoil
web designing company in nagercoil
website designing company in nagercoil
best web design company in nagercoil
web design company in velachery
web development company in velachery
website design company in velachery
website development company in velachery
web designing company in velachery
website designing company in velachery
best web design company in velachery
Thanks for Sharing - ( Groarz branding solutions )