The document discusses setting up a development environment for Java web applications using frameworks like Struts, including installing Java, Tomcat, and Struts. It also provides an overview of developing a basic "Hello World" Struts application, including creating an action class and configuring it in the struts.xml file to return the view page. The document includes code samples for configuring filters in web.xml and implementing a basic action class and JSP view page.
6. MVC (Model-View-Controller) Architecture MODEL … how the underlying data is structured (Enterprise Data + Business Rules) VIEW … presentation to the user/client (User Interface Element) CONTROLLER … receives input from user and take appropriate action … element that performs the processing … manage the request of the web browser
7.
8.
9.
10. Programmers develop web applications and implement their SQL Queries with the business logics directly in the HTML document. <html><head><title>Important title</title></head><body> <someScript> dbConnection = openDBConnection(someDB) resultSet = dbConnection.executeQuery('select bookName from books') loop over resultSet{ print (resultSet.field('bookName') + '<br>') } </someScript> </body></html> Convenient for small projects only For large scale projects… need of separation
11. <html:form action="someAction"> Please give your name and the book title <br> Name: <html:text property="name"/> <br> Title: <html:text property="title"/> <br> <html:submit/> </html:form> Struts support resources files. In this files you can define a key for a text. bookDialog.formIntro=Please give your name and the title of the book. bookDialog.inputName=Name: bookDialog.bookTitle=Title: This key can be used to diplay the text in the JSP file. <html:form action="someAction"> <bean:message key="bookDialog.formIntro"/> <br> <bean:message key="bookDialog.inputName"/> <html:text property="name"/> <br> <bean:message key="bookDialog.bookTitle"/> <html:text property="title"/> <br> <html:submit/> </html:form>
12. <html:form action="someAction"> Please give your name and the book title <br> Name: <html:text property="name"/> <br> Title: <html:text property="title"/> <br> <html:submit/> </html:form> Struts support resources files. In this files you can define a key for a text. bookDialog.formIntro=Please give your name and the title of the book. bookDialog.inputName=Name: bookDialog.bookTitle=Title: This key can be used to diplay the text in the JSP file. <html:form action="someAction"> <bean:message key="bookDialog.formIntro"/> <br> <bean:message key="bookDialog.inputName"/> <html:text property="name"/> <br> <bean:message key="bookDialog.bookTitle"/> <html:text property="title"/> <br> <html:submit/> </html:form>
13. Setting Up Development Environment Installing JDK: Download JDK latest. Follow the instruction given in the installation manual and install JDK. Installing Tomcat: Download Tomcat from the apache site and install it. To test your installation go to your installation directory/bin and issue startup command to run the server. Open browser and type http://localhost:8080/ to test the Server . It should display the welcome page.
14. Installing Struts Application Download latest version of Struts from the official site of Struts http:// jakarta.apache.org /struts . Extract the file into your favorite directory and copy struts-blank.war , struts-documentation.war and struts-example.war from " jakarta-struts-1.1ebapps " directory into " jakarta-tomcat-5.0.4ebapps " directory. struts-blank.war is the blank struts application which is useful in creating struts application from scratch. Use this file to create new web application. struts-documentation.war contains API and important documents for the struts application development. struts-example.war is simple MailReader Demonstration Application.
17. Developing First Struts Application … Rename struts-blank.war to struts-tutorial.war from jakarta-tomcat-5.0.4ebapps and copy it to the "jakarta-tomcat-5.0.4ebapps" directory. Tomcat automatically extracts the file and loads the application.
18. Extracting the content Now you can use the winzip to extract the content of struts-2.1.8.1-all.zip file. After extracting the file you will get following files/Directories:
19. Extracting the content The apps directory contains: a) struts2-blank-2.1.8.1.war - Blank Struts 2.1.8 application, can use used to start the blank application b) struts2-mailreader-2.1.8.1.war - Struts 2.1.8 mail reader application demo c) struts2-portlet-2.1.8.1.war - Struts 2.1.8 portlet application d) struts2-rest-showcase-2.1.8.1.war - Example to use the REST (Representational State Transfer) services e) struts2-showcase-2.1.8.1.war - Struts 2.1.8 showcase applications. Other directories are docs, lib and src.
21. Then run the tomcat and type http://localhost:8080/struts2-blank-2.1.8.1 in the browser . It should display the "Struts is up and running ..." message on the browser.
22. Struts Showcase Example Copy the struts2-showcase-2.1.8.1.war file into webapps directory of Tomcat and then start the Tomcat.
23. Struts Showcase Example … Now open the browser and type http://localhost:8080/struts2-showcase-2.1.8.1/ . Your browser should display the showcase application's home page.
26. Developing a Test Application - Display Message Web configuration file web.xml … To configure the Struts filter <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> … then configure the filter mapping for struts2 filter: <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
27. FILTERS FOR SECURITY In the web.xml file, you can specify the filter name and class, and initialization parameters. Then, associate the filter with a URL pattern in a filter mapping. <filter> <filter-name>adminAccessFilter</filter-name> <filter-class> com.jamesholmes.minihr.security.AuthorizationFilter </filter-class> <init-param> <param-name>roles</param-name> <param-value>administrator</param-value> </init-param> <init-param> <param-name>onError</param-name> <param-value>/index.jsp</param-value> </init-param> </filter> <filter-mapping> <filter-name>adminAccessFilter</filter-name> <url-pattern>/admin/*</url-pattern> </filter-mapping> The filter provides a basic security mechanism for a firewall to determining what traffic passes through the firewall based on IP address details. Example – To restrict IP Addresses
28. public class IPFilterExample implements Filter{ public IPFilterExample() {} public final static String IP = "193.168.10.146"; private FilterConfig filterConfig; public void init(FilterConfig config) throws ServletException{ this .filterConfig = config; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><head><title>IP Filter Example</title></head>"); String userip = request.getRemoteAddr(); HttpServletResponse httpResponse = null ; if (response instanceof HttpServletResponse){ httpResponse = (HttpServletResponse) response; } if (IP.equals(userip)) { httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN,"You are not allowed to access the servlet!"); } else { filterchain.doFilter(request, response); out.println("<body><h3><font color='green'>Passed successfully from IP Filter<font></h3></body></html>"); } } public void destroy() {} } IP FILTER
30. WRITING THE ACTION CLASS - HelloWorld.java import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extends ActionSupport { public String execute() throws Exception { setMessage(getText(MESSAGE)); return SUCCESS; } public static final String MESSAGE = "HelloWorld.message"; private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
31. HelloWorld.java The class extends com.opensymphony.xwork2.ActionSupport class. It provides a method getText(), to retrieve the message for the current local from the message resource file. The Struts framework executes the following default method : public String execute() throws Exception { setMessage(getText(MESSAGE)); return SUCCESS; } This method is just setting the message and the returning the SUCCESS value, which is the result of the action.
32. HelloWorld.java in Struts In this example HelloWorld.jsp is rendered and displayed to the user. Configuration of the HelloWorld action defined in struts.xml file: <action name="HelloWorld" class="net.roseindia.HelloWorld"> <result>/example/HelloWorld.jsp</result> </action>
33. View of Application The HelloWorld.jsp file is view in the Hello World example. Here is the code of HelloWorld.jsp file: <%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title><s:text name="HelloWorld.message"/></title> </head> <body> <h2><s:property value="message"/></h2> <br> <a href="/struts2/">Back to Index page</a> </body> </html> <s:property value="message"/> is used to display the message from the action class and <s:text name="HelloWorld.message"/> is used to display the message from the properties file.
34. Struts File Upload Example StrutsUploadForm.java): package roseindia.net; import org.apache.struts.action.*; import org.apache.struts.upload.FormFile; public class StrutsUploadForm extends ActionForm { private FormFile theFile; public FormFile getTheFile() { return theFile; } public void setTheFile(FormFile theFile) { this .theFile = theFile; } }
36. Defining form Bean in struts-config.xml file Add the following entry in the struts-config.xml file for defining the form bean: <form-bean name="FileUpload" type=“mysiute.net.StrutsUploadForm"/> Defining Action Mapping Add the following action mapping entry in the struts-config.xml file: <action path="/FileUpload" type=“mysite.net.StrutsUploadAction" name="FileUpload" scope="request" validate="true" input="/pages/FileUpload.jsp"> <forward name="success" path="/pages/uploadsuccess.jsp"/> </action>
38. uploadsuccess.jsp file <html> <head> <title>Success</title> </head> <body> <p align="center"><font size="5" color="#000080">File Successfully Received</font></p> </body> </html> Add the following line in the index.jsp to call the form <li> <html:link page="/pages/FileUpload.jsp">Struts File Upload</html:link> <br> Example shows you how to Upload File with Struts. </li>
39.
40. Understanding ActionClass … in the struts application extends Struts 'org.apache.struts.action.Action" Class … acts as wrapper around the business logic and provides an inteface to the application's Model layer. … acts as glue between the View and Model layer … transfers the data from the view layer to the specific business process layer and finally returns the processed data from business layer to the view layer
41. Understanding ActionClass … testAction.java 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 TestAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{ return mapping.findForward("testAction"); } } Action Class process the specified HTTP request, create the corresponding HTTP response (or forward to another web component that will create it), with provision for handling exceptions thrown by the business logic.