0% found this document useful (0 votes)
16 views5 pages

Assignment 8

The document outlines the design and implementation of a login page using Struts framework, including validations for name, mobile number, and email ID. It details the project structure, necessary dependencies, Struts configuration, action class creation, and JSP pages for login, success, and error handling. Additionally, it provides steps to run the application on an Apache Tomcat server and expected output for both successful and erroneous submissions.

Uploaded by

shreyadeore2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Assignment 8

The document outlines the design and implementation of a login page using Struts framework, including validations for name, mobile number, and email ID. It details the project structure, necessary dependencies, Struts configuration, action class creation, and JSP pages for login, success, and error handling. Additionally, it provides steps to run the application on an Apache Tomcat server and expected output for both successful and erroneous submissions.

Uploaded by

shreyadeore2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

8.

Design a login page with entries for name, mobile number email id and login
button. Use struts and perform following validations
a. Validation for correct names
b. Validation for mobile numbers
c. Validation for email id
d. Validation if no entered any value
e. Re-display for wrongly entered values with message
f. Congratulations and welcome page upon successful entries

Step 1: Project Structure

StrutsLoginApp/
│── src/
│ ├── com.example.action/
│ │ ├── LoginAction.java
│ ├── struts.xml
│── WebContent/
│ ├── index.jsp (Login Page)
│ ├── welcome.jsp (Welcome Page)
│ ├── error.jsp (Error Page)
│ ├── WEB-INF/
│ │ ├── web.xml
│ │ ├── struts-config.xml

Step 2: Add Dependencies (struts2-core.jar, commons-


validator.jar)
Ensure you have Struts 2 JARs in the lib folder of your project.

Step 3: Struts Configuration (struts.xml)

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


2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
<package name="default" extends="struts-default">

<action name="login" class="com.example.action.LoginAction">


<result name="success">/welcome.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
</struts>

Step 4: Create the Action Class (LoginAction.java)


package com.example.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {


private String name;
private String mobile;
private String email;

public String execute() {


return SUCCESS;
}

public void validate() {


if (name == null || name.trim().equals("")) {
addFieldError("name", "Name is required");
} else if (!name.matches("^[a-zA-Z\\s]+$")) {
addFieldError("name", "Name must contain only alphabets");
}

if (mobile == null || mobile.trim().equals("")) {


addFieldError("mobile", "Mobile number is required");
} else if (!mobile.matches("^[6-9]\\d{9}$")) {
addFieldError("mobile", "Invalid mobile number");
}

if (email == null || email.trim().equals("")) {


addFieldError("email", "Email ID is required");
} else if (!email.matches("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$"))
{
addFieldError("email", "Invalid email format");
}
}

public String getName() { return name; }


public void setName(String name) { this.name = name; }

public String getMobile() { return mobile; }


public void setMobile(String mobile) { this.mobile = mobile; }

public String getEmail() { return email; }


public void setEmail(String email) { this.email = email; }
}
Step 5: Login Page (index.jsp)
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Login Page</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; }
.error { color: red; }
</style>
</head>
<body>
<h2>Login Form</h2>

<s:form action="login" method="post">


<s:textfield name="name" label="Name" />
<s:fielderror fieldName="name" cssClass="error" />

<s:textfield name="mobile" label="Mobile Number" />


<s:fielderror fieldName="mobile" cssClass="error" />

<s:textfield name="email" label="Email ID" />


<s:fielderror fieldName="email" cssClass="error" />

<s:submit value="Login" />


</s:form>
</body>
</html>

Step 6: Success Page (welcome.jsp)

<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Congratulations! You have successfully logged in.</h2>
</body>
</html>

Step 7: Error Page (Optional, error.jsp)

<html>
<head>
<title>Error</title>
</head>
<body>
<h2 style="color:red;">Oops! Something went wrong.</h2>
<p>Please go back and check your entries.</p>
</body>
</html>

Step 8: Configure web.xml


<web-app 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_3_0.xsd"
version="3.0">

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Steps to Run
1. Install Apache Tomcat and configure your IDE (Eclipse/IntelliJ).
2. Place Struts 2 JAR files in the WEB-INF/lib directory.
3. Deploy the project in Tomcat's webapps folder.
4. Start the server and open in a browser:
http://localhost:8080/StrutsLoginApp/index.jsp
5. Enter:
o ✅ Name: John Doe ✅
o ✅ Mobile: 9876543210 ✅
o ✅ Email: [email protected]
o Click Login → Redirects to welcome.jsp
6. If wrong details are entered, errors are displayed on index.jsp.
Expected Output
Login Page (index.jsp)

+---------------------+
| Name: [__________] | (Error: Name must contain only alphabets)
| Mobile: [________] | (Error: Invalid mobile number)
| Email: [_________] | (Error: Invalid email format)
+---------------------+
[ Login ]

Success Page (welcome.jsp)


Congratulations! You have successfully logged in.

You might also like