2014-08-07 4 views
0

해당 페이지를 호출 할 때 웹 페이지에서 일부 콘텐츠를 다시로드하려고합니다.새로 고침없이 페이지를 다시로드하는 방법

<jsp:include page ="/GetAllDetailsOfFollowing" /><br> 

jsp 페이지에서 서블릿을 호출했습니다. 그러나 웹 페이지에서는 완전히 실행되지 않습니다. 그럼 수동으로 웹 페이지를 새로 고쳐야 만 모든 문서가 표시됩니다. 이 문제를 해결하는 방법. 웹 페이지 를 다시로드 한 후이 페이지를 즉시 다시로드 JQuery와 어떤 방법이이 서블릿

protected void doGet(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException { 
     Connection con = null; 
     HttpSession session=request.getSession(); 
     String followingPerson=(String)session.getAttribute("followingPerson"); 
     String userName =(String)session.getAttribute("userName"); 
     PreparedStatement st = null; 
     List dataListDetails = new ArrayList(); 
     List dataListTweets = new ArrayList(); 
     List dataListFollwers = new ArrayList(); 
     List dataListFollowing = new ArrayList(); 
     ResultSet rsForProfile = null, rsForTweets = null, rsForFollower = null, rsForFollowing = null; 
     try { 
      con = ConnectionManager.getConnection(); 
      String sql="select * from person where user_id=\""+followingPerson+"\""; 
      //sql = "select * from person where user_id=\"ankur\""; 
      // sql="select * from person where user_id=\""+followingPerson+"\""; 
      st = con.prepareStatement(sql); 
      st.executeQuery(); 
      rsForProfile = st.getResultSet(); 

      if (rsForProfile.next()) { 
       dataListDetails.add("First Name"); 
       dataListDetails.add(rsForProfile.getString("firstName")); 
       dataListDetails.add("Full Name"); 
       dataListDetails.add(rsForProfile.getString("fullName")); 
       dataListDetails.add("User Id"); 
       dataListDetails.add(rsForProfile.getString("user_id")); 
       dataListDetails.add("Email Id"); 
       dataListDetails.add(rsForProfile.getString("email_id")); 
       dataListDetails.add("Joined At"); 
       dataListDetails.add(rsForProfile.getString("joined")); 

      } 
      request.setAttribute("dataListDetails", dataListDetails); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       rsForProfile.close(); 
       rsForTweets.close(); 
       rsForFollower.close(); 
       rsForFollowing.close(); 
       st.close(); 
       con.close(); 
      } catch (SQLException s) { 

      } 

     } 


     RequestDispatcher rd = request 
       .getRequestDispatcher("GetAllDetailOfFollowing.jsp"); 
     if (rd != null) { 
      rd.include(request, response); 
     } 

    } 

내의 doGet 방법입니다 그리고 이것은 내가

<body> 
<table border ="1" cellpadding="1" style="margin-left: 511px; margin-top: 56px;"> 

    <% 
     Iterator itr; 
     List data =(List)request.getAttribute("dataListDetails"); 
     for(itr=data.iterator();itr.hasNext();){ 
    %> 
    <tr> 
    <td width="119"><%=itr.next() %></td> 
    <td width ="168"><%=itr.next() %></td> 
    </tr> 
    <% 
     } 
     request.removeAttribute("dataListDetails"); 
    %> 
</table> 

기본적으로 JSP에서 호출 오전 내 JSP입니다 나는 jsp를 시작한 다음 다른 JSP를 포함하는 서블릿을 포함하고있다. 이 마지막 jsp는 웹 페이지에 하나의 이야기를 인쇄하지만 웹 페이지를 새로 고침하면 표시됩니다. 또한 jsp 페이지에서 scriplet을 제거하는 방법을 알려줍니다.

+0

request.getParameter("param1") 우리에게 당신이 가지고있는 코드를 보여 param1 값을 사용하는 것을 얻을 수 있습니다 서블릿이라고? –

+0

JSP로 서블릿을 포함 시켰습니다. 위의 코드를 작성했습니다. – user3894092

+0

그래서이 포함 된 페이지 ('GetAllDetailsOfFollowing')는 서블릿을 호출하고 무엇인가를 반환합니까? 우리에게 GetAllDetailsOfFollowing 페이지를 보여주십시오 –

답변

0

당신은 쉽게 사용 아약스를 수행 할 수 있습니다

은 첫째로 당신은 아약스 호출 첫 페이지로드를 확인해야합니다. 이것은 .ready 기능을 사용하여 수행 할 수 있습니다. 내가 생각하는 전부

$(document).ready(function() { 
    $.ajax({ 
    type: 'POST', 
    url: '/GetAllDetailsOfFollowing', 
    data: { 
     param1: 'some param', 
     param2: 's0me other param' 
    }, 
    beforeSend: function() { 
     //nothing needed here 
     //usually you can display a loading message 
    }, 
    success: function(data) { 
     //on success show something to the user 
     //data is the html returned from the calling page, in your case the servlet 
     $('#mainContainer').html(data); //put the html in the correct place 
    }, 
    error: function() { 
     //show some error message here 
    } 
    }); 
} 

...

(서블릿에 매개 변수 전달)

UPDATE를 즐기십시오 :

$(document).ready(function() { // Handler for .ready() called. }); 

는이 같은 서블릿 호출 할 ajax 호출을 사용하여

이것은 귀하의 JSP입니다 :

<html> 
    <head> 
    </head> 
    <body> 
    <input type="hidden" id="param1" value="<%=someObject.getSomeAttribute()%>"/> 
    <div id="mainContainer">Nothing to show yet</div> 
    </body> 
</html> 

그럼 당신은 아약스에서이 같은 서블릿에 param1를 전달할 수 있습니다 전화 :

$.ajax({ 
    type: 'POST', 
    url: '/GetAllDetailsOfFollowing', 
    data: { 
     param1: $('#param1').val() 
    }.......... 
.............. 

그리고 서블릿에서 당신은

+0

당신은 내가 html 테이블을 서블릿으로 만들고 out.print()를 수행해야한다는 것을 의미합니다. – user3894092

+0

예, 서블릿은 모든 html 콘텐츠를 출력 할 수 있으며 jquery를 사용하면 페이지에서 원하는 위치에이 출력을 배치 할 수 있습니다. 서블릿에 매개 변수를 전달할 수도 있습니다 (내 업데이트 참조) – MaVRoSCy

+0

하지만 이것은 틀린 규칙입니다. 우리는 서블릿에서 html을 사용하면 안됩니다. – user3894092

관련 문제