如何在不刷新的情况下重新加载页面

时间:2014-08-07 04:45:52

标签: jquery jsp servlets

当我打电话给那个页面时,我想在我的网页上重新加载一些内容。

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

我从jsp页面调用了一个servlet。但是在网页上并没有完全执行。然后我必须手动刷新网页,然后它将显示所有文件。  如何解决这个问题呢。在重新加载网页后,jquery中是否有任何方法可以立即重新加载此页面 这是我在servlet中的doGet方法

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

    }

这是我从jsp调用的jsp

<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时包含servlet,然后包含另一个jsp的servlet。最后一个jsp将在网页上打印一个故事,但是当我刷新网页时,它将是可见的。还要讲述如何从jsp页面中删除scriplet。

1 个答案:

答案 0 :(得分:0)

您可以使用ajax轻松完成此操作:

首先,您必须在加载第一页时进行ajax调用。这可以使用.ready函数来实现。

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

然后使用ajax调用来调用您的servlet:

$( 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(将参数传递给servlet)

这是你的JSP:

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

然后在你的ajax调用中你可以像这样将param1传递给servlet:

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

然后在您的servlet中,您可以使用param1

获取request.getParameter("param1")