Tuesday, December 11, 2012

Call Servlet from normal java class

We can call  the Servlet form the basic java code , it might be used, but some situations will force to do this.
Some situations like, generate batch file in core java program and send the result 2 the servlet to  process the file as a native process

Example :
Step- I :  create a Dynamic web project web project

Write your servlet for example :
HelloServle.java
package com.app;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloServlet
 */
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        /*
         * Get the value of form parameter
         */
        String value1 = request.getParameter("parameter1");
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        pw.println("<html>");
        pw.println("<head><title>Hello World</title></title>");
        pw.println("<body>");
        pw.println("<h1>Hello World</h1>");
        pw.println("<b> Dear ,"+value1 +"</b>");
        pw.println("</body></html>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request,response);
    }

}
 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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 id="WebApp_ID">
    <display-name>WebApp</display-name>
      <servlet>
        <servlet-name>hello</servlet-name>
        <display-name>HelloServlet</display-name>
         <servlet-class>com.app.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello.do</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Compile the code  and deploy in the tomcat

Step- II :
RemoteClass.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
 

public class RemoteClass {

 public static void main(String args[]){
 String line;
  try {
        URL url = new URL("http://localhost:8080/WebApp/hello.do?parameter1="+value1 );
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        line = in.readLine();
        System.out.println( line );
        in.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
   }
}
Compile the code and run above code from as JavaApplication.





No comments:

Post a Comment