Your SlideShare is downloading. ×
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply



Published on

Published in: Education

1 Comment
No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1.  
  • 2.
    • Set of reusable software program that forms the basis for an application.
    • Standard Libraries to build the application quickly
    Rapid Application Development Frameworks
  • 3. Rapid Application Development Frameworks
    • Reduces Time and Energy
    • Provides a standard system to develop the project modules quickly
    • Software Requirement Specifications Oriented
    • Framework controls the flow of project module while in Libraries, programmer controls the flow of program.
    • Extensibility
  • 4. Architecture of Frameworks
    • Frozen Spots
    • … defines the overall architecture of a software system
    • Hot Spots (Virtual Name)
    • Represent those parts where the programmers add their own code to add the functionality specific to their own projects using framework.
    • Developers utilize the hot-spots as per the requirement of project
    • In Object Oriented Environment, framework consists of abstract and concrete classes
  • 5. Top Open Source Web Frameworks in Java
  • 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. MVC-1 Vs. MVC-2
    • Features of MVC-1:
    • HTML or JSP files are used to code the presentation.
    • To retrieve the data JavaBean can be used.
    • All the view, control elements are implemented using SERVLETS or JSP.
    • Tight Coupling between page and model as data access is usually done using Custom Tag or through Java Bean call.
    • Features of MVC-2:
    • Removes the page centric property of MVC1 architecture by separating Presentation, control logic and the application state.
    • There is only one controller which receives all the request for the application and is responsible for taking appropriate action in response to each request.
  • 8.
    • Open Source Framework for J2EE Web Applications
    • Follows MVC Design Pattern
    • Uses and extends Java Servlet API
    • Cross-platform
    • A strong set of custom tag libraries
    • Simplify JSP development
    • Stable and Reliable
    • XML/XSLT and Velocity supported
  • 9.
      • Java Programs that run on a Web Server
      • Reads and process data typed in HTML form on web page
      • Generates results with the existing data
      • Calls the business logic
      • Returns the document
    • JSP - Java Server Pages
      • HTML pages with Java Code
      • Blend of Static and Dynamic Content
    • Java Beans
      • Nothing else as Classes with fixed naming conventions of Sun for their event processing
      • Attributes (Variables) of Java Beans are private and managed by the access methods
  • 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=&quot;someAction&quot;> Please give your name and the book title <br> Name: <html:text property=&quot;name&quot;/> <br> Title: <html:text property=&quot;title&quot;/> <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=&quot;someAction&quot;> <bean:message key=&quot;bookDialog.formIntro&quot;/> <br> <bean:message key=&quot;bookDialog.inputName&quot;/> <html:text property=&quot;name&quot;/> <br> <bean:message key=&quot;bookDialog.bookTitle&quot;/> <html:text property=&quot;title&quot;/> <br> <html:submit/> </html:form>
  • 12. <html:form action=&quot;someAction&quot;> Please give your name and the book title <br> Name: <html:text property=&quot;name&quot;/> <br> Title: <html:text property=&quot;title&quot;/> <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=&quot;someAction&quot;> <bean:message key=&quot;bookDialog.formIntro&quot;/> <br> <bean:message key=&quot;bookDialog.inputName&quot;/> <html:text property=&quot;name&quot;/> <br> <bean:message key=&quot;bookDialog.bookTitle&quot;/> <html:text property=&quot;title&quot;/> <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:// /struts . Extract the file into your favorite directory and copy struts-blank.war , struts-documentation.war and struts-example.war from &quot; jakarta-struts-1.1webapps &quot; directory into &quot; jakarta-tomcat-5.0.4webapps &quot; 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.
  • 15. MyApplication          |     ----src     // Java Source Code folder ----WEB-INF //within folder               |     ---------web.xml //file    ---------struts-config.xml //file    ---------struts-bean.tld //file    ---------struts-html.tld //file    ---------struts-logic.tld //file    ---------struts-nested.tld //file    ---------struts-template.tld //file    ---------struts-tiles.tld //file    ---------classes //folder    ---------lib //within folder   
  • 16.
    • Alternate Java Web Servers
      • JBoss Caucho Resin
      • Rimfaxe Miniature Java Web Server
      • JRun Asynweb
      • WebSphere Reattore
      • Tonado Jetty
      • Jigsaw Rupy
      • Pygmy NanoHTTPD
  • 17. Developing First Struts Application … Rename struts-blank.war  to struts-tutorial.war from jakarta-tomcat-5.0.4webapps and copy it to the &quot;jakarta-tomcat-5.0.4webapps&quot; 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 file. After extracting the file you will get following files/Directories:
  • 19. Extracting the content The apps directory contains: a) struts2-blank- - Blank Struts 2.1.8 application, can use used to start the blank application b) struts2-mailreader- - Struts 2.1.8 mail reader application demo c) struts2-portlet- - Struts 2.1.8 portlet application d) struts2-rest-showcase- - Example to use the REST (Representational State Transfer) services e) struts2-showcase- - Struts 2.1.8 showcase applications. Other directories are docs, lib and src.
  • 20. Set-up
  • 21. Then run the tomcat and type http://localhost:8080/struts2-blank- in the browser . It should display the &quot;Struts is up and running ...&quot; message on the browser.
  • 22. Struts Showcase Example Copy the struts2-showcase- 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- . Your browser should display the showcase application's home page.
  • 24. Struts Showcase Example … The showcase application provides following examples: Ajax Ajax Chat Action Chaining Config Browser Conversion CRUD Execute & Wait File Download File Upload FreemarkerHangman JavaServer  Faces Tags Tiles Token Validation Interactive Demo Person Manager Struts 1 Integration
  • 25. Screenshot of the Chat application
  • 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> </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>    </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 = &quot;;;    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(&quot;text/html&quot;);     PrintWriter out = response.getWriter();     out.println(&quot;<html><head><title>IP Filter Example</title></head>&quot;);     String userip = request.getRemoteAddr();     HttpServletResponse httpResponse =  null ;      if  (response  instanceof  HttpServletResponse){       httpResponse = (HttpServletResponse) response;     }      if  (IP.equals(userip)) {       httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN,&quot;You are not         allowed to access the servlet!&quot;);           }  else  {       filterchain.doFilter(request, response);       out.println(&quot;<body><h3><font color='green'>Passed successfully from IP       Filter<font></h3></body></html>&quot;);     }   }    public void  destroy() {} } IP FILTER
  • 29. MOVING BACK TO OUR TEST APPLICATION - WEB.XML file <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <web-app id=&quot;WebApp_9&quot; version=&quot;2.4&quot; xmlns=&quot;; xmlns:xsi=&quot;; xsi:schemaLocation=&quot;;> <display-name>My Application Struts 2.8.1 Tutorials</display-name> <filter> <filter-name>struts2</filter-name> <filter-class></filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
  • 30. WRITING THE ACTION CLASS - 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 = &quot;HelloWorld.message&quot;;     private String message;     public String getMessage() {         return message;     }     public void setMessage(String message) {         this.message = message;     } }
  • 31. 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. 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=&quot;HelloWorld&quot; class=&quot;net.roseindia.HelloWorld&quot;>      <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=&quot;text/html; charset=UTF-8&quot; %> <%@ taglib prefix=&quot;s&quot; uri=&quot;/struts-tags&quot; %> <html> <head>     <title><s:text name=&quot;HelloWorld.message&quot;/></title> </head> <body>    <h2><s:property value=&quot;message&quot;/></h2> <br> <a href=&quot;/struts2/&quot;>Back to Index page</a> </body> </html> <s:property value=&quot;message&quot;/> is used to display the message from the action class and <s:text name=&quot;HelloWorld.message&quot;/>  is used to display the message from the properties file.
  • 34. Struts File Upload Example package; 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;   } }
  • 35. 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 org.apache.struts.upload.FormFile; public class StrutsUploadAction extends Action {   public ActionForward execute(     ActionMapping mapping,     ActionForm form,     HttpServletRequest request,     HttpServletResponse response) throws Exception{     StrutsUploadForm myForm = (StrutsUploadForm)form;         FormFile myFile = myForm.getTheFile();         String contentType = myFile.getContentType();         String fileName    = myFile.getFileName();         int fileSize       = myFile.getFileSize();         byte[] fileData    = myFile.getFileData();     System.out.println(&quot;contentType: &quot; + contentType);     System.out.println(&quot;File Name: &quot; + fileName);     System.out.println(&quot;File Size: &quot; + fileSize);         return mapping.findForward(&quot;success&quot;);  } } Creating Action Class
  • 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=&quot;FileUpload&quot;     type=“;/> Defining Action Mapping Add the following action mapping entry in the struts-config.xml file: <action      path=&quot;/FileUpload&quot;      type=“;      name=&quot;FileUpload&quot;      scope=&quot;request&quot;      validate=&quot;true&quot;      input=&quot;/pages/FileUpload.jsp&quot;>      <forward name=&quot;success&quot; path=&quot;/pages/uploadsuccess.jsp&quot;/> </action>
  • 37. Developing jsp page - FileUpload.jsp <%@ taglib uri=&quot;/tags/struts-bean&quot; prefix=&quot;bean&quot; %> <%@ taglib uri=&quot;/tags/struts-html&quot; prefix=&quot;html&quot; %> <html:html locale=&quot;true&quot;> <head> <title>Struts File Upload Example</title> <html:base/> </head> <body bgcolor=&quot;white&quot;> <html:form action=&quot;/FileUpload&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;> <table><tr> <td align=&quot;center&quot; colspan=&quot;2&quot;> <font size=&quot;4&quot;>Please Enter the Following Details</font> <td align=&quot;left&quot; colspan=&quot;2&quot;> <font color=&quot;red&quot;><html:errors/></font> <td align=&quot;right&quot;> File Name </td> <td align=&quot;left&quot;> <html:file property=&quot;theFile&quot;/>  <tr> <td align=&quot;center&quot; colspan=&quot;2&quot;> <html:submit>Upload File</html:submit> </table> </html:form> </body> </html:html>
  • 38. uploadsuccess.jsp file <html> <head> <title>Success</title> </head> <body> <p align=&quot;center&quot;><font size=&quot;5&quot; color=&quot;#000080&quot;>File Successfully Received</font></p> </body> </html> Add the following line in the index.jsp to call the form <li> <html:link page=&quot;/pages/FileUpload.jsp&quot;>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&quot; 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 … 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(&quot;testAction&quot;);   } } 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.
  • 42.
    • Parameters of ActionClass
      • mapping - The ActionMapping used to select this instance
      • form - The optional ActionForm bean for this request (if any)
      • request - The HTTP request we are processing
      • response - The HTTP response we are creating
    • Throws:
      • Action class throws java.lang.Exception - if the application business logic throws an exception
    • Adding the Action Mapping in the struts-config.xml To test the application we will add a link in the index.jsp  <html:link page=&quot;/;>Test the Action</html:link>
    • Following code under the <action-mappings> tag is used to for mapping the TestAction class.
    • <action path=&quot;/TestAction&quot; type=“;>
    • <forward name=&quot;testAction&quot; path=&quot;/pages/TestAction.jsp&quot;/> </action>
  • 43.