刷新从Java类获取的JSP文件中的<div>中的值

huangapple go评论70阅读模式
英文:

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:

&lt;div id=&quot;showtime&quot;&gt;
            &lt;%
                out.print(download.show[0]); &lt;---- this is public table in java class download
            %&gt;
&lt;/div&gt;

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:

&lt;script&gt;
        function dateup(){
            $.ajax({
                type: &quot;GET&quot;,
                url: &quot;${pageContext.request.contextPath}/update&quot;,
                success: function(response){
                    $(&#39;#showtime&#39;).html(response);
                    alert(&quot;success&quot;);
                },
                error: function () {
                    alert(&quot;not success&quot;);
                }
            });
        }
&lt;/script&gt;

Servlet update:

@WebServlet(name = &quot;update&quot;)
public class update extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String text = &quot;some text&quot;;

        response.setContentType(&quot;text/plain&quot;);
        response.setCharacterEncoding(&quot;UTF-8&quot;);
        response.getWriter().write(text);.

    }

}

Mapping:

&lt;servlet&gt;
        &lt;servlet-name&gt;update&lt;/servlet-name&gt;
        &lt;servlet-class&gt;servlets.update&lt;/servlet-class&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
        &lt;servlet-name&gt;update&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/update&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;

And I changed &lt;div&gt; to:
&lt;div id=&quot;showtime&quot;&gt;&lt;/div&gt;

Button to trigger function:
&lt;button onclick=&quot;dateup()&quot;&gt;press here&lt;/button&gt;

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."

huangapple
  • 本文由 发表于 2020年9月30日 21:49:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64139020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定