使用javascript在html页面上显示servlet数据

时间:2014-04-23 07:04:43

标签: javascript html servlets

我有一个java servlet,我想用javascript在我的html页面上显示我的servlet中的数据。

这是我的servlet

        /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Sybren
 */
@WebServlet(urlPatterns = {"/stats"})
public class stats extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
//            out.println("<!DOCTYPE html>");
//            out.println("<html>");
//            out.println("<head>");
//            out.println("<title>Servlet stats</title>");            
//            out.println("</head>");
//            out.println("<body>");
//            out.println("<h1>Servlet stats at " + request.getContextPath() + "</h1>");
//            out.println("</body>");
//            out.println("</html>");
             out.println("Hello");
        } finally {            
            out.close();
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

我的html页面

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <h4>Click here to go to <a href="stats">My servlet page</a></h4>

        <h4>Servlet</h4>

        <script type="text/javascript">
    function loadStats() {

        //document.location='stats';
        window.open("stats");

    }
    loadStats.call();


</script>






    </body>
</html>

html页面在新窗口中打开servlet数据但是如何在html页面上显示servlet数据(使用javascript)?

1 个答案:

答案 0 :(得分:0)

您可以采取多种方式,但大多数人建议使用jQuery.ajax

jQuery.ajax({
    url: "stats",
}).done(function (msg) {
    jQuery('body').append(jQuery(msg));
});

如果你想要做的只是向用户显示stats,你也可以尝试<iframe src="stats"></iframe>