Write A Login Action class

Write A Login Action class

Previous Home Next

 

When the request comes from client then ActionServet calls an Actions class which is subclass of org.apache.struts.action.Action  We have to override the execute() method.


 The execute() method returns type is ActionForward. The execute methods forwards a key.On the behalf of this key ActionServelet decides the next view and give response to client.The execute method takes four arguments:
  • ActionMapping
  • ActionForm
  • HttpServletRequest 
  • HttpServletResponse

Syntax of execute method :-


public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response){}

Four object is used in this method :-
  • mapping:- This is object of ActionMapping . ActionMapping contains all deployment information for a particular action. ActionMapping contains a group of ActionForward associated with the current action. ActionMapping class used to determine where the results of action class will be send after competitions of presses.
  • form :-This is object of ActionForm.This contains the parameters passed by input form.
  • request:-This is object of current HttpServletRequest.
  • response:-This is object of current HttpServletResponse. The return type of execute methods is ActionForword returned by Action call through which ActionServlet decided next View. This key is compared with values of tags name attributes to forwards the request to next View to client as response. We have also map configurations of action subclass into struts-config.xml. Below is example of Action Mappings Configuration Here success.jsp is JSP page where request is forwarded when action will successfully completed.
LoginAction.java


package com.r4r.struts;

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;

public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response){ LoginForm loginForm=(LoginForm)form; if(loginForm.getName().equals("Mukund")
&&loginForm.getPassword().equals(
"Singh")){ return mapping.findForward("success"); }else{ return mapping.findForward("error"); } } }



Previous Home Next