首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

servlet调用httpclient解决方法

2013-08-04 
servlet调用httpclient下面这个servlet怎么调用后面那个HttpClient类实现登录package org.cl.servletimpo

servlet调用httpclient
下面这个servlet怎么调用后面那个HttpClient类实现登录
package org.cl.servlet;
import java.io.IOException;
import java.net.URISyntaxException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.cl.impl.ITuserImpl;
import org.cl.vo.Tuser;
public class TuserServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK"); // 设置中文
HttpSession hs = request.getSession();
Tuser user = new Tuser();
user.setlogonName(request.getParameter("logonName"));
user.setPassword(request.getParameter("password"));
try {
ITuserImpl.login(user.getlogonName(), user.getPassword());
} catch (URISyntaxException e) {
e.printStackTrace();
}    
request.setAttribute("user", user);
RequestDispatcher rd = request.getRequestDispatcher("");
rd.forward(request, response);
}


}
被调用的HttpClinet
package org.cl.impl;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.cl.vo.Tuser;



public class ITuserImpl {

public static Tuser login(String logonName,String password) throws URISyntaxException {
String url="http://host:port/web_xml_interface/user_logon.xml?" +
"logonName="+logonName+"&password="+password+"&" + 
"platformId=010006&logonType=2&Ver=sdk_001" ;
HttpClient httpclient = new DefaultHttpClient();


List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("logonName", logonName));
qparams.add(new BasicNameValuePair("password", password));
//HttpPost post = new HttpPost("url");
Tuser requestUser = new Tuser();
requestUser.setlogonName(logonName);
requestUser.setPassword(password);
URI uri = URIUtils.createURI("http","host:port",-1,"/web_xml_interface/user_logon.xml",
    URLEncodedUtils.format(qparams, "UTF-8"), null);
HttpGet httpget =new HttpGet(url);

InputStream stream = null;
try{ 
      //HttpResponse status = httpclient.execute(post);
  HttpResponse response = httpclient.execute(httpget);
  stream = response.getEntity().getContent();
  if(stream != null)
          stream.close();
        }catch (IOException e) {
         e.printStackTrace();
        }catch (Exception e) {
         e.printStackTrace();
        } finally {
          
            httpclient.getConnectionManager().shutdown();
        }
        return requestUser;
}
} HttpClinet? Servlet URL
[解决办法]

ITuserImpl  impl = new ITuserImpl ();
impl.login();

[解决办法]
可以使用request传递,跳转的时候request还是有效的。

热点排行