Wednesday, June 5, 2013

Add google map in html page

index.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="styles.css" />
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
</head>
<body>

<div id="canvas"></div>
<br />
<label for="latitude">Latitude:</label>
<input id="latitude" type="text" value="" />
<label for="longitude">Longitude:</label>
<input id="longitude" type="text" value="" />

<script type="text/javascript" src="gmap.js"></script>

</body>
</html>

gmap.js


// configuration
var myZoom = 12;
var myMarkerIsDraggable = true;
var myCoordsLenght = 6;
var defaultLat = 37.973787;
var defaultLng = 23.722426;

// creates the map
// zooms
// centers the map
// sets the map's type 
var map = new google.maps.Map(document.getElementById('canvas'), {
zoom: myZoom,
center: new google.maps.LatLng(defaultLat, defaultLng),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

// creates a draggable marker to the given coords
var myMarker = new google.maps.Marker({
position: new google.maps.LatLng(defaultLat, defaultLng),
draggable: myMarkerIsDraggable
});

// adds a listener to the marker
// gets the coords when drag event ends
// then updates the input with the new coords
google.maps.event.addListener(myMarker, 'dragend', function(evt){
document.getElementById('latitude').value = evt.latLng.lat().toFixed(myCoordsLenght);
document.getElementById('longitude').value = evt.latLng.lng().toFixed(myCoordsLenght);
});

// centers the map on markers coords
map.setCenter(myMarker.position);

// adds the marker on the map

myMarker.setMap(map);


styles.css

#canvas {
    width: 600px;
    height: 400px;

}


Thursday, November 15, 2012

Server Configuration

Server is a computer program that provides services to client software running on computers.
Types of Server:
  1. Web Server
  2. Proxy Server
  3. Mail Server
  4. DNS (Domain Name System) Server
  5. Database Server
  6. File Server
 Web Server provides content such as web pages using HTTP (Hyper Text Transfer Protocol) over the WWW (World )
A DNS Server runs special-purpose networking software, features a public IP address, and contains a database of network names and addresses for other Internet hosts. 

DNS servers communicate with each other using private network protocols. All DNS servers are organized in a hierarchy. At the top level of the hierarchy is root servers. Root servers store the complete database of Internet domain names and their corresponding IP addresses. There is 13 root servers contain the complete database of domain names and IP addresses. Ten of these servers are in US.
     
     

Tuesday, May 8, 2012

Java Server Faces(JSF) with Netbeans

Java Server Faces (JSF) is a user interface framework for Java web applications. It is designed to significantly ease the burden of writing and maintaining applications that run on a Java application server and render their UIs back to a target client.

Create a simple Hellworld program :

Create a JSF page:

Create a maven web app in netbeans. (File >New Project > maven >web app >Helloworld ).
Then right click the project, go to properties >frameworks >add.
Select Java Server Faces.

index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"      
      xmlns:h="http://java.sun.com/jsf/html">
 
    <h:head>
        <title>Hello World</title>
    </h:head>
    <h:body>
     <h3>Hello World Example/h3>
     <h:form>
        <h:inputText value="#{helloBean.name}"></h:inputText>
        <h:commandButton value="Welcome Me" action="welcome"></h:commandButton>
     </h:form>
    </h:body>
</html>


JSF 2.0 dependencies:

<dependencies> <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>
<dependencies>

You must add these dependencies into pom.xml

Create Managed Bean:
Managed bean can be accessed from a JSF page.
Use @ManagedBean annotation to indicate this is a managed bean.


Righi click the project, >New > JSF Managed Bean > helloBean

helloBean.java

package org.yit.yschool.bean;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
 
@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {
 
 private static final long serialVersionUID = 1L;
 
 private String name;
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}


welcome.xhtml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"    
      xmlns:h="http://java.sun.com/jsf/html">
 
    <h:head>
     <title>Hello World</title>
    </h:head>
    <h:body>
     <h3>Hello World Example</h3>
     <h4>Welcome #{helloBean.name}</h4>
    </h:body>
</html>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
   <web-app>
<display-name>Archetype Created Web Application</display-name> <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> </web-app>


Thursday, May 3, 2012

MVC application in JSP


Model View Controller is a architectural design pattern.
  • Model is business logic.
  • View is user interface.
  • Controller handles the 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.

This is the small application using MVC architecture.The following are what I have done for this application.
  • Writing an HTML form page (index.jsp)
  • Creating a Servlet controller - (Controller.java)
  • Creating the Model) -(StudentDetails.java)
  • Creating an XML deployment descriptor
  • Creating an JSP View - (response.jsp)
The web application is a Student marks entered application. These are considered here:
  • Students marks entered in the following way. Student Name (StudentName loaded in ComboBox), Grade (1-13 ComboBox), Subject, Marks
  • User can enter these value and click "Add".
  • Here all these fields are mandatory fields and marks can not be more than 100.
  • System would validate those fields and show it in the response page.
Below is the users input screen.


After fill this form and click "Add", the following page will be display.


Bellow is error pages.

  1. This error page will be display when we didn't fill all fields.



2. This error page will be display when we enter the marks in more than 100 or less than 0



3. This error page will be display when we enter the marks in text.


The HTML form page
This is the simple HTML form (index.jsp).

