Axis Session 关于Web Service Forum 用户登录 用户名 密码 的 保存

原创 2007年10月05日 17:54:00
最近在弄Web Service,刚学不久,解决了这个关于Axis Session的问题。网上相关的文章有很多,借鉴了其中的一种方法。调试没有问题,但不知道等Web Service Client搭建起来后,会不会出现新的问题。
由于最近才开始搞JAVA,难免会出现很幼稚的错误,研究相关主题的朋友,可以给点意见哈。
(注:Session还需要建立一个数据库,此例仍在继续开发中,完成后会上传完整的版本 )
关于Session的使用,主要涉及到了四个文件。
(2007.11.2注: ForumSoapbindingImpl 里面关于PUBLIC STATIC的两个变量应该删除掉,这是一个严重的错误。此外在服务器端,并不一定需要SESSION,所以在相应的四个文件中,涉及到SESSION的也应该删除掉。关于SESSION,在客户端做就可以了。)

Forum.wsdl  /* wsdl */
Forum.java   /*界面*/
DBconn.java   /*  用来操作数据库 */
ForumSoapBindingImpl.java    /*用于操作Forum.java*/


下面只给出关于SESSION的关键代码,注释就不写了,养成好习惯是需要时间的,呵呵。。。

Forum.wsdl
--------------
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://ForumSS07/Forum/" xmlns:intf="http://ForumSS07/Forum/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ForumSS07/Forum/">

<!--WSDL created by Apache Axis version: 1.3
Built on Oct 05, 2005 (05:23:37 EDT)-->

 <wsdl:types>
  <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ForumSS07/Forum/">
   <element name="UserLoginRequest">
    <complexType>
     <sequence>
      <element name="Username" nillable="true" type="xsd:string"/>
      <element name="Password" nillable="true" type="xsd:string"/>
     </sequence>
    </complexType>
   </element>
   <complexType name="Account">
    <sequence>
     <element name="UserID" type="xsd:int"/>
     <element name="Name" nillable="true" type="xsd:string"/>
     <element name="Vorname" nillable="true" type="xsd:string"/>
     <element name="Email" nillable="true" type="xsd:string"/>
     <element name="Username" nillable="true" type="xsd:string"/>
     <element name="Password" nillable="true" type="xsd:string"/>
    </sequence>
   </complexType>
   <element name="UserLoginResponse">
    <complexType>
     <sequence>
      <element name="UserLoginResponseItem" nillable="true" type="impl:Account"/>
     </sequence>
    </complexType>
   </element>
   <element name="EditHauptThemaRequest">
    <complexType>
     <sequence>
      <element name="Username" nillable="true" type="xsd:string"/>
      <element name="DynaID" type="xsd:int"/>
      <element name="Inhalt" nillable="true" type="xsd:string"/>
     </sequence>
    </complexType>
   </element>
   <element name="EditHauptThemaResponse">
    <complexType>
     <sequence>
      <element name="EditHauptThemaResponse" type="xsd:boolean"/>
     </sequence>
    </complexType>
   </element>
   <element name="SessionRequest">
    <complexType>
     <sequence>
      <element name="Username" nillable="true" type="xsd:string"/>
      <element name="Password" nillable="true" type="xsd:string"/>
     </sequence>
    </complexType>
   </element>
   <element name="SessionResponse">
    <complexType>
     <sequence>
      <element name="SessionResponse" type="xsd:boolean"/>
     </sequence>
    </complexType>
   </element>
  </schema>
 </wsdl:types>

  <wsdl:message name="SessionRequest">
      <wsdl:part element="impl:SessionRequest" name="SessionRequest"/>
   </wsdl:message>

  <wsdl:message name="EditHauptThemaRequest">
      <wsdl:part element="impl:EditHauptThemaRequest" name="EditHauptThemaRequest"/>
   </wsdl:message>

   <wsdl:message name="UserLoginRequest">
      <wsdl:part element="impl:UserLoginRequest" name="UserLoginRequest"/>
   </wsdl:message>

   <wsdl:message name="UserLoginResponse">
      <wsdl:part element="impl:UserLoginResponse" name="UserLoginResponse"/>
   </wsdl:message>

   <wsdl:message name="EditHauptThemaResponse">
      <wsdl:part element="impl:EditHauptThemaResponse" name="EditHauptThemaResponse"/>
   </wsdl:message>

   <wsdl:message name="SessionResponse">
      <wsdl:part element="impl:SessionResponse" name="SessionResponse"/>
   </wsdl:message>

   <wsdl:portType name="Forum">
      <wsdl:operation name="UserLogin" parameterOrder="UserLoginRequest">
       <wsdl:input message="impl:UserLoginRequest" name="UserLoginRequest"/>
       <wsdl:output message="impl:UserLoginResponse" name="UserLoginResponse"/>
      </wsdl:operation>

      <wsdl:operation name="EditHauptThema" parameterOrder="EditHauptThemaRequest">
       <wsdl:input message="impl:EditHauptThemaRequest" name="EditHauptThemaRequest"/>
      <wsdl:output message="impl:EditHauptThemaResponse" name="EditHauptThemaResponse"/>
      </wsdl:operation>

      <wsdl:operation name="Session" parameterOrder="SessionRequest">
      <wsdl:input message="impl:SessionRequest" name="SessionRequest"/>
       <wsdl:output message="impl:SessionResponse" name="SessionResponse"/>
      </wsdl:operation>
