Delphi调用Servlet

来源:互联网 发布:貂蝉离间数据 编辑:程序博客网 时间:2024/05/17 21:29

一直想让Delphi做为RIA,而业务层使用Java,而Java与Delphi之间使用JSON进行交互!现在先实现一下如何使用Delphi调用Servlet

目前有2种方案:

  1. WebService
  2. IndyHttp调用Servlet

以下是用IndyHttp来调用Servlet

一、先写一个helloworld的Servlet

1.编写ui以及service

[java] view plain copy
  1. //工具包  
  2. package com.cdrs.jutils;  
  3. import java.io.IOException;  
  4. import java.util.Date;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. public abstract class FormObject extends HttpServlet {  
  10.    
  11.     protected static final String   ACTION                      = "ACTION";  
  12.     public FormObject() {  
  13.         super();  
  14.     }  
  15.     /** 
  16.      * Destruction of the servlet. <br> 
  17.      */  
  18.     public void destroy() {  
  19.         super.destroy(); // Just puts "destroy" string in log  
  20.         // Put your code here  
  21.     }  
  22.    
  23.      */  
  24.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  25.             throws ServletException, IOException {  
  26.    
  27.     }  
  28.    
  29.     @SuppressWarnings("deprecation")  
  30.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  31.             throws ServletException, IOException {  
  32.         Date date = new Date();  
  33.         System.out.println(date.getHours()+date.getMinutes()+date.getSeconds());  
  34.         performTask(request, response);  
  35.     }  
  36.     /** 
  37.      * Initialization of the servlet. <br> 
  38.      * 
  39.      * @throws ServletException if an error occurs 
  40.      */  
  41.     public void init() throws ServletException {  
  42.         // Put your code here  
  43.     }  
  44.     public abstract void performTask(HttpServletRequest req,  
  45.             HttpServletResponse res) throws ServletException, IOException;  
  46. }  
  47. //ui包  
  48. package com.cdrs.servlettest.ui;  
  49. import java.io.IOException;  
  50. import java.io.PrintWriter;  
  51. import java.sql.SQLException;  
  52. import javax.naming.NamingException;  
  53. import javax.servlet.ServletException;  
  54. import javax.servlet.http.HttpServletRequest;  
  55. import javax.servlet.http.HttpServletResponse;  
  56. import com.cdrs.jutils.FormObject;  
  57. import com.cdrs.servlettest.service.ServletTestService;  
  58. public class ServletTestForm extends FormObject {  
  59.     /** 
  60.      *  
  61.      */  
  62.     private static final long serialVersionUID = 1L;  
  63.     private ServletTestService serv = new ServletTestService();  
  64.     private void hello(HttpServletRequest req, HttpServletResponse res)  
  65.     throws IOException, SQLException, NamingException {  
  66.         res.setContentType("text/html;charset=UTF-8");  
  67.         PrintWriter out = res.getWriter();  
  68.    
  69.         String context = req.getParameter("txtcontext");  
  70.            
  71.         out.write(serv.hello(context)) ;  
  72. }  
  73.     public void performTask(HttpServletRequest req, HttpServletResponse res)  
  74.             throws ServletException, IOException {  
  75.         // TODO Auto-generated method stub  
  76.         try {  
  77.             String reqAction = req.getParameter(ACTION).trim();  
  78.        
  79.                
  80.               if (reqAction.equals("hello")) {  
  81.                 hello(req, res);  
  82.               }  
  83.         } catch (Exception e) {  
  84.             e.printStackTrace();  
  85.    
  86.             req.setAttribute("errormessage", e.getMessage());  
  87.                
  88.         }  
  89.     }  
  90. }  
  91. //服务包  
  92. package com.cdrs.servlettest.service;  
  93. import java.sql.SQLException;  
  94. import javax.naming.NamingException;  
  95. public class ServletTestService {  
  96.     public String hello(String context) throws SQLException, NamingException {  
  97.         return context;  
  98.     }  
  99. }  

 

2.配置web.xml

[xhtml] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"   
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.   <servlet>  
  8.     <description>Just for test</description>  
  9.     <display-name>Just for test</display-name>  
  10.     <servlet-name>ServletTest</servlet-name>  
  11.     <servlet-class>com.cdrs.servlettest.ui.ServletTestForm</servlet-class>  
  12.   </servlet>  
  13.   <servlet-mapping>  
  14.     <servlet-name>ServletTest</servlet-name>  
  15.     <url-pattern>/ServletTest</url-pattern>  
  16.   </servlet-mapping>  
  17.    
  18.     
  19.   <welcome-file-list>  
  20.     <welcome-file>index.jsp</welcome-file>  
  21.   </welcome-file-list>  
  22. </web-app>  

二、在Delphi端调用Servlet

[delphi] view plain copy
  1. var  
  2.   ParamList : TStringList;  
  3. begin  
  4.   ParamList := TStringList.Create;  
  5.   try  
  6.     ParamList.Add('ACTION=hello');  
  7.     ParamList.Add('txtcontext=4');  
  8.      idhtp1.Post('http://192.168.1.109:8080/ServletExample/ServletTest',ParamList) ;  
  9.   finally  
  10.     FreeAndNil(ParamList);  
  11.   end;  
  12. end;  

0 0