Java

Saturday 16 March 2013

web services Introduction



Introduction to web services:

 

TUTIORALS FROM MADHAV:

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT.... 




1.What are web services?
2.Types of web servicesa

What are web services?


  •   Web services are client and server applications that communicate over the World Wide Web (WWW) hyper text transfer protocol (HTTP)
  •   As described by the www consortium (W3C), web services provide a standard means of interoperating between software applications running on a variety of platform and frameworks.
  •  Web services are characterized by great interoperability and extensibility, as well as the machine-process able descriptions,
  • Web services can be combined in a loosely coupled way to achieve complex operations.
  •  Programs providing simple services can interactive with each other to deliver sophisticated added value services.

Types of web services:


The two types:
1.big web services
2.restful web services.

  • In java, jax-ws provides the functionally for big web services.
  •   Big web services use XML messages that follow the simple object access protocol (SOAP) standard, an xml language defining a message architecture and message formats.
  •   Such systems often contain a machine-readable description of the operations offered by the service, written in the web services description language (WSDL),and xml language for defining interfaces syntactically.
  •   The soap message format and the WSDL interface definition language have gained widespread adoption.
  •  Many development tools, such as netbeans IDE, can reduce the complexity of developing web service applications. 
  •   A SOAP –based design must include the following elements.
  •   A format contract must be established to describe the interface that the web service offers.
  • WSDL can be used to describe the details of the contract, which may include messages,Operations, bindings, and the location of the web services
  •  In java EE6 ,JAX-RS provides the functionality for representational state transfer(restful) web services.
  •   Rest is well suited for basic, ad hoc integration scenarios.





TUTIORALS FROM MADHAV:

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT....

 Related topics:

websevice jax ws rpc example


websevice jax-ws-rpc style:
-----------------------------------

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    HIBERNATE-ANNOTATIONS     SPRING-IOC       SPRING –AOP-SCHEMABASED   

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT.... 





NOTE:   1)  maven dependeccy pom.xml  , we can get from previous example.
              

SayHaiService .java
--------------

package madhav;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public interface SayHaiService {
    @WebMethod
    public String sayHai(String hai);
   
}

SayHaiServiceImpl .java
---------------
package madhav;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(endpointInterface = "madhav.SayHaiService")
public class SayHaiServiceImpl implements SayHaiService{

    @Override
    @WebMethod
    public String sayHai(String hai) {
        return hai;
    }

}


Publish .java
--------------

package madhav;
import javax.xml.ws.Endpoint;
public class Publish {
    public static void main(String[] args) {
        Endpoint.publish("http://localhost:8080/webservice-JAX-WS-RPC/sayhai", new SayHaiServiceImpl());
        System.out.println("say hai service is published");
    }

}




Client .java
---------------
package madhav;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class Client {
    public static void main(String[] args) {
        URL url = null;
        try {
            url = new URL("http://localhost:8080/webservice-JAX-WS-RPC/sayhai?wsdl");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        QName qname = new QName("http://madhav/",
                "SayHaiServiceImplService");

        Service service = Service.create(url, qname);

        SayHaiService sayHaiService = service.getPort(SayHaiService.class);

        System.out.println(sayHaiService.sayHai("hai how r u"));

    }

}
TUTIORALS FROM MADHAV:

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    HIBERNATE-ANNOTATIONS     SPRING-IOC       SPRING –AOP-SCHEMABASED   

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT....  



Monday 4 February 2013

sprin mvc multiactioncontroller example




TUTIORALS FROM MADHAV:


     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY  


Spring MVC "MultiActrionController"

Spring MVC:



Tepes of controllers:
  1. ·         Controller 
  2.           AbstractCommandController
  3. ·         SimpleFormController
  4. ·         WizardFormController
  5. ·         MultiActionController


Ex 5:
// this example program willdescribes- MultiActionController

Index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
               
                <link rel="stylesheet" type="text/css" href="styles.css">
                -->
  </head>
 
  <body bgcolor="wheat">
   This is my JSP page. <br>
    <a href="sayHai.spring">click hea to say hai</a><br>
     <a href="sayBye.spring">click hear to say bye</a><br>
      <a href="login.spring">click hear for login into softech </a><br>
  </body>
</html>
Hai.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'Hai.jsp' starting page</title>
   
               
                <link rel="stylesheet" type="text/css" href="styles.css">
                -->
  </head>
 
  <body bgcolor="wheat">
    hai this is madhav <br>welcome to softech<br>
  </body>
</html>
Login.jsp
<!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>Softech computer education</title>
<head>
<body bgcolor="wheat">
<form action="login.spring"><pre>
user name:<input type="text" name="uname"/>
password :<input type="password" name="pass"/>
  <input type ="submit" value="login"/>
</pre>
</form>
</body>
</html>
Bte.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'Bye.jsp' starting page</title>
   
               
                <link rel="stylesheet" type="text/css" href="styles.css">
                -->
  </head>
 
  <body bgcolor="wheat">
    Bye...............<br>
  </body>
</html>
StudentController.java
package madhav;
import org.springframework.web.servlet.*;
import org.springframework.web.servlet.mvc.*;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver;
import org.springframework.validation.*;
import javax.servlet.http.*;
public class StudentController extends MultiActionController{
                public ModelAndView sayHai(HttpServletRequest req,HttpServletResponse res)throws Exception
                {
                                                                return new ModelAndView("/Hai");
                }
                public ModelAndView sayBye(HttpServletRequest req,HttpServletResponse res)throws Exception
                {
                                                                return new ModelAndView("/Bye");
                }
                public ModelAndView login(HttpServletRequest req,HttpServletResponse res)throws Exception
                {
                                String type=(String)req.getParameter("name");
                                if(type==null)
                                                return new ModelAndView("/Login");
                                                else if(type.equals("hugo"))
                                                                return new ModelAndView("/hugo");
                                                else
                                                                return new ModelAndView("/UserHome");
                }
}
Servlet-dispatcher.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:p="http://www.springframework.org/schema/p"
                xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
               
               
<bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
                                  p:prefix="/WEB-INF/jspfiles/" p:suffix=".jsp" />
    <bean name="/*.spring" class="madhav.StudentController">
                   </bean>
 </beans>                                       
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
                xmlns="http://java.sun.com/xml/ns/j2ee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID">
                <display-name>speingmvcacc</display-name>
 
  <servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.spring</url-pattern>
</servlet-mapping>
<welcome-file-list>
                <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Output screens: