英文:
Refresh value in <div> in jsp file taken from java class
问题
以下是您要求的翻译内容:
我对于Java和JSP文件还很新手。我有一个应用程序,它使用Java类定期从设备下载数据,并将其发送到JSP文件中的<div>:
<div id="showtime">
    <%
        out.print(download.show[0]); // 这是Java类download中的公共表格
    %>
</div>
这是我的问题:是否有任何方法可以在不刷新整个页面或重置会话超时倒计时的情况下,使用来自Java类的值刷新<div>中的值?
编辑:我尝试过一些使用AJAX的解决方案,但它不起作用,我不知道我哪里错了:
<script>
    function dateup(){
        $.ajax({
            type: "GET",
            url: "${pageContext.request.contextPath}/update",
            success: function(response){
                $('#showtime').html(response);
                alert("成功");
            },
            error: function () {
                alert("不成功");
            }
        });
    }
</script>
更新的Servlet:
@WebServlet(name = "update")
public class update extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String text = "一些文本";
        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(text);
    }
}
映射:
<servlet>
    <servlet-name>update</servlet-name>
    <servlet-class>servlets.update</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>update</servlet-name>
    <url-pattern>/update</url-pattern>
</servlet-mapping>
我将<div>更改为:
<div id="showtime"></div>
用于触发函数的按钮:
<button onclick="dateup()">点击这里</button>
但它不起作用。<div>不会更改其内容。它既不触发成功函数,也不触发错误函数。
英文:
I'm newbie in java and jsp files. I have an application which is using java class to periodically download data from device and send it to the <div> in jsp file:
<div id="showtime">
            <%
                out.print(download.show[0]); <---- this is public table in java class download
            %>
</div>
Here is my question: is there any method I can use to refresh value in <div> taken from java class without refreshing whole page or reseting session timeout countdown?
Edit: I've tried some solutions with AJAX but it doesn't work, I don't know where I made mistake:
<script>
        function dateup(){
            $.ajax({
                type: "GET",
                url: "${pageContext.request.contextPath}/update",
                success: function(response){
                    $('#showtime').html(response);
                    alert("success");
                },
                error: function () {
                    alert("not success");
                }
            });
        }
</script>
Servlet update:
@WebServlet(name = "update")
public class update extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String text = "some text";
        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(text);.
    }
}
Mapping:
<servlet>
        <servlet-name>update</servlet-name>
        <servlet-class>servlets.update</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>update</servlet-name>
        <url-pattern>/update</url-pattern>
</servlet-mapping>
And I changed <div> to:
<div id="showtime"></div>
Button to trigger function:
<button onclick="dateup()">press here</button>
But it doesn't work. <div> won't change it's context. This not trigger success function nor error one.
答案1
得分: 1
JSP是一种服务器端技术,这意味着如果您想要刷新页面,您将需要向服务器执行请求,服务器将返回新页面。无法通过常规的JSP机制仅返回页面的部分内容。
如果您只想刷新一个值,您需要使用JavaScript发起一个Ajax调用到服务器以获取所需数据,并使用这些数据重新填充该值。
英文:
"JSP is a server side technology, which means that if you want to refresh the page you will have to perform a request to the server which will return the new page. It is not possible to just return part of a page through normal JSP mechanisms.
If you want to just refresh a value you will need to use javascript to make an ajax call to the server to get the data you need, and repopulate the value with this data."
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论