0% found this document useful (0 votes)
21 views

Ex No - 11

Uploaded by

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

Ex No - 11

Uploaded by

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

Ex no : 11Create a program using struts.

Aim : To create registration program using struts

Program and procedure:

1. Create New Maven Project:

Go to File > New > Other….

Select Maven > Maven Project and click Next.

Choose an appropriate workspace location and click Next.

2. Select Archetype:

Choose maven-archetype-quickstart or skip this step to create a simple project. Click Next.

3. Enter Project Details:


Group Id: com.example

Artifact Id: registration-app

Version: 1.0-SNAPSHOT

Click Finish.

Step 3: Update pom.xml for Struts Dependencies

1. Open the pom.xml file in the root of your project.

2. Add the Struts dependencies inside the <dependencies> tag:

Program for dependency

<dependencies>

<dependency>

<groupId>org.apache.struts</groupId>

<artifactId>struts2-core</artifactId>
<version>2.5.20</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>3.1.0</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet.jsp</groupId>

<artifactId>javax.servlet.jsp-api</artifactId>

<version>2.3.3</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet.jsp.jstl</groupId>

<artifactId>javax.servlet.jsp.jstl-api</artifactId>

<version>1.2.1</version>

<scope>provided</scope>

</dependency>

</dependencies>

3. Save the pom.xml file and right-click on your project, then select Maven > Update Project to
download the necessary dependencies.
Step 4: Create the Registration Form

1. Create the Web Content Directory:

Right-click on the project in Eclipse and select New > Folder. Name it src/main/webapp.

2. Create a JSP file for the registration form:

Right-click on the src/main/webapp folder, select New > File, and name it registration.jsp.

Program

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

<html>

<head>

<title>User Registration</title>

</head>

<body>

<h2>User Registration</h2>

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

<s:textfield name="username" label="Username" required="true" />


<s:textfield name="password" label="Password" required="true" />

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

<s:submit value="Register" />

</s:form>

</body>

</html>

Step 5: Create the Action Class

1. Create a Java package for actions:

Right-click on src/main/java, select New > Package, and name it com.example.action.

2. Create the action class:

Right-click on the com.example.action package, select New > Class, and name it RegistrationAction.

Program

package com.example.action;

import com.opensymphony.xwork2.ActionSupport;
public class RegistrationAction extends ActionSupport {

private String username;

private String password;

private String email;

// Getters and Setters

public String getUsername() {

return username;

public void setUsername(String username) {

this.username = username;

public String getPassword() {

return password;

public void setPassword(String password) {

this.password = password;

public String getEmail() {

return email;
}

public void setEmail(String email) {

this.email = email;

@Override

public String execute() {

// Here you would typically save the user information to a database

System.out.println("User registered: " + username + ", " + email);

return SUCCESS;

Step 6: Configure Struts

1. Create struts.xml:

Right-click on src/main/resources, select New > File, and name it struts.xml.

Program

<!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="registerUser" class="com.example.action.RegistrationAction">

<result name="success">/success.jsp</result>

<result name="input">/registration.jsp</result>

</action>

</package>

</struts>

Step 7: Create a Success Page

1. Create a success.jsp page:

Right-click on src/main/webapp, select New > File, and name it success.jsp.

Program

<html>

<head>

<title>Registration Successful</title>

</head>

<body>
<h2>Registration Successful!</h2>

<p>Thank you for registering.</p>

<a href="registration.jsp">Register Another User</a>

</body>

</html>

Step 8: Configure web.xml

1. Create a web.xml file:

Right-click on src/main/webapp/WEB-INF, select New > File, and name it web.xml.

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>
<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<welcome-file-list>

<welcome-file>registration.jsp</welcome-file>

</welcome-file-list>

</web-app>

Step 9: Run the Application

1. Create a Server:

In the Servers view in Eclipse, right-click and select New > Server.

Choose Apache Tomcat and click Next.

Select the Tomcat installation directory and click Finish.

2. Deploy the Application:


Right-click on your project, select Run As > Run on Server.

Choose the Tomcat server you just created.

3. Access the Application:

Open a web browser and go to http://localhost:8080/registration-app/registration.jsp.

result

thus the program created a simple user registration application

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy