OOP-ResearchMake It Simpler by Object Oriented Programming

File upload Servlets/JSP example / How to send e-mails from Servlets by JavaMail API

Free source code example for file upload Servlets/JSP by multipart/form-data. Example for how to send e-mails from Servlets/JSP by JavaMail API at the scheduled time.

This page lists the free source code examples for Servlets/JSP programming. For example, you can easily create Photo Album Servlets/JSP, to which you can upload the image files by multipart/form-data.

Another solution... file upload by e-mail :

The file upload by multipart/form-data works in most cases. However, a growing number of the WAP phones are shipped with the digital cameras, i.e. the users can take the pictures by their mobile phones and send them to their friends by the e-mails. And, by OOP Pop3Receiver API, you can take the advantage of this feature. It is the Java API for connecting to the POP3 servers through JavaMail API, and you can extract the file attachment from the received e-mail. Be ready for file upload from WAP phone!


PRODUCT: : Product by OOP-Research
DOC: : Documentation written by OOP-Research
PATCH: : Patch for the other vendor's system


DOC:First example of FormGenerator API

This is the first-step example of OOP FormGenerator API. This source code example consists of 2 Servlet:

  • example.formgen1.Cgi
  • example.formgen1.Verify

The first one generates the empty CGI FORM.

CGI FORM
[Empty CGI FORM]

The POST request from this CGI FORM will be sent to the second Servlet. The second Servlet verifies all the request parameters. If some request parameter is invalid, the second Servlet shows the same CGI FORM again. But, at this time, the CGI FORM includes the previous request parameters in it, as the default values. In addition, this CGI FORM indicates which input is invalid.

CGI FORM with invalid input
[CGI FORM with invalid input]

The POST request from this CGI FORM will be sent to this Servlet again. When all the request parameters are verified, the second Servlet shows the HTML table including them.

In this source code example, all the response from Java Servlet (web application GUI, or View part of MVC (Model View Controller)) can be generated from the normal HTML (or any mark up language such as XHTML, WML and HDML). Unlike the JSP/JSTL based solution (such as Apache Struts or JavaServer Faces), all the response can be written in just the HTML tags. The web designers (or page authors) need not learn even a bit of custom tag libraries in JSP/JSTL.
Once the HTML is prepared, the Java Swing GUI based conversion tool generates the XML. While in the conversion, you will be asked to specify the acceptable data type for each request parameter from the CGI FORM. Given this XML, a few lines of codes will be enough for the input validation in your Servlet.



Back to Top Read More

DOC:Second example of FormGenerator API

This is the second example of OOP FormGenerator API. In case of the JSP/JSTL based solution like Apache Struts or JavaServer Faces, the localized message should be written in the property resource file for the expected Locale. In the framework design by OOP FormGenerator API, Localization (L10n) of Servlet can be very easy. For each expected Locale (language preference), the localized messages can be written in the normal HTML. So, there is not difficulty even in the 2 bytes characters such as Japanese Shift_JIS, Chinese BIG5 or GB2312.

In this example, we add the additional text input to the CGI FORM of the first example. The new text field in this example asks the user to enter the telephone number. The expected pattern of the telephone number differs with the countries. In U.S., the telephone number is something like 123-456-7890:

Input validation for U.S.
[Input validation for U.S.]

But it should be 12-34-56-78-90 in France:

Input validation for France
[Input validation for France]

This means the different algorithm must be applied to the input validation for each Locale. It may sound somewhat difficult. But, don't worry! Because the acceptable data type can also be written in the XML, FormGenerator API makes the things very easy.



Back to Top Read More

DOC:Free source code examples of file upload Servlet/JSP by multipart/form-data

These examples will show you how to upload the files by Servlet / JSP through multipart/form-data.

They depend on OOP MimeParser, the Java API for file upload Servlet/JSP. This API parses the InputStream of multipart/form-data and saves the uploaded files under the specified directory. With this Java API, you can:

  • Restrict the size of the file to be uploaded
  • Restrict the mime-types (Content-Type) of the file to be uploaded
  • Restrict the width/height of the image file (JPEG, GIF or PNG)
  • Specify where to save the uploaded file

If the user tries to upload the larger files or the files of the invalid mime-types, this API throws the exception. In this case, your Servlet/JSP can catch the exception and show the appropriate message. Your Servlets/JSP has the full control over where to save the uploaded files.
With the easy programming interface of this API, a few lines of code will be enough for your file upload Servlet/JSP. Please download these source code examples and try them.



Go Site Map Read More

DOC:Source code example for how to send e-mail from Servlet with JavaMail API

How to send e-mail from Servlet / JSP with JavaMail API. You can upload files by multipart/form-data and attache them to the e-mail. You can send e-mails at the scheduled time. When you specify when the e-mail will be sent, the available TimeZones will be listed and you can select the favorite Time Zone among them. The e-mail is stored into the SQL table by the Object Serialization. Once the e-mail is queued, the back-ground Thread will select it at the scheduled time and send it.



Go Site Map Read More
Caution!
All the APIs for Servlet/JSP introduced by this web site are now included in Bento framework:
  • Simpler than JSTL or Apache Struts
  • MVC framework by HTML
  • Input validation from CGI FORM
  • Easy user authentication
  • Easy localization (L10n)
To download the APIs and source code examples, please visit the web site of Bento framework.


JBuilder 2007


General Information

For Java Development

Java and all Java-based trademarks and logos are trademarks or registered of Sun Microsystems, Inc. in the United States and other countries.


ALL CONTENTS COPYRIGHT 1997-2007, OOP-Research Corporation. All rights reserved.