JAVA

Core Java

 Instance Members: These are instance variables and instance methods of an
object. They can only be accessed or invoked through an object reference.

Instance Variable: A field that is allocated when the class is instantiated, i.e.,
when an object of the class is created. Also called non-static field.

Instance Method: A method that belongs to an instance of the class. Objects of
the same class share its implementation.

Static Members :These are static variables and static methods of a class. They
can be accessed or invoked either by using the class name or
through an object reference.

Static Variable: A field that is allocated when the class is loaded. It belongs
to the class and not to any specific object of the class. Also 
called static field or class variable.

Static Method: A method which belongs to the class and not to any object of
the class. Also called class method. following example explain concept.


Static members can also be accessed via object references, but this is considered
bad style:
CharStack stack1;
int count1 = stack1.getInstanceCount(); // Reference invokes static method
Static members in a class can be accessed both by the class name and via object references,
but instance members can only be accessed by object references. 





MY STRUTS 1.2 TUTORIAL

DispatchAction is one of the Struts built-in action that provides a mechanism that facilitates having a set of related functionality in a single action instead of creating separate independent actions for each function.
Let us create a sample example that uses DispatchAction and implement certain functionality of our project.
 
Step 1: Create DispatchAction class file.
Create an Action class called UserManagementAction and extend it with class org.apache.struts.actions.DispatchAction. Copy following code into it.
package net.viralpatel.struts.helloworld.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class UserManagementAction extends DispatchAction {
     
    public ActionForward create(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
     
        request.setAttribute("message", "User created successfully");
         
        return mapping.findForward("success");
    }



    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
     
        request.setAttribute("message", "User deleted successfully");
         
        return mapping.findForward("success");
    }



    public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
     
        request.setAttribute("message", "User updated successfully");
         
        return mapping.findForward("success");
    }



    public ActionForward block(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
     
        request.setAttribute("message", "User blocked successfully");
         
        return mapping.findForward("success");
    }
}
In above code, we have created separate methods (create(), delete(), update() and block()) for each functionality. Also note that the method signature of these methods are exactly similar to the execute() method of Action class file.
public ActionForward delete(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception { }
Step 2: Create a mapping for action in struts-config.xml file.
Open your struts-config.xml and copy following action mapping entry in it.
<action path="/user" parameter="parameter"
    type="net.viralpatel.struts.helloworld.action.UserManagementAction">
    <forward name="success" path="/UserSuccess.jsp" />
    <forward name="failure" path="/UserSuccess.jsp" />
</action>
We have added an extra attribute in <action> tag, parameter=”parameter”. DispatchAction will read a request parameter called “parameter” and its value will decide the method to be called. Suppose you have a request parameter “parameter” with value “create”, Dispatch Action will call create() method from your Action file.
Step 3: Create JSPs for viewing the application.
Create two JSP files UserManagement.jsp and UserSuccess.jsp and copy following code into it. UserManagement.jsp will display a menu for selecting action to be taken. UserSuccess.jsp will just print the appropriate action taken by the Action class.
UserManagement.jsp
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
    <title>Dispatch Action Example - viralpatel.net</title>
</head>
<body>
    <h2>User Management (Dispatch Action Example)</h2>
     
    <html:link href="user.do?parameter=create">Create User</html:link>
     |
    <html:link href="user.do?parameter=delete">Delete User</html:link>
     |
    <html:link href="user.do?parameter=update">Update User</html:link>
     |
    <html:link href="user.do?parameter=block">Block User</html:link>
     
</body>
</html>
UserSuccess.jsp
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
    <title>Dispatch Action Example - viralpatel.net</title>
</head>
<body>
    <h3>User Message (Dispatch Action Example)</h3>
    <center>
    <font color="blue"><h3><%= request.getAttribute("message") %></h3></font>
    <center>
</body>
</html>
In UserManagement.jsp, we have created links using <html:link> tag and passed a parameter “parameter” with values create, delete, block etc. This value is fetched by the Dispatch Action and is used to call corresponding method in Action class. Thus if we click Update User link, a parameter update will be passed to the action class and corresponding update() method will be called by framework.
Step 4: Run the application.
Compile and Run the project using Eclipse or Ant and open UserManagement.jsp file in your favorite browser.

No comments:

Post a Comment