JAX-WS Web integration
In previous examples we done the JAX-WS simple example with publisher and client. In this example we are going to integrate JAX-WS with web application and one web application client. One of our previous example we done this same stuff which is using Apache Axis api.Goals
1. Create a web application which publish web services
2. Create a web application client
2. Create a web application client
Software used
1. Eclipse
2. Java 1.7
3. JAX-WS API
4. Tomcat service
5. JAX-WS RI
1. Create a web application which publish web service
In this example we are using Eclipse so create a new dynamic web project from file new and create a web service class which define our example function (Web service to get State name upon passing state code)
package com.pretech;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class StateProvider {
@WebMethod
public String getStateName(String name) {
String stateName = null;
if (name.equals("KL")) {
stateName = "KERALA";
}
if (name.equals("KA")) {
stateName = "KARNATAKA";
}
return stateName;
}
}
2.Add JAX-WS RI libraries into WEB-INF/lib folder
Download JAX-WS RI jars from https://jax-ws.java.net/2.2.8/ and place into WEB-INF/lib folder
3. Create sun-jaxws.xml file and place in to WEB-INF directory
<?xml version="1.0" encoding="UTF-8"?>
<endpoints
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint
name="StateWs"
implementation="com.pretech.StateProvider"
url-pattern="/state"/>
</endpoints>
<endpoints
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint
name="StateWs"
implementation="com.pretech.StateProvider"
url-pattern="/state"/>
</endpoints>
4. Modify web.xml file (Add web service context listener and servlet)
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
5. Deploy Web application in tomcat server and check the wsdl file
Once the server started check our wsdl file using below url http://localhost:8080/JAXWS-Tomcat-Publisher/state?wsdl
Create a web application client
1.Generate artifacts
Use wsimport tool and generate artifacts, use below command to generate client required artifacts
wsimport -d . -keep http://localhost:8080/JAXWS-Tomcat-Publisher/state?wsdl2.Create a Client Web application
Create a new web application project and copy all artifacts in to web application source folder
3.Create a jsp page to consume web service
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.pretech.*" %>
<!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>State Consumer Example</title>
</head>
<body>
<%
StateProviderService service = new StateProviderService();
StateProvider hservice=service.getStateProviderPort();
if(request.getParameter("code")!=null)
{
String code=request.getParameter("code");
if(!code.equals(""))
{
out.println(hservice.getStateName(code));
}
}
%>
<form action="/JAX-WS-Tomcat-Client/StateConsumer.jsp" method="get">
Enter state code
<input type="text" name="code">
<input type="submit" value="submit">
</form>
</body>
</html>
pageEncoding="ISO-8859-1"%>
<%@ page import="com.pretech.*" %>
<!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>State Consumer Example</title>
</head>
<body>
<%
StateProviderService service = new StateProviderService();
StateProvider hservice=service.getStateProviderPort();
if(request.getParameter("code")!=null)
{
String code=request.getParameter("code");
if(!code.equals(""))
{
out.println(hservice.getStateName(code));
}
}
%>
<form action="/JAX-WS-Tomcat-Client/StateConsumer.jsp" method="get">
Enter state code
<input type="text" name="code">
<input type="submit" value="submit">
</form>
</body>
</html>
No comments:
Post a Comment