Pages

Tuesday, May 29, 2012

Java Persistence API (JPA) Application

Java  Persistence API (JPA) provides POJO(Plan Old Java Object) standard and object relational mapping (ORM) for data persistence among applications.


JPA Architecture


Creating a simple JPA Application using MySql

Creating a database in MySql

  • Create a database StudentDetails in Mysql.
          create database StudentDetails;

Establishing a database connection to NetBeans

In NetBeans under the Services tab, Right click on the database and select New  Connection
  

Monday, May 21, 2012

Java Server Faces (JSF)


Java Server Faces (JSF) is a server side user interface component framework for Java technology based web applications.
JSF is a component oriented and event driven framework for web applications.

Create a simple JSF application in Maven

  • Create the maven web application via command prompt eg:- SimpleJSF  and open it in Net beans.

       The file layout of the project is some thing like this.


  • Insert the following dependencies to your pom.xml file to include the jsf2.0 libraries
       <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.0.0-b13</version>
            <scope>compile</scope>
       </dependency>


      <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.0.0-b13</version>
            <scope>compile</scope>
       </dependency>

  • Create a new JSF file (index.xhtml) in the project. (New---> JSF Page)


  • In web.xml include the following tags for servlet mapping and set the welcome page.

       <context-param>

           <param-name>javax.faces.PROJECT_STAGE</param-name>

           <param-value>Development</param-value>

       </context-param>

  

       <servlet>
          <servlet-name>Faces Servlet</servlet-name>
          <servlet-class>javax.faces.webapp.FacesServlet
          </servlet-class>
          <load-on-startup>1</load-on-startup>
       </servlet>

      <servlet-mapping>
           <servlet-name>Faces Servlet</servlet-name>
           <url-pattern>*.xhtml</url-pattern>
      </servlet-mapping>
  
     <welcome-file-list>
          <welcome-file>index.xhtml </welcome-file>
      </welcome-file-list>


  • After that create a managedbean (New-->JSFMAnagedBean-->studentManagedBean)

      In JSF 2.0, Java bean that can be accessed from JSF page is called   
      Managed Bean.


  
   The managed bean can be a normal Java bean, which contains the getter        
  and setter methods,business logic or even a backing bean.

  • After that you create the following classes



    The Structure of the application is like this:



      Here Student.java is the Java bean class.



  • If you want to insert the prime faces in your application. You should insert the following dependency and repository
       <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>3.2</version>
     </dependency>



    <repository>
            <id>prime-repo</id>
            <name>PrimeFaces Maven Repository</name>
            <url>http://repository.primefaces.org</url>
            <layout>default</layout>
    </repository>

Friday, May 4, 2012

MVC Architecture

MVC is an architectural design pattern. 
  • Model - business logic.
  • View - User Interface
  • Controller - handles user input
Why MVC ?
  • View is free of any business logic so looks simple and readable.
  • Business logic within the model can be reused for more than one views.
Simple example for MVC with JSP
  •  A web page for register the student details. 
  •  Here Student name, Parent name, Grade, and Address are input 
  • fields.
  •  User can enter these values by clicking "Insert"
  •  Here Student name and Grade are mandatory fields.
  •  System should validate this fields and show the proper error message.

For this example 
  • Create jsp pages for input interface, Error, Success response messages. - view
  • Create a java class (Student.java) for a student model - model
  • Create a servlet class for redirect a  proper view - controller  

Student Registration page.



When we click "Insert" button after enter all details. 


If we don't enter the Mandatory fields. This error message will display



Mapping a servlet page
Servlets need to be configured ans it needs to be mapped to a URI (URL/URN).
So we have to include the following mapping to the web.xml file.

<servlet>
    <servlet-name>ServletPage</servlet-name>
    <servlet-class>org.ymini.lukshica.controller.ServletPage</servlet-class>
 </servlet>
  
 <servlet-mapping>
    <servlet-name>ServletPage</servlet-name>
    <url-pattern>/Test</url-pattern>
 </servlet-mapping>


Mappings are used by the servlet container to translate a particular request URI to a particular servlet.

Thursday, April 26, 2012

Maven Tomcat Plugins



I tried to install and build a war into a tomcat server through 
maven. 
 I add the following tomcat plugin into the pom.xml file 



<plugins> 
        <!-- Maven Tomcat Plugin --> 
        <plugin> 
                <groupId>org.codehaus.mojo</groupId> 
                <artifactId>tomcat-maven-plugin</artifactId> 
                        <configuration> 
                                <server>TomcatServer</server> 
                        </configuration> 
        </plugin> 
</plugins>




Wednesday, April 25, 2012

My First Project in Maven

Steps to build and execute a maven project

·    Install jdk 1.7 and set the class path
·     Install 3.0.4 and set the class path -http://maven.apache.org/download.html#Installation
·    Type “mvn –version” in the command prompt to check whether the maven installed properly or not.
 ·   Create a directory to build your project and go that directory via command prompt
       C:\users\Lukshy\Project>
 ·   Type “ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=org.yarlithub.yschool -DartifactId=yschool” in the command prompt. 


       · Then a directory yschool (project name) has created in that folder. Go to that directory and compile the project.
      Type “mvn compile” 
      C:\users\Lukshy\Project\yschool> mvn compile

 ·  Here also the build success message displayed
 ·  Include the jetty plugin in the pom.xml file
      · Type "mvn install jetty : run” . Now the jetty will be start.
 · Now we can see the page in the browser.Go to the browser and type:   
      http://localhost:8080/yschool
·    Go to the file index.jsp along this path yschool -->src --> main --> webapp -->index.jsp and we can we can edit it. 






Tuesday, April 24, 2012

Session 1 - Java web programming

This session was conducted by JK. First he talked about why they started ySchool project and what they going to do with this course.From this course I hope that we can get the ability to develop an open source project.

What I learned
What is web programming?
Web programming is about interaction between the computers via browser.JSP and Servlets are Java’s web technologies.
Web Server – The webserver contains the all web data. When a client requests a web object it gives the correct object to the client.
Eg: - Apache Server         
ISS
Servlet Container – It is also a webserver. It is most specific to dynamic data.When we write java related applications the apache server doesn’t accept them.  Therefore we need a servlet.  Servlet can process JSP.
Application Server - Application server also a server which exposes business logic to client applications through various protocols.


Process - when a client request a simple web page

  • The client request a simple hello.jsp file to the server
  • The server contains the jsp container. Which is used for process JSP pages
  • The container knows which servlet controller is appropriate to process this page and send to it.
  • Then the servlet page is generated.
  • The page is send back to the container as a servlet class file with a response message.
  • The container execute it and send the response to the client.
MVC - Model View Controller
MVC is a way of splitting an application in to three parts: a model, a view and a controller.
Nowadays we have to satisfy multiple type of end users. There fore we need a Model View Controller to support different type of users.
In web applications 
JSP pages are the views and the servlets are as controllers.




      

ySchool Project


I have a great opportunity to participate at the free crash course on Java web programming. This free online course is held by yarlIt hub community for developing ySchool project.
As a Jaffna student I would like to say, joining on yschool project is a good chance to us. Because we haven’t got any opportunity like this yet