Struts <html:hidden> Tag

In this Struts example, you will learn to create a HTML hidden field with Struts <html:hidden> tag. In order to use the Struts HTML Tags you need to include the following taglib directive in the jsp page.

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> 

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml


1. Create JSP

Now we will create a HTML hidden field using Struts’s HTML tag

Write the following code in login.jsp

<%@taglib uri="" prefix="html"%>
<title>Struts <Html:hidden> Hidden Field</title>
<html:form action="/login">
    User Name : <html:text property="userName"/>
    <html:hidden property="userType" value="admin" />

Now create another JSP success.jsp that display the text written in hidden field. Write the following code in success.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="" prefix="bean"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Success Page</title>
        Login Success. Welcome <bean:write name="LoginForm" property="userName"></bean:write>
        <br>Hidden Field Value :  <bean:write name="LoginForm" property="userType"></bean:write>

2. Action Form

Create a Action Form class and name it that contains the following code.

package com.kruders.form;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm{
    private static final long serialVersionUID = 1L;
    private String userName;
    private String userType;
    public String getUserName() {
        return userName;
    public void setUserName(String userName) {
        this.userName = userName;
    public String getUserType() {
        return userType;
    public void setUserType(String userType) {
        this.userType = userType;
    public static long getSerialversionuid() {
        return serialVersionUID;

3. Action Class (Controller)

Let us create an Action class that will handles and forward the request.

package com.kruders.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.kruders.form.LoginForm;
public class LoginAction extends Action{
    public ActionForward execute(ActionMapping mapping,ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        return mapping.findForward("success");

4. Struts Config

Write the following code in struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
        <form-bean name="LoginForm" type="com.kruders.form.LoginForm"/>
        <forward name="login" path="/"/>
        <action input="/login.jsp" name="LoginForm" path="/login"  validate="true" scope="request" type="com.kruders.action.LoginAction">
            <forward name="success" path="/success.jsp" />

5. Deployment Descriptor

Now configure the deployment descriptor. Here, we have asked the container to give our ActionServlet any request that matches the pattern *.do

Add the following configuration information in the web.xml file


6. Run Project

Now when you run the project, following screen will be displayed as shown in Figure 20.1

Figure 20.1 Figure 20.1

Now when you enter the value in text box and click submit button, following screen will be displayed as shown in Figure 20.2

Figure 20.2 Figure 20.2

The folder structure of the example is shown below in Figure 20.3

Figure 20.3 Figure 20.3

You can download the source code of this example here.


No comments yet.

Leave a Reply