Struts HTML File Tag ( < html:file > )

Struts HTML Tag Library

Struts HTML tag library provides tags which are used to create input forms and HTML user interfaces. The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms.

Syntax to use Struts HTML tag library

<%@ taglib prefix="html" uri="" %>

< html:file >

< html:file > -renders a file input field.Property should be the corresponding FormFile type.As with the corresponding HTML < input> element, the enclosing form element must specify “POST” for the method attribute, and “multipart/form-data” for the enctype attribute.

Example Code for < html:file >

1.Create an Jsp page and name it as file.jsp.It is the Welcome page for a user.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="" %>
<%@taglib prefix="bean" uri="" %>

< html>
    < head>

        < title> HTML File example </title>
    < head>
    < body bgcolor="#DDDDDD">
        < h1 > Struts html:file example </h1>

        < html:form action="/filepath" method="POST" enctype="multipart/form-data">

           < h3> Please Enter File Name < /h3>
		   < h3> < bean:write name="fileform" filter="false" property="error"/> </h3>
		   File Name: < html:file property="file"/> < br> < br>
		   < html:submit value="Submit"/>
            < html:reset value="Reset"/>

2.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.Here we have one property to hold in tha form bean “file” of type FormFile which belongs to “org.apache.struts.upload” package to hold the file specified in the file input field.

package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class fileform extends org.apache.struts.action.ActionForm {
    private FormFile file;
    private String contenttype;
    private int size;
    private String fname;
    private String error;

    public String getContenttype() {
        return contenttype;

    public void setContenttype(String contenttype) {
        this.contenttype = contenttype;

    public String getError() {
        return error;

    public void setError(String error) {
        this.error = error;

     public FormFile getFile() {
        return file;

    public void setFile(FormFile file) {
        this.file = file;

    public String getFname() {
        return fname;

    public void setFname(String fname) {
        this.fname = fname;

    public int getSize() {
        return size;

    public void setSize(int size) {
        this.size = size;

    public fileform() {



3.Simple Action class which is a sub class of Action class used to process the user’s request.In this class we check for null values entered in the file input field and set their respective error message.

package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class formaction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
     private final static String FAILURE = "failure";

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        fileform formBean=(fileform)form;
        FormFile myFile=formBean.getFile();
        String fname=myFile.getFileName();
        String ctype=myFile.getContentType();
        int size=myFile.getFileSize();
        if(fname.equals("") &amp;&amp; size==0)
            formBean.setError("&lt; span style='color:red'&gt; Enter any File Name&lt;/span&gt;");
            return mapping.findForward(FAILURE);
        return mapping.findForward(SUCCESS);

4.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

< struts-config>
    < form-beans>
        < form-bean name="fileform" type="com.myapp.struts.fileform"/>

< action-mappings>
        < action name="fileform" path="/filepath" scope="request" type="com.myapp.struts.formaction" validate="false">
        < forward name="success" path="/uploadsuccess.jsp"/>
		< forward name="failure" path="/file.jsp"/>


5.Create another simple Jsp page uploadsuccess.jspwhich is for displaying the output Message.If file input field is empty and by checking size and file name error message is displayed on the same page itself.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="" prefix="bean" %>

< html>
    < head>
        < meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        < title> Struts File Tag Upload success File </title>
    < body bgcolor="#DDDDDD">
	 < h1> File Uploaded Successfully...	</h1>
        < h3> Uploaded File Details are:	</h3>
		 < h4> File Name:< span style="color:#0055BB"> < bean:write name="fileform" property="fname" filter="false"/> < /span>< /h4>
        < h4> File Content Type:< span style="color:#0055BB"> < bean:write name="fileform" property="contenttype" filter="false"/> < /span>< /h4>
        < h4> File Size: < span style="color:#0055BB"> < bean:write name="fileform" property="size" filter="false"/> bytes< /span>< /h4>

6.Building and running the application

Access page:http://localhost:8084/file/

Struts Framework Articles

also read:

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!