Tuesday, 29 November 2016

Fetch Record Using Struts2 & Maven

Step 1:index.jsp

         <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      
         "http://www.w3.org/TR/html4/loose.dtd">
        <html>
       <head>
       <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
       <title>Main Page</title>
       </head>
      <body>
      <a href="view">View  Records</a>
      </body>
      </html>

Step 2: Customer.java

public class Customer {
private int customerId;
private String customerName;
private int forevenId;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public int getForevenId() {
return forevenId;
}
public void setForevenId(int forevenId) {
this.forevenId = forevenId;
}
}


Step 3:Fetch.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class Fetch
{
ArrayList<customer> list=new ArrayList<customer>();
public ArrayList<customer> getList() {
   return list;
}
public void setList(ArrayList<customer> list) {
   this.list = list;
}
public String execute(){
try{
 Class.forName("com.mysql.jdbc.Driver");
 Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mapping","root","root");
      PreparedStatement ps=con.prepareStatement("select * from customer");
 ResultSet rs=ps.executeQuery();

 while(rs.next()){
 customer user=new customer();
 user.setCustid(rs.getInt(1));
   user.setCustname(rs.getString(2));
 user.setForevenid(rs.getInt(3));
 
  list.add(user);
 }

 con.close();
}catch(Exception e){e.printStackTrace();}
       
return "success";
}
}


Step 4:displayrecords.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<h3>All Records:</h3>
<s:iterator  value="list">
<fieldset>
<s:property value="custid"/><br/>
<s:property value="custname"/><br/>
<s:property value="forevenid"/><br/>
</fieldset>
</s:iterator>
</body>

</html>


Step 5:struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation
//DTD Struts Configuration 2.1//EN"  
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="anbc" extends="struts-default">
<action name="viewrecords" class="Fetch">
<result name="success">displayrecords.jsp</result>
</action>
</package>

</struts>  

Step 6: web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>StrutsDemo</display-name>
  <display-name>StrutsValidation</display-name>
     <filter>
  <filter-name>struts2</filter-name>
   <filter-class>
    org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
   </filter-class>
  </filter>
    <filter-mapping>
   <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Step 7: pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.my</groupId>
  <artifactId>StrutsDemo</artifactId>
  <version>0.0.1</version>
  <packaging>war</packaging>
 
  <dependencies>
  <dependency>
  <groupId>org.apache.struts</groupId>
  <artifactId>struts2-core</artifactId>
  <version>2.5.5</version>
  </dependency>
  <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.6</version>
  </dependency>
  </dependencies>

</project>

Friday, 18 November 2016

HibernateUtil Class

1.Create HibernateUtil Class Externally

package persistance;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
    // Close caches and connection pools
    getSessionFactory().close();
    }

}




2.Main Class

package FeesCategories;

import org.hibernate.Session;

import java.sql.*;

import persistance.HibernateUtil;
public class AddFessCategory {
public static void send(fees_category fc)
{
try{
Session session=HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
String name=fc.getFees();
// session.save(name);
PreparedStatement ps=(PreparedStatement) session.connection().prepareStatement("insert into fees_category values(null,?)");
ps.setString(1, name);
//System.out.println("success");
int a=ps.executeUpdate();
  if(a>0)
  {
  System.out.println("inserted");
  }
     
 session.getTransaction().commit();
     
}
catch(Exception e)

{
System.out.println(e);
}
}

}

Friday, 21 October 2016

Spring MVC Using Login Page

Step 1:   Create Maven Project.

              Create View folder under WEB-INF and then create jsp folders here.

Step 2: Add dependencies in pom.xml
            Spring-core
            Spring-context
            Spring-web
            Spring-webmvc

Step 3: Create Controller class(Homecontroller.java)

registration.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>  ${msg}  </title>
</head>
<body>
<form:form action="submit" method="post">
Name <input type="text" name="name">
Password<input type="password" name="password">
Mobile No<input type="text" name="mobile">
City<input type="text" name="city">
<input type="submit" value="Submit">
</form:form>
<h1> ${msg1} </h1>
</body>

</html>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>Spring_4</display-name>
  <servlet>
  <servlet-name>springmvcdispatcher1</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>springmvcdispatcher1</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/springmvcdispatcher1-servlet.xml</param-value>
  </context-param>
  <listener>
  <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

</web-app>


                   Create Spring bean configuration file.the file name must be same as servlet name.here the servlet name is springmvcdispatcher1.so the the bean configuration file name is 
springmvcdispatcher1-servlet.xml.


springmvcdispatcher1-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.spring.register"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>


Homecontroller.java


package com.spring.register;
import java.sql.Connection;
import java.sql.PreparedStatement;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {

@RequestMapping(value="/registration",method=RequestMethod.GET)
public String init(Model model)
{
model.addAttribute("msg","Enter the values");
return "registration";
}
@RequestMapping(value="/submit",method = RequestMethod.POST)
   public String sub(Model model, @ModelAttribute("register") Registration register)
   {
String name=register.getName();
String password=register.getPassword();
String city=register.getCity();
int mobile=register.getMobile();
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory=cfg.buildSessionFactory();
Session session=factory.openSession();
Transaction t=session.beginTransaction();
//register.setId(2);
register.setName(name);
register.setPassword(password);
register.setCity(city);
register.setMobile(mobile);
session.save(register);

//PreparedStatement ps=session.connection().prepareStatement("insert into registration values(null,?,?,?,?)");
 t.commit();
session.close();
model.addAttribute("msg1","successfully registered");
return "registration";
         }
}











                                                                                           