<%-- 
    Document   : index
    Created on : May 2, 2012, 2:01:45 PM
    Author     : Vanaja
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        
    </head>
    <body>
        <center>
        <h1>Add Student Marks</h1>
        <form action="Controller" method="Post" > 
            <table border="0">
                <tbody>
                    <tr>
                        <td width="100">Student Name:</td>
                        <td><select name="studentName">
                                <option>Lukshica</option>
                                <option>Mayooran</option>
                                <option>Vijay</option>
                                <option>Nilash</option>
                                <option>Thenu</option>
                                <option>Ramanan</option>
                                <option>Tharo</option>
                                <option>Keerthika</option>
                                <option>Meera</option>
                            </select></td>
                    </tr>
                    <tr>
                        <td> Grade:</td>
                        <td><select name="grade">
                                <option>1</option>
                                <option>2</option>
                                <option>3</option>
                                <option>4</option>
                                <option>5</option>
                                <option>6</option>
                                <option>7</option>
                                <option>8</option>
                                <option>9</option>
                                <option>10</option>
                                <option>11</option>
                                <option>12</option>
                                <option>13</option>
                            </select></td>
                    </tr>
                    <tr>
                        <td>Subject:</td>
                        <td><input type="text" name="subject"/></td>
                    </tr>
                    <tr>
                        <td> Marks:</td>
                        <td><input type="text" name="marks"/></td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="Add" name="Add" /></td>
                        <td><input type="reset" value="Reset" name="Reset" /></td>
                    </tr>
                </tbody>
            </table> 
            </center>
        </form>
    </body>
</html>

The Deployment Descriptor
First we must create the deployment descriptor which is web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >


<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>Controller</servlet-name>
    <servlet-class>ControllerPackage.Controller</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>Controller</servlet-name>
    <url-pattern>/Controller</url-pattern>
  </servlet-mapping>
</web-app>

Here <servlet> tag used to register servlets within this webapp.
<servlet-mapping> tag use to map the url with the servlet.

The Controller Servlet and Model class

Now we must create the model class that will be used by the controller.
Model class:( StudentDetails.java )

package Student;
/**
 *
 * @author Vanaja
 */
public class StudentDetails {
   String studentName;
   String grade;
   String subject;
   int marks;


    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public String getGrade() {
        return grade;
    }
    public void setGrade(String grade) {
        this.grade = grade;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public int getMarks() {
        return marks;
    }
    public void setMarks(int marks) {
        this.marks = marks;
    }
}


Controller class: ( Controller.java)

package ControllerPackage;


import Student.*;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author Vanaja
 */
@WebServlet(name = "Controller", urlPatterns = {"/Controller"})
public class Controller extends HttpServlet {  
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
    }
  
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String studentName;
        String grade;
        String subject;
        String marks;
        
        studentName=request.getParameter("studentName");
        grade=request.getParameter("grade");
        subject=request.getParameter("subject");
        marks=request.getParameter("marks");
        
        Validateclass v=new Validateclass(); 
        boolean result1=v.checkFields(subject,marks);   
         
        if(result1){ 


            boolean result2=v.is_int(marks);
            if(result2){ 


                int marks1=Integer.parseInt(marks);
                boolean result3=v.checkMarks(marks1);
                if(result3){
                    
                    StudentDetails sd=v.setDetails(studentName,grade,subject,marks1);
                    request.setAttribute("Student",sd);
                    RequestDispatcher dispatcher = request.getRequestDispatcher("response.jsp");
                    dispatcher.forward(request,response); 
                }
                else
                    response.sendRedirect("error2.jsp"); 
            }
            else
               response.sendRedirect("error3.jsp"); 
        }
        else
            response.sendRedirect("error.jsp");
    }
    
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}


Now we want to create Validate class.
Validate class: (ValidateClass.java)

package ControllerPackage;


import Student.StudentDetails;
/**
 *
 * @author Vanaja
 */
public class Validateclass {
    
    public boolean checkFields(String subject, String marks)
    {
        if("".equals(subject.trim()) || subject==null || "".equals(marks.trim()) || marks==null )
            return false;
        return true;
    }
    
    public boolean checkMarks(int marks)
    {
        if( marks > 100 || marks < 0 )
            return false;
        return true;
    }
    
    public boolean is_int(String marks)
    {
        for (int i = 0 ; i < marks.length() ; i++) { 
              if((marks.charAt(i) < 48 ) || (marks.charAt(i) > 57)) 
                     return false ;
               } 
               return true;
    }
           
    public StudentDetails setDetails( String studentName,String grade,String subject,int marks)
    {
        StudentDetails sd=new StudentDetails();
        sd.setStudentName(studentName);
        sd.setSubject(subject);
        sd.setGrade(grade);
        sd.setMarks(marks);
        return sd;
    }
}

The JSP View
Now we create must create the JSP view page that will display the successfully added student marks (response.jsp).

<jsp:useBean id="Student" scope="session" class="Student.StudentDetails" />


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <center>
        <h2>You successfully add the marks </h2>
       <table border="0">
<tr>
<td width="100">Student Name:</td>
        <td><jsp:getProperty name="Student" property="studentName" /></td>
        </tr> 
        
        <tr>
        <td>Grade:</td>
        <td><jsp:getProperty name="Student" property="grade" /></td>
        </tr>
        
        <tr>
        <td>Subject:</td>
        <td><jsp:getProperty name="Student" property="subject" /></td>
        </tr>
        
        <tr>
        <td>Marks:</td>
        <td><jsp:getProperty name="Student" property="marks" /></td>
</tr> 
    </table>
    </center>  
        
    </body>
</html>

Bean has the setter and getter methods.
-the setter: used to note the value in a certain property.
-the getter: used to show the value of a certain property.

The advantage of using getters and setters is that an object of the class is always responsible for its values. With the setter, the class can control the values it is supposed to write down in its property. In certain cases, the class can refuse to write down an illegal value. The same goes for the getters  : the class can decide to show the value in a certain format.