How to run Struts

Document Sample
How to run Struts Powered By Docstoc
					How to run Struts

1.create web project in eclipse(click File -> New -> Project -> Web Project)

2.Right click the project and click myeclipse -> Add Struts Capabilities -> set the base
package for struts

For eg : com.senthil.struts
Click Finish.

3.create Action and Bean folder under com.senthil folder

For eg: com -> senthil ->Action
        com -> senthil -> Bean

4.create beanfile under Bean folder and testmybean under action folder

For eg: Bean -> beanfile.java
        Action -> testmybean.java



Beanfile.java
package com.senthil.Bean;

import org.apache.struts.action.ActionForm;

public class beanfile extends ActionForm{

       public String uname="";
       public String pass="";
       public String getPass() {
             return pass;
       }
       public void setPass(String pass) {
             this.pass = pass;
       }
       public String getUname() {
             return uname;
       }
       public void setUname(String uname) {
             this.uname = uname;
       }

}
Testmybean.java
package com.senthil.Action;

import java.io.IOException;

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.ActionForward;
import       org.apache.struts.action.ActionMapping;

import com.senthil.Bean.beanfile;


public class testmybean extends Action{

      public ActionForward execute(ActionMapping
actionmapping,ActionForm actionform,HttpServletRequest
request,HttpServletResponse response)
      {

                  beanfile ob=(beanfile)actionform;

                  String uname=ob.uname;
                  String pass=ob.pass;

                  System.out.println ("username--------->"+uname);
                  System.out.println("password---------->"+pass);

                  try {

         response.sendRedirect("welcome.jsp?un="+uname+"&ps="+pass);
               } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
               }



                  return null;


         }

}
5. set the form-bean and action mapping in struts-config.xml

For eg:

<form-beans >

  <form-bean name="senthil1" type="com.senthil.Bean.beanfile" ></form-
bean>
  </form-beans>

<action-mappings >
  <action name="senthil1" path="/gotoconfig"
type="com.senthil.test.testmybean">
<forward name="Success" path="/welcome.jsp"></forward>
  </action-mappings>




How to Deploy tomcat server for the first time

1.click Deploy myeclipse j2ee project to server
2.Click add -> Edit server connectors
3.click myeclipse -> Application Servers -> Tomcat 5 -> enable
4.set the Tomcat home directory

 For eg : D:\Program Files\Apache Software Foundation\Tomcat 5.0

5. set the jdk path
   click myeclipse -> Application Servers -> Tomcat 5 -> jdk -> Add and
set the jre home directory

for eg: D:\Program Files\Java\jdk1.5.0

6. Click ok.

Index.jsp

<%@ page contentType="text/html; charset=iso-8859-1" language="java"
import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
<title>Untitled Document</title>
</head>

<body>
<form name="senthil1" action="gotoconfig.do" method="post">
<table width="100%%" border="0">
  <tr>
    <td width="50%" align="right"> Username</td>
    <td width="50%"><input name="uname" type="text" /></td>
  </tr>
  <tr>
    <td align="right"> Password</td>
    <td><input name="pass" type="password" /></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input type="submit" /></td>

  </tr>
</table>
</form>
</body>
</html>



Welcome.jsp


<%@ page contentType="text/html; charset=iso-8859-1" language="java"
import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
<title>Untitled Document</title>
<script type="text/javascript">
<%
String user=request.getParameter("uname");
String password=request.getParameter("pass");
%>
</script>



</head>

<body>

<label>WelCOME LOgin Success </label>
<table>
<tr>
<td>User<%=user %></td>
<td>Password<%=password %></td>
</tr>
</table>
</body>
</html>

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:5/15/2012
language:
pages:4