Struts Tutorial Home

Struts Home

JSP Resources

Community

Site



web.xml file


 



The mapping for the ActionServlet class is written in the web.xml file so we have to change it for mapping our CustomActionServlet class.


 


<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
<servlet>
    <servlet-name>
action
</servlet-name>
    <servlet-class>
com.visualbuilder.CustomActionServlet
</servlet-class>
    <init-param>
        <param-name>
config
</param-name>
        <param-value>
/WEB-INF/struts-config.xml
</param-value>
    </init-param>
    <load-on-startup>
2
</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>
action
</servlet-name>
    <url-pattern>
*.do
</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>


 


(3)CustomActionServlet.java file


 




This is the file which is the replaces the Struts Controller class. We extends the Struts ActionServlet to get the default behaviour and override the process method.


 


 


package com.visualbuilder;


 


import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.ActionServlet;


 


public class CustomActionServlet extends ActionServlet {


    private static final long serialVersionUID = 1L;


    public void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //TODO You can put the Code before the request handling.
                System.out.println("Before Handling Request");
        super.process(request,response);
        //TODO You can put the Code after the request handling.
                System.out.println("After Handling Request");
        }
}


(4)InputForm.java file


 


package com.visualbuilder;


 


import org.apache.struts.validator.ValidatorForm;


 


public class InputForm extends ValidatorForm {


 


String name;


String email;


String phone;


 


public String getEmail() {
    return email;
    }


public void setEmail(String email) {
    this.email = email;
    }


public String getName() {
    return name;
    }


public void setName(String name) {
    this.name = name;
    }


public String getPhone() {
    return phone;
    }


public void setPhone(String phone) {
    this.phone = phone;
    }
}




                    

Copyright © 2012 VisualBuilder. All rights reserved