Delphi调用Servlet
来源:互联网 发布:貂蝉离间数据 编辑:程序博客网 时间:2024/05/17 21:29
一直想让Delphi做为RIA,而业务层使用Java,而Java与Delphi之间使用JSON进行交互!现在先实现一下如何使用Delphi调用Servlet
目前有2种方案:
- WebService
- IndyHttp调用Servlet
以下是用IndyHttp来调用Servlet
一、先写一个helloworld的Servlet
1.编写ui以及service
- //工具包
- package com.cdrs.jutils;
- import java.io.IOException;
- import java.util.Date;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public abstract class FormObject extends HttpServlet {
- protected static final String ACTION = "ACTION";
- public FormObject() {
- super();
- }
- /**
- * Destruction of the servlet. <br>
- */
- public void destroy() {
- super.destroy(); // Just puts "destroy" string in log
- // Put your code here
- }
- */
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- }
- @SuppressWarnings("deprecation")
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- Date date = new Date();
- System.out.println(date.getHours()+date.getMinutes()+date.getSeconds());
- performTask(request, response);
- }
- /**
- * Initialization of the servlet. <br>
- *
- * @throws ServletException if an error occurs
- */
- public void init() throws ServletException {
- // Put your code here
- }
- public abstract void performTask(HttpServletRequest req,
- HttpServletResponse res) throws ServletException, IOException;
- }
- //ui包
- package com.cdrs.servlettest.ui;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.SQLException;
- import javax.naming.NamingException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.cdrs.jutils.FormObject;
- import com.cdrs.servlettest.service.ServletTestService;
- public class ServletTestForm extends FormObject {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private ServletTestService serv = new ServletTestService();
- private void hello(HttpServletRequest req, HttpServletResponse res)
- throws IOException, SQLException, NamingException {
- res.setContentType("text/html;charset=UTF-8");
- PrintWriter out = res.getWriter();
- String context = req.getParameter("txtcontext");
- out.write(serv.hello(context)) ;
- }
- public void performTask(HttpServletRequest req, HttpServletResponse res)
- throws ServletException, IOException {
- // TODO Auto-generated method stub
- try {
- String reqAction = req.getParameter(ACTION).trim();
- if (reqAction.equals("hello")) {
- hello(req, res);
- }
- } catch (Exception e) {
- e.printStackTrace();
- req.setAttribute("errormessage", e.getMessage());
- }
- }
- }
- //服务包
- package com.cdrs.servlettest.service;
- import java.sql.SQLException;
- import javax.naming.NamingException;
- public class ServletTestService {
- public String hello(String context) throws SQLException, NamingException {
- return context;
- }
- }
2.配置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">
- <servlet>
- <description>Just for test</description>
- <display-name>Just for test</display-name>
- <servlet-name>ServletTest</servlet-name>
- <servlet-class>com.cdrs.servlettest.ui.ServletTestForm</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>ServletTest</servlet-name>
- <url-pattern>/ServletTest</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
二、在Delphi端调用Servlet
- var
- ParamList : TStringList;
- begin
- ParamList := TStringList.Create;
- try
- ParamList.Add('ACTION=hello');
- ParamList.Add('txtcontext=4');
- idhtp1.Post('http://192.168.1.109:8080/ServletExample/ServletTest',ParamList) ;
- finally
- FreeAndNil(ParamList);
- end;
- end;
0 0
- Delphi调用Servlet
- Delphi调用Servlet
- delphi 调用
- 调用 Servlet
- servlet调用
- Servlet 调用servlet
- Servlet—Servlet简介、Servlet调用过程
- Delphi调用Word
- delphi的调用约定
- delphi 调用 Excel 宏
- delphi调用CMD文件
- delphi dll 调用技巧
- Delphi调用Excel大全
- sap delphi 调用SAP
- delphi 调用 java
- Delphi调用 Oracle 函数
- DELPHI调用API
- Delphi调用 Oracle 函数
- 9. Palindrome Number
- Lua中 assert、pcall 的错误处理
- javascript创建数组或赋值的三种方法
- 我需要一个管理时间的工具
- 利用ScktSrvr打造多功能Socket服务器
- Delphi调用Servlet
- GPS定位
- Learning FP-Growth Algorithm in Python
- 字符串及垃圾回收机制
- Correct use of System.Web.HttpResponse.Redirect
- Linux下安装并配置Weblogic运行环境
- 第2章 线程安全性
- [python] LDA处理文档主题分布代码入门笔记
- js用currentStyle和getComputedStyle获取css样式(非行间)