</wsdl:portType>

   <wsdl:binding name="ForumSoapBinding" type="impl:Forum">
    <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="UserLogin">
    <wsdlsoap:operation soapAction="http://ForumSS07/Forum/UserLogin"/>
   
<wsdl:input name="UserLoginRequest">
<wsdlsoap:body use="literal"/>
 </wsdl:input>

<wsdl:output name="UserLoginResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>

<wsdl:operation name="EditHauptThema">
<wsdlsoap:operation soapAction="http://ForumSS07/Forum/EditHauptThema"/>
<wsdl:input name="EditHauptThemaRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>

<wsdl:output name="EditHauptThemaResponse">
<wsdlsoap:body use="literal"/>
 </wsdl:output>
</wsdl:operation>

<wsdl:operation name="Session">
<wsdlsoap:operation soapAction="http://ForumSS07/Forum/Session"/>
<wsdl:input name="SessionRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>

<wsdl:output name="SessionResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

<wsdl:service name="ForumService">
 <wsdl:port binding="impl:ForumSoapBinding" name="Forum">
<wsdlsoap:address location="http://localhost/ForumSS07/services/Forum"/>
</wsdl:port>
</wsdl:service>

</wsdl:definitions>



Forum.java
-------------
package ForumSS07.Forum;

public interface Forum extends java.rmi.Remote {


public UserLoginResponse userLogin(UserLoginRequest userLoginRequest) throws java.rmi.RemoteException;

public SessionResponse session(SessionRequest sessionRequest) throws java.rmi.RemoteException;

public EditHauptThemaResponse editHauptThema(EditHauptThemaRequest editHauptThemaRequest) throws java.rmi.RemoteException;

}

ForumSoapBindingImpl.java
--------------------
package ForumSS07.Forum;

import org.apache.axis.MessageContext;
import org.apache.axis.session.Session;

public class ForumSoapBindingImpl implements Forum{
     
    static public String username_log;
    static public String username_psw;

    public UserLoginResponse userLogin(UserLoginRequest userLoginRequest) throws java.rmi.RemoteException {
        
         String LoginUsername=userLoginRequest.getUsername();
         String LoginPassword=userLoginRequest.getPassword();
          MessageContext mc = MessageContext.getCurrentContext();
         Session session = mc.getSession();
         session.set("username",LoginUsername);
         session.set("password",LoginPassword);
         username_log=session.get("username").toString();
         username_psw=session.get("password").toString();
         Account a = DBconn.getConnection().getAccount(LoginUsername,LoginPassword);  
        return new UserLoginResponse(a);
    }
   
    public SessionResponse session(SessionRequest sessionRequest){
   
    
        return DBconn.getConnection().setsession(username_log,username_psw);
       
    }

   public EditHauptThemaResponse editHauptThema(EditHauptThemaRequest editHauptThemaRequest) throws java.rmi.RemoteException{

        String username=username_log;
        int dynaid=editHauptThemaRequest.getDynaID();
        String new_inhalt=editHauptThemaRequest.getInhalt();
        return DBconn.getConnection().edithauptthema(username,dynaid,new_inhalt);
    }

}


DBconn.java
-------------------

package ForumSS07.Forum;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.*;

import org.apache.axis.MessageContext;
import org.apache.axis.session.Session;

public class DBconn{


    private static final String AUCTION_MYSQL_URL = "jdbc:mysql://localhost:3306/forum?user=root&password=admin";
    private static final String MYSQL_DRIVER_CLASS = "com.mysql.jdbc.Driver";
    private Connection conn = null;
    private static DBconn instance = null;
    static public String username_login;
    static public String username_password;

   
    private DBconn() {
     
        try {
            Class.forName(MYSQL_DRIVER_CLASS);
            conn = DriverManager.getConnection(AUCTION_MYSQL_URL);
            conn.setAutoCommit(false);
        } catch (ClassNotFoundException e) {
            conn = null;
            e.printStackTrace();
        } catch (SQLException e) {
            conn = null;
            e.printStackTrace();
        }}
   
