如何通过JSP去访问一个需要登陆的页面啊

lanyuhan 2004-08-27 05:50:16
比如我要在我的页面里写一个连接,连到一个页面然后自动输入用户名密码登陆,我该怎么做呀?

谢谢,偶没分了,大家海涵啊,5555
...全文
85 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
mbl 2004-08-27
  • 打赏
  • 举报
回复
帮你顶
liusoft 2004-08-27
  • 打赏
  • 举报
回复
<%@ page language="java" contentType="text/html;charset=GBK" %>

<%@ page import="java.util.*" %>
<%@ page import="org.apache.commons.lang.*" %>
<%@ page import="org.apache.commons.httpclient.*" %>
<%@ page import="org.apache.commons.httpclient.methods.*" %>

<%

HttpClient client = (HttpClient)session.getAttribute("org.apache.commons.httpclient.HttpClient");
if(client==null){
client = new HttpClient();
//使用POST方法登录
PostMethod post = new PostMethod(登陆页面地址);
post.addParameter(new NameValuePair("user","用户名"));
post.addParameter(new NameValuePair("pass","口令"));
client.executeMethod(post);
post.releaseConnection();
session.setAttribute("org.apache.commons.httpclient.HttpClient",client);
}
//打印服务器返回的状态

PostMethod get = new PostMethod("登陆后的页面"); client.executeMethod(get);
String body = get.getResponseBodyAsString();
response.getWriter().print(new String(body.getBytes("8859_1")));
get.releaseConnection();

//释放连接
%>

http://www.javayou.com

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