Struts 2 <s:password> password example

Struts 2 <s:password> password example

Previous Home Next

 

The Struts 2.0 framework provide password input element through the<s:password></s:password> tag. 
This tag provide the facility to get password input .
The Struts 2 password component provide the following <s:password></s:password> tag:

<s:password label="Password" name="password" />


 
Directory Structure of <s:password> tag Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>

<s:form action="login">
<s:textfield name="name" label="Name"></s:textfield>
<s:password name="password" label="Password"></s:password>
<s:submit value="Login"></s:submit>
</s:form>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <filter>
  <filter-name>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration
2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="login" class="org.r4r.LoginAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>

LoginAction.java

package org.r4r;

public class LoginAction {
	private String name;
	private String password;
	
	public String execute(){
		if(name.startsWith("a")||password.startsWith("b"))
			return "success";
		else
			return "error";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	

}

success.jsp

<%@taglib uri="/struts-tags" prefix="s" %>
<h3>You are successfully login</h3>
<br/>
Welcome ,<s:property value="name"/>

error.jsp

<h3>Invalid username and password</h3><br/>
<jsp:include page="index.jsp"></jsp:include>

Output






Previous Home Next