如何使用java登录JSF页面

如何使用java登录JSF页面,java,jsf,post,httpclient,Java,Jsf,Post,Httpclient,我正在尝试登录.jsf intranet页面,以下是其中的一部分: <form method="POST" action="j_security_check" name="loginForm" id="loginForm"> <input name="j_username" type="text" class="textbox" maxlength="30"/> <input name="j_

我正在尝试登录.jsf intranet页面,以下是其中的一部分:

     <form method="POST" action="j_security_check" name="loginForm" id="loginForm">
                <input name="j_username" type="text" class="textbox" maxlength="30"/>
                <input name="j_password" type="password" class="textbox" maxlength="30"/>
                <input type=submit value="Enter" class="button">
                <input type=submit value="Exit" class="button">
    </form>

我在java上搜索并尝试过类似的内容,但没有成功,我得到了与结果相同的页面:

            HttpPost post = new HttpPost("http://xxxx/login.jsf");   

            List <NameValuePair> parameters = new ArrayList <NameValuePair>();   
            parameters.add(new BasicNameValuePair("j_username", "92232776"));   
            parameters.add(new BasicNameValuePair("j_password", "(7110oi14)")); 

            UrlEncodedFormEntity sendentity;

            sendentity = new UrlEncodedFormEntity(parameters, HTTP.UTF_8);

            post.setEntity(sendentity);    
            HttpClient client = new DefaultHttpClient();   
            HttpResponse response = client.execute(post, httpContext);   

            System.out.print(convertInputStreamToString(response.getEntity().getContent()));   
HttpPost=新的HttpPost(“http://xxxx/login.jsf");   
列表参数=新的ArrayList();
添加(新的BasicNameValuePair(“j_用户名”,“92232776”);
添加(新的BasicNameValuePair(“j_密码”,“7110oi14”);
UrlEncodedFormEntity发送实体;
sendentity=newurlencodedformentity(参数,HTTP.UTF_8);
post.setEntity(sendentity);
HttpClient=new DefaultHttpClient();
HttpResponse response=client.execute(post,httpContext);
System.out.print(convertInputStreamToString(response.getEntity().getContent());
(我正在使用httpcomponents-client-4.2)

我需要做什么才能登录到这个页面?有什么我需要做的代码按钮“发送”


谢谢大家。

登录信息存储在会话中。会话由cookies支持。您需要跨请求维护cookie。基本上,只要Cookie有效,您就需要在同一会话中的每个后续HTTP请求上返回检索到的
Set Cookie
响应头

在HttpClient中,您需要准备一个在中设置的,然后在每次调用中传递的


请注意,这个
j_security_check
login步骤与JSF没有特别的关系,它也适用于所有其他基于Javaservlet的登录表单。一旦您打算提交由
创建的真正的JSF表单,那么您需要考虑更多的因素。然后再看这个更详细的答案:虽然这是通过JSF表单以编程方式上传文件,但提交JSF表单的基本原则是相同的(必须考虑某些隐藏的输入字段等等)。

登录信息存储在会话中。会话由cookies支持。您需要跨请求维护cookie。基本上,只要Cookie有效,您就需要在同一会话中的每个后续HTTP请求上返回检索到的
Set Cookie
响应头

在HttpClient中,您需要准备一个在中设置的,然后在每次调用中传递的


请注意,这个
j_security_check
login步骤与JSF没有特别的关系,它也适用于所有其他基于Javaservlet的登录表单。一旦您打算提交由
创建的真正的JSF表单,那么您需要考虑更多的因素。接下来,请看这个更详细的答案:虽然这是通过JSF表单以编程方式上传文件,但提交JSF表单的基本原则是相同的(必须考虑某些隐藏的输入字段等等)。

为什么不告诉我们这个异常?这与登录任务完全无关。
NullPointerException
仅表示您试图访问或调用的引用是
null
。修复方法很简单:只要确保它不是
null
,或者做
if
检查
null
,然后绕过它。我得到的登录页面与答案相同,甚至在执行http请求时使用cookies。。。你知道会是什么吗?(不再是nullpointer=D)你为什么不告诉我那个异常?这与登录任务完全无关。
NullPointerException
仅表示您试图访问或调用的引用是
null
。修复方法很简单:只要确保它不是
null
,或者做
if
检查
null
,然后绕过它。我得到的登录页面与答案相同,甚至在执行http请求时使用cookies。。。你知道会是什么吗?(无更多空指针=D)
HttpClient httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
// ...

HttpResponse response1 = httpClient.execute(method1, httpContext);
// ...

HttpResponse response2 = httpClient.execute(method2, httpContext);
// ...