2011-05-10 2 views
0

전달 페이지에 오류가 나는 앵커JSP :이 질문은 이전에 관련

<a href="#" id="Email">send email</a> 

그것이 내가 요청을 처리

서블릿에서
$("#Email").click(function() { 

    var option={ 
     "action":"sendEmail" 
    }; 
    $.getJSON('StudentManagementServlet',option, function(hasEmail) { 
     if(hasEmail == false){ 
      // //view form to let user enter his email 
      $("#CommViaEmail").fadeIn("normal"); 
     } 
    }); 
}); 

JSON을 사용하여 서블릿을 호출을 통해 클릭 할 때

if (action != null && action.equals("sendEmail")) { 

    //open connection to db 
    con.setAutoCommit(false); 
    String email = ParentManagement.getParentEmail(con, stdNo); 
    if (email != null) { 
     String commResult = createAccountAndSendEmail(con, parentNo, email); 
     request.setAttribute("result", commResult); 
     request.setAttribute("incp", "ResultPage"); 
     RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp"); 
     dispatcher.forward(request, response); //doesn't make forward!!!!! 
     System.out.println(">>send email DONE!!"); 
     con.commit(); 
     return; 
    } else { 
     boolean hasEmail = false; 
     String json = new Gson().toJson(hasEmail); 
     response.setContentType("application/json"); 
     response.setCharacterEncoding("UTF-8"); 
     response.getWriter().write(json); 

    } 
} 

여기에 문제는 사용자가 이메일을 가지고 있다면 이메일을 보내지 만 결과 페이지로 전달하지 않기를 요청하는 것입니다. e, 심지어 print 문이 인쇄됩니다 "System.out.println (">> 보내기 전자 메일 보내기 !! ");" ??

답변

1

JS/jQuery가 해당 작업을 수행하도록해야합니다. 서블릿이 JSON 결과로 true를 작성하고 JS에

if (hasEmail) { 
    window.location = 'index.jsp'; 
} else { 
    $("#CommViaEmail").fadeIn("normal"); //view form to let user enter his email 
} 

을하거나 URL 자신을 제어 할 때,

..., function(data) { 
    if (data.hasEmail) { 
     window.location = data.location; 
    } else { 
     $("#CommViaEmail").fadeIn("normal"); //view form to let user enter his email 
    } 
} 
+0

@BalusC, 요청과 함께 전달할 속성이 많습니다. 어떻게 javascript를 사용하여 전달할 수 있습니까? – palAlaa

+0

대신 요청 매개 변수로 전달할 수 있습니다. 결국, 왜 다른 페이지로 넘어가시겠습니까? 같은 페이지에서 바로 성공 메시지를 보여줄 필요가 없습니다. – BalusC

+0

@BalusC, 선생님은 웹 응용 프로그램에서 다른 페이지에 표시된 결과를 한 번 말해 주셨습니다. – palAlaa

0

클라이언트로부터 AJAX 요청을 보내고 있으며 서버 측에서 해당 요청을 '전달'하려고합니다.

AJAX 요청 DONT 페이지를 새로 고칩니다. hasEmail 변수는 입니다. javascript 함수index.jsp의 HTML을 포함하는 문자열입니다.

+0

로 JSON

Map<String, Object> data = new HashMap<String, Object>(); data.put("hasEmail", true); data.put("location", "index.jsp"); // ... 

에 새 위치를 추가 할 수 hasEmail 변수는 문자열이 아닌 부울 변수를 가지고 있습니다.이 변수는 사용자에게 전자 메일이 없다는 것을 알려주므로 해당 전자가 자신의 전자 메일을 추가하도록 표시합니다. – palAlaa

+0

서버 측 코드의 "else"블록이 실행될 때만 false가됩니다. 그렇지 않으면 index.jsp의 html 문자열을 포함하게됩니다. –

+0

index.jsp를 hasEmail 변수로 설정 한 부분을 찾을 수 없습니다! – palAlaa

관련 문제