Wednesday, October 12, 2011

Develop JAX-WS using eclipse, test using SOAPUI and deploy to Tomcat


Today I am going to create a jax-ws web service and deploy to tomcat. So lets start it …

Steps : -
1 ) First we need to download jax-ws jar that we can download it from here .

2) Copy that downloaded jar file to C:/ and then  run java -jar JAXWS2.1.1_20070501.jar

3) Now Open Eclipse and Create a dynamic web project. Name it as JAXWSCalculator

4) Create a class under Src and Name it as Calculator.java under package de.swapgmbh.service.calculate

   Here is the sample Calculator.java class

   package de.swapgmbh.service.calculate;
     
   import javax.jws.WebMethod;
   import javax.jws.WebService;
   @WebService
   public class Calculator {
   @WebMethod
   public int add(int expr1, int expr2) {
   System.out.println("Now adding " + expr1 + " and "+expr2);
   return expr1+expr2;
   }
   }

6) Now Create a folder named wsdl under WebContent/WEB-INF

7) Create a folder under your root project directory structure named generate.





8) Now Click on Run -> External Tools -> Configure External Tools


   Now create a New Configuration.

   Give Name as wsgen,

   Location: C:\jaxws-ri\bin\wsgen.bat (This you have downloaded 

   and copied into c:\)

   For Arguments:
  -verbose -wsdl -keep -r "D:\New      Worksapce1\JAXWSCalculator\WebContent\WEB-INF\wsdl" -d       "D:\New Worksapce1\JAXWSCalculator\generated" -cp D:\New Worksapce1\JAXWSCalculator\build\classes  ${java_type_name}


Here I have given the location of my workspace ex (D:\New 

Worksapce1\JAXWSCalculator\WebContent\WEB-INF\wsdl). You need to 

give the location of your workspace.


9)In environment tab create this two variable,
JAVA_HOME-C:\jdk1.5.04 (Path of you JDK)
JAXWS_HOME - C:\jaxws-ri (This is you download and kept in C:\)





10) Now click apply


11) Modify the web.xml File

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 <display-name>JAXWSCalculator</display-name>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<description>JAX-WS endpoint - calculator</description>
<display-name>JAXWSCalculator</display-name>
<servlet-name>calculator</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>calculator</servlet-name>
<url-pattern>/addnumbers</url-pattern>
</servlet-mapping>
</web-app>

12) Create a new file sun-jaxws.xml under WEB-INF
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
<endpoint name="calculator" implementation="de.swapgmbh.service.calculate.Calculator" url-pattern="/addnumbers"/>
</endpoints>



13)  Now build the project using Project -> Build All

14) Now run wsgen that we have recently created.

15) Check wsdl that is generated and change the location attribute of <soap location: “”> as http://localhost:8080/JAXWSCalculator/

Now copy all the jar files from C:\jaxws-ri\lib to lib directory of your project.

Now  Export the project as war and copy to webapp directory of the tomcat.

Start tomcat and type http://localhost:8080/JAXWSCalculator/addnumbers?wsdl












Saturday, May 22, 2010

MVC with mysql Servlet and JSP

In this example I will be creating a simple application in which I will transfer the control from main page to controller servlet where it will establish database connection and store the result to a bean class.It will put the object to the session attribute and then transfer the control to jsp page where the result will be displayed.

Lets create a Home Page called a.jsp under WebContents which contains a link. This link will redirect the flow to a.java file.
a.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>Insert title here</title>
</head>
<body>
<a href="a">click me</a>
</body>
</html>    
                                          


------------------------------------------------------------------------------------------------------------



 a.java

------------------------------------------------------------------------------------------------------------
package mit.mvc;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class a extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
{
       PrintWriter out = response.getWriter();
       Connection con;
       ResultSet rs;
       DbBean bb=new DbBean();
       int[] id=new int[10];
      
       try
       {             con=DriverManager.getConnection("jdbc:mysql://localhost/student","root", "");
             
              Statement st=con.createStatement();
              rs=st.executeQuery("select * from stud");
              int i=0;
              while(rs.next())
              {             id[i]=Integer.parseInt(rs.getString("id"));
                     i++;
              }
             
              bb.setid(id);
       }catch(Exception e)
       {}
       HttpSession s=request.getSession();
       s.setAttribute("result", bb);
       RequestDispatcher dispatcher = request.getRequestDispatcher("c.jsp");
       dispatcher.forward(request, response);
}

public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
{
       doGet(request,response);
}
}              
-------------------------------------------------------------------------------------------------------

Explanation : For this application to work we need to create a student database with stud table. stud table have id and name column.
This file simply creates database connection and creates and walk through the stud table and puts the id that is  fetched from the database into integer array id. After that DbBean class is initialized and its id attribute is set to the the id attribute of the servlets class. The initialized bean object is putted into session and the control is transfered to c.java

Lets see DbBean.java
------------------------------------------------------------------------------------------------------------------------









package mit.mvc;
import java.io.*;
public class DbBean {
     
      private int[] id=new int[10];
      public void setid(int[] id)
      {
            this.id=id;
      }
      public int getid(int i)
      {
            return id[i];
      }    
}          
----------------------------------------------------------------------------------------------------
c.java
----------------------------------------------------------------------------------------------------









<%@ 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">

<%@ page language="java" import="java.sql.*" %>

<%@page import="mit.mvc.DbBean"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<jsp:useBean id="result" scope="session" type="mit.mvc.DbBean"/>

<%=result.getid(1) %>

</body>
</html>