Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Struts
1.2.
Interceptor
2.
fileUpload Interceptor
3.
Examples
3.1.
Create UserImage.jsp
3.2.
Create SuccessUserImage.jsp
4.
Configuration Properties
5.
Error Messages
6.
Frequently Asked Questions
6.1.
What are the uses of Struts?
6.2.
Is Struts front end or back end?
6.3.
What is the difference between Struts and JSP?
6.4.
What is Struts .xml?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

fileUpload Interceptor

Introduction

Hello Ninjas,

Welcome to Coding Ninjas Studio.

Welcome Image

In this article, we will discuss the fileUpload Interceptor but before talking about the Interceptors, let us know about the Struts.

Struts

Craig McClanahan created the struts framework, which was donated to Apache Foundation in May 2000. In June 2001, the first version of Struts, i.e., Struts 1.0, was launched.

Now we are using Struts 2, which is used to develop Model View Controller (MVC)-based web applications.

Struts 2 is the combination of the webwork framework of opensymphony and struts 1.

It provides support to POJO-based actions, Validation Support, AJAX Support, Integration support to various frameworks such as Hibernate, Spring, Tiles, etc., and support to various result types such as Freemarker, Velocity, JSP, etc.

Interceptor

Interceptor is an object invoked during the preprocessing and postprocessing of a request. In Struts 2, It performs operations such as validation, exception handling, internationalization, displaying the intermediate results, etc.

In this article, we will discuss fileUpload Interceptor.

fileUpload Interceptor

File Upload Image

The fileUpload Interceptor works for all the requests automatically that include files. The fielUpload Interceptor controls the working of file upload in struts2. For example, defining allowed types, maximum file size, etc.

Parameters of fileUpload Interceptor

Following are the two parameters defined for the fileUpload Interceptor.

  • maximumSize - It indicates the maximum size of the file to be uploaded.
  • allowedTypes - It indicates the allowed types, i.e., which file format to upload, like jpeg, jpg, png, etc.

 

Automatically added parameters

It automatically adds two parameters to the request:

  • String fileName: It represents the filename of the file.
  • String contentType: It specifies the content type of the file.

 

The fileName and contentType name depend on the request parameter for the file. If the filename is a name, you need to use nameFileName and nameContentType.

Now, let's see some examples of Image Upload using Struts2.

Examples

Create UserImage.jsp

This jsp page creates a form using struts UI tags. It allows users to upload the file.

<%@ page contentType="text/html; charset=UTF-8"%>  
<%@ taglib prefix="s" uri="/struts-tags"%>  
<html>  
    <head>  
        <title>Upload User Image</title>  
    </head>  
    <body>  
        <h2>  
            File Upload Example Using Struts 2 (without
        </h2>  
        <s:actionerror />  
        <s:form action="userImage" method="post" enctype="multipart/form-data">  
            <s:file name="userImage" label="Image" />  
            <s:submit value="Upload" align="center" />  
        </s:form>  
    </body>  
</html>  

Create SuccessUserImage.jsp

This jsp page creates a form using struts UI tags. It receives the name, password, and email id from the user.

<%@ page contentType="text/html; charset=UTF-8"%><%@ taglib prefix="s"  
    uri="/struts-tags"%>  
<html>  
    <head>  
        <title>Success: Upload User Image</title>  
    </head>  
    <body>  
        <h2>  
            File Upload Example Using Struts 2
        </h2>  
        User Image: <s:property value="userImage" /><br/>  
        Content Type:<s:property value="userImageContentType" /><br/>  
        File Name: <s:property value="userImageFileName" /><br/>  
        Uploaded Image: <img src="userimages/<s:property value="userImageFileName"/>"   
                                                     width="100" height="100" />  
    </body>  
</html>  

Configuration Properties

We are using the following configuration properties to control the file uploading process.

  • struts.multipart.maxSize - It indicates the maximum size of a file to be accepted for uploading. The size must be in bytes.
  • struts.multipart.parser - This library is used to upload the multipart form. By default, it is Jakarta.
  • struts.multipart.saveDir - It indicates the location to store the temporary file. By default is javax.servlet.context.tempdir.

 

Java Image

We know we can't be perfect, but we can reduce the possibility of error by understanding some common errors that will happen.

So, let's discuss some error message keys to avoid the error.

Error Messages

  • Struts.messages.error.uploading - This error generally occurs when the file cannot be uploaded.
  • Struts.messages.error.file.too.large - This error occurs when the uploaded file is too large as specified by the parameters, maximumSize.
  • Struts.messages.error.content.type.not.allowed - This error occurs when the uploaded file does not match the expected content types specified.

 

We have discussed the fileUpload Interceptor, including its parameters, configuration properties, etc. Now let's see some FAQs.

Frequently Asked Questions

What are the uses of Struts?

Struts enable us to create maintainable, extensible, and flexible web applications based on standard technologies, such as JSP pages, JavaBeans, resource bundles, and XML.

Is Struts front end or back end?

Struts is used to develop only the frontend frameworks, while we can use Hibernate to develop backend systems.

What is the difference between Struts and JSP?

Struts is a framework, and JSP is a web-tier technology.

What is Struts .xml?

The struts .xml file contains the configuration information you will modify as actions are developed.

Conclusion

We have discussed Struts and one of its Interceptors, i.e., fileUpload Interceptors, with their examples.

After reading about the fileUpload Interceptor, are you not feeling excited to read/explore more articles on Data Structures and Algorithms? Don't worry; Coding Ninjas has you covered. See JavaJSP-XMLJSPIntroduction to JSP, and  Spring Boot to learn.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Conclusion Image

Live masterclass