Wednesday, 28 September 2016

Automatically Table Creation Using Hibernate

1.To create pojo class

Student.java

public class student {

private int id;
private String name;
private String email;
private int marks;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}

}

2.To Add Dependencies

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.hiber</groupId>
  <artifactId>AutoTable</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>AutoTable Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>3.6.5.Final</version>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.23</version>
    </dependency>
    <dependency>
    <groupId>javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.12.1.GA</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>AutoTable</finalName>
     <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>AutoTable</display-name>
  <welcome-file-list>
 
    <welcome-file>index.jsp</welcome-file>
 
  </welcome-file-list>

</web-app>


3.To Create Configuration file

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/student</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping resource="student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


4.To Create Hibernate Mapping File

student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
   <class name="student" table="student">
      <meta attribute="class-description">
         This class contains the student detail. 
      </meta>
      <id name="id" column="sid"/>
      <property name="name" column="sname"/>
      <property name="email" column="semail"/>
      <property name="marks" column="smarks"/>
   </class>
</hibernate-mapping>


5.Test.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Test {
public static void main(String args[]){
try{
stud st=new stud();
st.setId(111);
st.setName("jenifer");
st.setEmail("jeni@gmail.com");
st.setMarks(170);
//Student Object State Is Transient at this point of time
Configuration cfg =new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session s=sf.openSession();
s.save(st);
//Student Object State Is Persistent at this point of time
s.beginTransaction().commit();
//Student Object State Is Database State(It means data is saved inside database)
s.evict(st);
}
catch(Exception e){
System.out.println(e);
}
}

}


ISSUES

1.Cannot Change the version of project facet web module 3.1

>go to the project location>.settings>or.eclipse.wst.common.project.facet.core.xml

change jst.web version to 3.1

2.Maven Java EE Configuration problem



Monday, 29 August 2016

Spring Using Jdbc

Step 1:


Employee.java

package com.jeni.Spring;
public class Employee {
private int id;
private String Name;
private String salary;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
}


Step 2:

EmployeeDao.java

package com.jeni.Spring;
import org.springframework.jdbc.core.JdbcTemplate;
public class EmployeeDao {
private JdbcTemplate jdbcTemplate;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
   this.jdbcTemplate = jdbcTemplate;
}

public int saveEmployee(Employee e){
   String query="insert into register values('"+e.getId()+"','"+e.getName()+"','"+e.getSalary()+"')";
   return jdbcTemplate.update(query);
}

public Employee newEmp() {
Employee emp = new Employee();
emp.setId(1001);
emp.setName("deepan");
emp.setSalary("7000");
return emp;
}

public int updateEmployee(Employee e){
   String query="update register set name='"+e.getName()+"',salary='"+e.getSalary()+"' where id='"+e.getId()+"' ";
   return jdbcTemplate.update(query);
}
public int deleteEmployee(Employee e){
   String query="delete from register where id='"+e.getId()+"' ";
   return jdbcTemplate.update(query);
}  }


Step 3:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jeni</groupId>
<artifactId>Spring</artifactId>
<name>jdbcSpring</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>

<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>

<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>

<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
</dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Step 4:application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3307/employee" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
 
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
 
<bean id="edao" class="com.jeni.Spring.EmployeeDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>


Step 5:Test.java

package com.jeni.Spring;

import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Test {

public static void main(String[] args) {
FileSystemXmlApplicationContext ctx;
     try{
     ctx=new FileSystemXmlApplicationContext("applicationcontext.xml");
   EmployeeDao dao=(EmployeeDao)ctx.getBean("edao");  
   
   int saveEmployee = dao.saveEmployee(dao.newEmp());
   System.out.println("success"+saveEmployee);
     }
     catch(Exception ee)
     {
     System.out.println(ee);
     }
}
}








Saturday, 27 August 2016

Servlets in Struts 2

To configure URL patterns to servlet and Struts2 



Struts.xml

<constant name="struts.action.excludePattern" value="/Register"/>

Monday, 22 August 2016

CONSUME ONLINE WEB SERVICES USING JAVA

Step 1: Online webservice

http://www.webservicex.net/geoipservice.asmx?wsdl


Step 2:  In your command prompt


              >mkdir sei
              >cd sei/

              set your jdk bin path
                       > set path=" "
                       > wsimport  http://www.webservicex.net/geoipservice.asmx?wsdl

Step 3:  After that the package created in sei folder.it contains all class files.

Step 4: Then Convert class file to java file using the below commands

              sei> wsimport -keep -s src http://www.webservicex.net/geoipservice.asmx?wsdl

Step 5: After that import all the java files in our project.



                         

Step 6: Create a class file

IPFinder.java

package net.webservicex;
import java.util.*;
public class IPfinder {
public static void main(String arg[])
{
// if(arg.length!=1)
// {
// System.out.println("welcome");
// }
// else
// {

        System.out.println("Enter the ip address");
Scanner sc=new Scanner(System.in);

String ipAddress=sc.next();
GeoIPService ipservice=new GeoIPService();
GeoIPServiceSoap geoipservicesoap=ipservice.getGeoIPServiceSoap();
GeoIP geoip=geoipservicesoap.getGeoIP(ipAddress);
System.out.println(geoip.getCountryName());
}
}