使用javascript在html页面上显示servlet数据
我有一个Javaservlet,我想用javascript在html页面上显示servlet中的数据 这是我的servlet使用javascript在html页面上显示servlet数据,javascript,html,servlets,Javascript,Html,Servlets,我有一个Javaservlet,我想用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.S
/*
* 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>
单击此处转到
Servlet
函数loadStats(){
//document.location='stats';
窗口打开(“统计”);
}
loadStats.call();
html页面在一个新窗口中打开servlet数据,但是如何在html页面上显示servlet数据(使用javascript)?您可以通过多种方式来实现,但大多数推荐
如果您只想向用户显示
stats
,您也可以尝试。我正在使用。这对我很有用,没问题,很乐意帮忙。当做
jQuery.ajax({
url: "stats",
}).done(function (msg) {
jQuery('body').append(jQuery(msg));
});