Web Service应用举例 及一问题的解决方法

由于系统要求 需要做一webservice将应用服务器的压力分配到其它的机器上,于是写了下面的WebSerivce服务
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using DBCore;

namespace QQWebService
{
 /// <summary>
 /// QQMessage 的摘要说明。
 /// </summary>
 [WebService(Namespace="QQWebService")]
 public class QQMessage : System.Web.Services.WebService
 {
  DBCore.DataBaseVisitor.AbsDBHelper dbhelper = new DBCore.DataBaseVisitor.FactoryDBHelper().CreateDBHelper("DataBaseSql",DBCore.DataBaseVisitor.DataBaseConnectionType.Sql);

  public QQMessage()
  {
   //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
   InitializeComponent();
  }

  [WebMethod]
  public void SetDBHelperofSQLClient(string  conn)
  {
   this.dbhelper = new DBCore.DataBaseVisitor.JSLSqlHelper();
   dbhelper.SetDBConnection = conn;
  }

  [WebMethod]
  public DataSet GetSender(string userid)
  {
   string sql="select distinct Sender from tOA_Message_Temp_User where   IsRead='0' and receiver='"+userid+"'";
   return dbhelper.ExecuteDataset(sql);
  }
...

   [WebMethod]
  public DataSet CSelectSys(ArrayList al)
  {
   if (al == null) return null;
   string sql = @"
   Select TopicID,Content,Sender,SendTime,IsSend,IsAutoBack,IsRead,Receiver,ReceiverTime,Sender as username
   From tOA_Message_Temp_User
   where Sender='$0$'
            and Receiver='$1$'
   and IsRead='$2$'
    ";
   for(int i = 0; i < al.Count; i++)
   {
    sql = sql.Replace("$"+i.ToString()+"$",al[i] != null ? al[i].ToString() : "");
   }
   return dbhelper.ExecuteDataset(sql.Replace("/t"," "));
  }

   #region 组件设计器生成的代码
  
  //Web 服务设计器所必需的
  private IContainer components = null;
    
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if(disposing && components != null)
   {
    components.Dispose();
   }
   base.Dispose(disposing);  
  }
  
  #endregion

  }
}

在系统当中添加web引用,关键位置做了如下处理
xxx.QQClient {
    using System.Diagnostics;
    using System.Xml.Serialization;
    using System;
    using System.Web.Services.Protocols;
    using System.ComponentModel;
    using System.Web.Services;
   
   
    /// <remarks/>
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Web.Services.WebServiceBindingAttribute(Name="QQMessageSoap", Namespace="QQWebService")]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(object[]))]
    public class QQMessage : System.Web.Services.Protocols.SoapHttpClientProtocol {
       
        /// <remarks/>
        public QQMessage() {
            this.Url = "http://10.60.0.147/QQWebService/QQMessage.asmx";
        }
 
  public QQMessage(string url)
  {
   this.Url = url;
  }     

  public QQMessage(string url, string conn)
  {
   this.Url = url;
   SetDBHelperofSQLClient(conn);
  }
...

在系统引用webservice时做如下类

using System;
using System.Collections;
using System.Data;
using DBCore;
namespace JQHY.jslqq
{
 /// <summary>
 /// JSLQQSource 的摘要说明。
 /// </summary>
 public class JSLQQSource
 {

  static JQHY.QQClient.QQMessage qc = new JQHY.QQClient.QQMessage(System.Configuration.ConfigurationSettings.AppSettings["QQClientSerivce"],System.Web.HttpContext.Current.Application["DataBaseSql"].ToString());

  static object[ ] ToArray( System.Collections.ICollection coll )
  {
   object[ ] result = new object[coll.Count];
   int i = 0;
   foreach( object obj in coll )
   {
    result[ i++ ] = obj;
   }
   return result;
  }
  public JSLQQSource()
  {

  }

  public DataSet GetSender(string userid)
  {
   return qc.GetSender(userid);
  }
  public DataSet CSelectSys(ArrayList al)
  {
   return qc.CSelectSys(ToArray(al));
  }
...

测试发现web service只能访问本地资源,一直找不到问题所在 。
后来将程序部署到别的机器上试发现没有此类问题,寻找很久,终于解决
得如下结论: :-)

复杂的问题往往有着简单的答案
 [Web Service于Win2003下访问SQLServer2000记得一定要打上SQLSever.SP3补丁]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本文假设您已经熟悉SpringMVC框架的基本使用方法,以下是一个基于SpringMVC的电影查询和展示的Web应用的代码实现过程: 1. 创建数据库表 首先需要创建一个电影相关的数据库表,例如movies表。表结构如下: ```sql CREATE TABLE `movies` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `actors` varchar(255) NOT NULL, `director` varchar(255) NOT NULL, `type` varchar(255) NOT NULL, `release_date` date NOT NULL, `rating` decimal(3,1) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` 2. 配置数据库连接 在Spring配置文件中配置数据库连接参数,例如在applicationContext.xml中添加以下内容: ```xml <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/moviedb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> ``` 注意要替换url、username和password为自己的数据库连接信息。 3. 创建实体类 创建一个与movies表对应的Java实体类,例如Movie类。类定义如下: ```java public class Movie { private int id; private String name; private String actors; private String director; private String type; private Date releaseDate; private double rating; // getter/setter方法省略 } ``` 4. 创建DAO层 创建一个数据访问对象(DAO)用于实现对movies表的查询操作,例如MovieDao类。类定义如下: ```java @Repository public class MovieDao { @Autowired private JdbcTemplate jdbcTemplate; public List<Movie> findByType(String type) { String sql = "SELECT * FROM movies WHERE type = ?"; Object[] args = { type }; List<Movie> movies = jdbcTemplate.query(sql, args, new MovieRowMapper()); return movies; } private static final class MovieRowMapper implements RowMapper<Movie> { public Movie mapRow(ResultSet rs, int rowNum) throws SQLException { Movie movie = new Movie(); movie.setId(rs.getInt("id")); movie.setName(rs.getString("name")); movie.setActors(rs.getString("actors")); movie.setDirector(rs.getString("director")); movie.setType(rs.getString("type")); movie.setReleaseDate(rs.getDate("release_date")); movie.setRating(rs.getDouble("rating")); return movie; } } } ``` 注意要在类上添加@Repository注解,以便Spring自动管理该类的实例化和依赖注入。该类实现了根据电影类型查询电影列表的功能。 5. 创建Service层 创建一个服务层(Service),用于封装DAO层的数据查询操作,为控制层提供服务,例如MovieService类。类定义如下: ```java @Service public class MovieService { @Autowired private MovieDao movieDao; public List<Movie> findByType(String type) { return movieDao.findByType(type); } } ``` 注意要在类上添加@Service注解,以便Spring自动管理该类的实例化和依赖注入。该类封装了MovieDao的findByType方法。 6. 创建控制层 创建一个控制层(Controller),用于接收用户请求,调用Service层提供的服务,返回查询结果。例如MovieController类。类定义如下: ```java @Controller @RequestMapping("/movies") public class MovieController { @Autowired private MovieService movieService; @RequestMapping("/{type}") public String findByType(@PathVariable String type, Model model) { List<Movie> movies = movieService.findByType(type); model.addAttribute("movies", movies); return "movies"; } } ``` 注意要在类上添加@Controller注解和@RequestMapping注解,以便Spring自动管理该类的实例化和URL映射。该类实现了根据电影类型查询电影列表的功能,并将查询结果存储到Model中,返回视图名为"movies"。 7. 创建视图层 使用JSP或Thymeleaf等模板引擎创建一个视图层,例如movies.jsp。视图代码如下: ```html <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>电影列表</title> </head> <body> <h2>电影列表</h2> <table> <tr> <th>名称</th> <th>演员</th> <th>导演</th> <th>类型</th> <th>上映时间</th> <th>评分</th> </tr> <c:forEach items="${movies}" var="movie"> <tr> <td>${movie.name}</td> <td>${movie.actors}</td> <td>${movie.director}</td> <td>${movie.type}</td> <td>${movie.releaseDate}</td> <td>${movie.rating}</td> </tr> </c:forEach> </table> </body> </html> ``` 注意要在JSP页面中引入JSTL标签库(例如<c:forEach>标签)。 8. 配置SpringMVC 在SpringMVC配置文件中配置URL映射、视图解析器等参数,例如在spring-mvc.xml中添加以下内容: ```xml <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:annotation-driven /> <context:component-scan base-package="com.example.controller" /> ``` 其中InternalResourceViewResolver用于将视图名解析为JSP页面的路径,prefix指定JSP页面所在的目录,suffix指定JSP页面的后缀。mvc:annotation-driven用于启用SpringMVC注解驱动。component-scan用于扫描Controller层的类。 9. 部署应用应用打包成WAR包,并部署到Tomcat等Web服务器上,启动应用。在浏览器中访问http://localhost:8080/movies/{type},即可根据电影类型查询电影列表并展示在JSP页面上。 以上是一个基于SpringMVC的电影查询和展示的Web应用的代码实现过程,希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值