    public static DBconn getConnection() {
        if (instance == null) {
            instance = new DBconn();
        }
        return instance;
       
    }  
 
    public Account getAccount(String username, String password) {
        Account account = null;
        ResultSet rs = null;
        Statement stmt = null;
     
        try {
            stmt = conn.createStatement();      
            rs = stmt.executeQuery("SELECT UserID,Name,Vorname,Email,Username,Password FROM account where Username = '" + username+ "' AND Password = '" + password + "';");
            if (rs.first()) {
                account = new Account();
                account.setUserID(rs.getInt(1));
                account.setName(rs.getString(2));
                account.setVorname(rs.getString(3));
                account.setEmail(rs.getString(4));
                account.setUsername(username);
                account.setPassword(password);
                username_login=username;
                username_password=password;
                }
            rs.close();
            stmt.close();
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            e.printStackTrace();
        }
        return account;
    }
   
    public SessionResponse setsession(String Username,String Password){
       MessageContext mc = MessageContext.getCurrentContext();
        Session session = mc.getSession();
        session.set("username",Username);
        session.set("password",Password);
        if (!session.get("username").equals(username_login)) return new SessionResponse(false);
        else return new SessionResponse(true);   
    }

    public EditHauptThemaResponse edithauptthema(String Username,int DynaID,String Inhalt){
        Statement stmt=null;
        ResultSet rsuid=null;
        try {
         stmt = conn.createStatement();
         String getid="Select UserID from account where Username='"+Username+"'";
         rsuid = stmt.executeQuery(getid);
         rsuid.next();
         int userid=rsuid.getInt(1);
         rsuid.close();
         String edithtUpdate = "UPDATE themen SET Inhalt='"+Inhalt+"' where UserID='"+userid+"' and DynaID='"+DynaID+"'" ;
         stmt.execute(edithtUpdate);
         return new EditHauptThemaResponse(true);
     } catch (SQLException e) {
         System.out.println("SQLException: " + e.getMessage());
         System.out.println("SQLState: " + e.getSQLState());
         e.printStackTrace();
     } finally {
         try {
             stmt.close();
         } catch (Exception e) { }
     }
     return new EditHauptThemaResponse(false);
    }
 
}
   

Axis Web Service开发之旅 (七) --跨服务会话(Session)管理

对于一个复杂的系统,不可能只有一个WebService服务,例如,至少会有一个管理用户的WebService(用户登录和注册)以及处理业务的WebService。象这种情况,就必须在多个WebServ...

Axis Web Service开发之旅 (六) --会话(Session)管理

WebService给人最直观的感觉就是由一个个方法组成,并在客户端通过SOAP协议调用这些方法。这些方法可能有返回值,也可能没有返回值。虽然这样可以完成一些工具,但这些被调用的方法是孤立的,当一个方...

使用Axis2实现Web Service的用户会话

转自:http://blog.csdn.net/tsun7263/article/details/4705043 对于需要进行用户认证的应用来说,需要Web Service具有会话的功能,用...

用axis2开发web service

  • 2010年05月06日 17:37
  • 853KB
  • 下载

Web Service (Axis 1.1) step by step.ppt

  • 2012年02月02日 17:16
  • 414KB
  • 下载

[图解教程]Axis2与Eclipse整合开发Web Service之二:WSDL逆向生成服务端

一般开发Web Service,是选写服务端,再根据生成的WSDL文件生成客户端。看过一本SOA的书中说,如果实现防止不同语言一些特定的数据类型的不兼容性,推荐先定义WSDL接口,包括一输入输出参数、...

axis2 实例 web service实践

  • 2008年12月01日 12:26
  • 18.72MB
  • 下载

Axis Web Service 应用实例

  • 2009年06月15日 09:47
  • 1.99MB
  • 下载

通向架构师的道路(第十天)之Axis2 Web Service(一)

一、Axis2简介 1.1 介绍Axis2 Axis框架来自 Apache 开放源代码组织,它是基于JAVA语言的最新的 SOAP 规范(SOAP 1.2)和 SOAP withAttachmen...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Axis Session 关于Web Service Forum 用户登录 用户名 密码 的 保存
举报原因:
原因补充:

(最多只允许输入30个字)