Requirements:
Step_1: Open NetBeans IDE. Step_2:
Go File and select New Project.
New Project window will open
Under Categories
You Select web
Under projects
You Select web application
You select Next. New web Application window will open
Here you give project name (I have used project name (StrutsExam)
And Click Next . Another New web Application window will open
Here you select Struts 1.2.9. Struts 1.2.9 configuration will enable. And select Add Struts TLDs checkbox.
Select Finish.
Now you Successfully Project created.
Step_3:
Create Folder for
1). actionform
2). action
Right Click on source package. New and Folder
Here you give Folder Name. actionforms. And select Finish.
Now you have created actionforms folder successfully.
And
Step_4:
Create actionform
Right click actionforms and select New and other.
New File window will open.
Under categories
Select struts
Under File types
click Next.
New window will open. New Struts ActionForm Bean
You give class name (I have used loginforms)
Click Finish. Now you have created actionform successfully.
Note: You may delete all content between { and }
you insert below code between { and }.
private String username;
private String password;
you select both statement.
And right click and go insert code. And select Getter and setter..
Generate getter and setter window will open. Tick both. And select Generate. Automatically getter and setter for username and password are generated.
package actionforms;
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 loginforms extends org.apache.struts.action.ActionForm
{
private String username;
private String password;
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
}
Step_5:
Create action class create.
Right click actions and New select other.
New File window will open.
package actions;
import actionforms.loginforms;
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.ActionMapping;
import org.apache.struts.action.ActionForward;
public class loginaction extends org.apache.struts.action.Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
loginforms sform=(loginforms)form;
if((sform.getUsername().equals("suresh"))&&(sform.getPassword().equals("kumar")))
{
return mapping.findForward("success");
}
else
{
return mapping.findForward("failure");
}
}
}
Step_6:
Configure Struts-config.xml
Open web pages.and WEB-INF.
Open struts-config.xml.
< action-mappings >
< action
input="/"
name="loginforms"
path="/login"
scope="session"
type="actions.loginaction" >
< forward name="success" path="/jsp/success.jsp"/ >
< forward name="failure" path="/jsp/failure.jsp"/ >
< /action >
You have inserted insert forward tag.
Step_7:
Create jsp file.
Create Folder for jsp. Already I have explained How to create Folder.
Right click jsp.
New. Other. New File window will open.
Under categories
Select web
Under File types
Select JSP
click Next.
New JSP File window will open.
You give jsp file name( I have used Home)
Finish.
Jsp file have been created.
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< html >
< head > < title > JSP Page < /title > < /head >
< body >
< /body >
< /html >
and you have to made some change.
You insert below code.in jsp file.
< %@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >
You insert below code between < body > and < / body > tag.
< html:form action="/login" >
username: < html:text property="username"/ > < br >
password: < html:password property="password"/ > < br >
< html:submit value="login"/ >
< /html:form >
Finally you get jsp file
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< %@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >
< html >
< head > < title > JSP Page < /title > < /head >
< body >
< html:form action="/login" >
username: < html:text property="username"/ > < br >
password: < html:password property="password"/> < br >
< html:submit value="login"/ >
< /html:form >
< /body >
< /html >
Step_8:
Create success’s file
Create failure’s file
Already I have explained how to create jsp file.
You have to create success.jsp and failure.jsp
Step_9:
Run home.jsp file.
Right click on home.jsp and run file.
And browser will open.
You give username and password.
If username is suresh and password is kumar.
The success.jsp file will open.
Otherwise failure.jsp file will open.
Conclusion:
Now you have known how to create struts project and how to create actionforms and actions and jsp files and how to configure struts-config.xml and how to run. Now you are struts developer.
No comments:
Post a Comment