2016-04-26 12 views
-1

サーブレットからページリダイレクトを作成しようとしていますが、動作しません。なぜ誰かが助けてくれれば助かります。何時間も検索していましたが、結果は得られませんでした。Java EEサーブレットページのリダイレクト+コントローラへのデータの送信

JSコード

$("#login_form_submit").click(function(){ 
var form = $('#login_form'); 
    $.ajax({ 
      type: "Post", 
      url: "AccountServlet", 
      data: form.serialize(), 
      dataType: "json", 

      //if received a response from the server 
      success: function(data, textStatus, jqXHR) { 
       if(data.success){ 
        $("#ajaxResponse").html(""); 
        console.log(data); 

        if (data.User.role == 1){ 
         main.user = data.User; 
         //window.location.href = "Student_home.jsp"; 
        } 
       } 
       //display error message 
       else { 
        $("#ajaxResponse").html("<div><b>Something goes wrong check your email or password</b></div>"); 

       } 
      }, 

      //If there was no response from the server 
      error: function(jqXHR, textStatus, errorThrown){ 
       $("#ajaxResponse").html("<div><b>Something goes wrong check your email or password</b></div>"); 
       $("#ajaxResponse").html(jqXHR.responseText); 
      }, 

      //capture the request before it was sent to server 
      beforeSend: function(jqXHR, settings){ 
       //disable the button until we get the response 
       $('#login_form_submit').attr("disabled", true); 
      }, 

      //this is called after the response or error functions are finsihed 
      //so that we can take some action 
      complete: function(jqXHR, textStatus){ 
       //enable the button 
       $('#login_form_submit').attr("disabled", false); 
      } 

     });   

}); 

サーブレットコード

@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 

    PrintWriter out = response.getWriter(); 
    response.setContentType("text/html"); 
    response.setHeader("Cache-control", "no-cache, no-store"); 
    response.setHeader("Pragma", "no-cache"); 
    response.setHeader("Expires", "-1"); 

    response.setHeader("Access-Control-Allow-Origin", "*"); 
    response.setHeader("Access-Control-Allow-Methods", "POST"); 
    response.setHeader("Access-Control-Allow-Headers", "Content-Type"); 
    response.setHeader("Access-Control-Max-Age", "86400"); 


    String userName = request.getParameter("userName"); 
    String password = request.getParameter("password"); 
    DatabaseWrapper context = new DatabaseWrapper(); 
    User user = context.Check_login(userName, password); 
    session.current_user = user; 
    Gson gson = new Gson(); 
    JsonObject myObj = new JsonObject(); 

    if (user == null){ 
     myObj.addProperty("success", false); 
     out.println(myObj.toString()); 
    }else if (user instanceof Student){ 
     JsonElement userObject = gson.toJsonTree((Student)user); 
     myObj.addProperty("success", true); 
     myObj.add("User", userObject); 
     out.println(myObj.toString()); 

     out.close(); 
     //response.setStatus(HttpServletResponse.SC_FOUND); 
     response.sendRedirect("Student_home.jsp"); 
     return; 


    } 

私は

RequestDispatcher dispatcher = request.getRequestDispatcher("/Student_home.jsp"); 
     dispatcher.forward(request, response); 

を使用することも試みたが、それは、任意の提案を成功しませんでした!

javascriptを使用すると問題はありませんが、サーブレットからリダイレクトする必要があります。

私はJavaScriptで

window.location.href = "Student_home.jsp"; 

Guysは、私は答えを見つけたが、何らかの理由で、私は解決策でそれを投稿することができません!このラインを使用

問題は、私はajax呼び出しをリダイレクトしているので、それを防ぐ方法はありません。考えられるのはjqueryの成功の応答です。フォーム要素を作成して自然な方法で送信する別の呼び出しを使用してリダイレクトします

私の解決策:

function Redirect (servlet , method_type){ 
var form = $(document.createElement('form')); 
$(form).attr("action", servlet); 
$(form).attr("method", method_type); 
$(form).css("display", "none"); 
form.appendTo(document.body); 
$(form).submit(); 

};

おかげさまで一人一人。

+2

ajaxを使用する場合は、javascriptがリダイレクトを処理する必要があります。ブラウザは「通常の」送信を行う場合にのみリダイレクトを処理します。 – BevynQ

+0

メインリクエストではなく、単にajaxリクエストをリダイレクトしています。サーブレットがJSONレスポンスとして目的のリダイレクトURLを返すようにしてから、JavaScriptがリダイレクトするようにします。 – BalusC

+0

リダイレクトでajaxコールから返された日付を保持する方法私はこれを試したが、リダイレクト後にデータが消えてしまったからです。 –

答えて

0

close()の後に応答することはできません。

ヘッダーに関しては、応答がコミットされた後に書き込むことさえできません。あなたの場合、リダイレクトしている場合は、応答に何も書き込まないでください。

同じことがRequestDispatcher.forward()に適用されます。転送する前に応答に何も書き込まないでください。そして決して閉じないでください()。

関連する問題