簡體   English   中英

如何使用java登錄JSF頁面

[英]How to Login in a JSF page using java

我正在嘗試登錄.jsf內部網頁,這是其中的一部分:

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

(我正在使用httpcomponents-client-4.2)

登錄此頁需要做什么? 我需要用代碼按鈕“發送”嗎?

感謝大伙們。

登錄信息存儲在會話中。 會話由cookie支持。 您需要在請求中維護cookie。 基本上,只要cookie有效,您就需要在同一會話中的每個后續HTTP請求上傳回檢索到的Set-Cookie響應頭。

在HttpClient中,您需要准備一個您在HttpContext中設置的CookieStore ,然后您將在每個HttpClient#execute() CookieStore HttpClient#execute()調用中傳遞。

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);
// ...

請注意,此j_security_check登錄步驟與JSF沒有特別關系,它也適用於所有其他基於Java Servlet的登錄表單。 一旦您打算提交由<h:form>創建的真實JSF表單,那么您需要考慮更多。 然后轉到這個更詳細的答案: 我如何以編程方式將文件上傳到網站? 雖然這會以編程方式通過JSF表單上傳文件,但提交JSF表單的基本原則是相同的(必須考慮某些隱藏的輸入字段等等)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM