Previous | Home | Next |
Here we will discuss about the how we can create a web application using struts2 without database. For creating a web application using struts we need to follow some basic step which is given below:
Step 1. Take a project by going in File Menu Wizard -> New Project -> click on Java Web and then web application next -> put the name of the project on the required place and next -> select server on which we want to build the project and then next -> now select to struts2 (if not available then select to struts and attatch the jar file of the struts2) -> click on finish
Step 2. Now Design The Jsp Page. For Creating A Jsp Page Go In File Menu Wizard -> Click On New File -> Click On Web And Then Jsp -> Put The Required Name On The Given Field For The Name -> Finish.
<%@taglib prefix="s" uri="/struts-tags"%> <head> <title><s:text name="StrutsApplicationWithoutDatabase"/></title> </head> <body> <s:form action="validate" name="fom"> <s:textfield label="First Name" name="first_name"/> <s:password label="Password" name="pass"/> <s:password label="Re-Password" name="rpass"/> <s:submit value="Insert"/> </s:form> </body>
Step 3. Now again we need to create a jsp page. follow the above step instruction.
<%@page import="r4r.UserLogin" %> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Success</title> </head> <body> Welcome <%=request.getAttribute("u")%> </body> </html>
Step 4. Now again we need to create a jsp page. follow the above step(Step no 2) instruction.
<H1>This is an error page......</H1>
Step 5. Now we need to create a package. for this we need to go in the file menu wizard then click on new file -> java and then package as shown in image then next -> put the required name of the package on the place and then finish.
Step 6. Now we need to create a java class. for this we need to go in the file menu wizard -> click on new file and -> java and then click on java class -> put the required name on the place of name of the file -> select the package and then finish.
package r4r; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; public class UserLogin { private String first_name; private String pass; private String rpass; public String exec() { if(getPass().equals(getRpass())) { HttpServletRequest req = ServletActionContext.getRequest(); req.setAttribute("u", getFirst_name()); return "S"; } else return "F"; } public String getFirst_name() { return first_name; } public void setFirst_name(String first_name) { this.first_name = first_name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public String getRpass() { return rpass; } public void setRpass(String rpass) { this.rpass = rpass; } }
Step 7. Now we set the action into the struts xml file which create in the struts2 by default in the default package or if we don't have the struts 2 then we need to add this.
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- Configuration for the default package. --> <package name="default" extends="struts-default"> <action name="validate" class="r4r.UserLogin" method="exec" > <result name="S">/Sucess.jsp</result> <result name="F">/error.jsp</result> </action> </package> </struts>
Step 8. Now we need to do the build and run the project. for this we go in the file menu wizard -> click on run menu button and -> then build and then run.
Step 9. Put the required value in the jsp page field in the browser.
Step 10. If we make the wrong value then we go in the error page which is shown in the image that is below.
Previous | Home | Next |