日期:2014-05-17  浏览次数:20874 次

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 ServletExceptionIOException {
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();