【问题标题】:Returning a value from servlet to jsp page using ajax使用ajax将值从servlet返回到jsp页面
【发布时间】:2014-04-16 22:19:35
【问题描述】:

首先感谢您花时间回答我的问题。

这是我想做的:当我的 jsp 页面加载时,我想从我的 servlet 中获取一个值来设置我的按钮的实际状态和我的滑块值的实际状态。在我的页面上更新之后,我想更改它的值。我已经可以将 JSP 页面值传递给我的 servlet,但我有点坚持将值从 servlet 传递给 jsp 页面。

这里有一些代码可以帮助你

问候

JSP 文件

<!--<!--DOCTYPE html -->

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="./resources/css/jquery.mobile-1.4.2.css">
<script src="./resources/script/jquery-1.11.0.js"></script>
<script src="./resources/script/myJquery.js"></script>
<script src="./resources/script/jquery.mobile-1.4.2.js"></script>
</head>
<body>


    <!-- RDC SUB WINDOW LIVING -->
    <div data-role="page" ID="LIVING">
        <div data-role="header">
            <a href="#RDC"
                class="ui-btn ui-corner-all ui-shadow ui-icon-home ui-btn-icon-left">HOME</a>
            <h1>LIVING ROOM</h1>
        </div>

        <div data-role="main" class="ui-content">
            <div data-role="collapsible">
                <h1>CELLING: LIGHT</h1>
                <div class="containing-element">
                    <img src="./resources/images/light-on.png" alt="LIGHT-ON"
                        class="ui-li-icon"> <br> <select id="tLight"
                        name="tLight" data-role="slider">
                        <option value="off">Off</option>
                        <option value="on">On</option>
                    </select> <input type="range" name="tDimmer" id="tDimmer" value="0" min="0"
                        max="100" data-popup-enabled="true">
                </div>

            </div>
        </div>

        <div data-role="footer">
            <h1></h1>
        </div>
    </div>

AJAX 代码

//Living room Server $POST
$(document).ready(function() {

    $(function Dimmer() {
        $("#tDimmer").change(function() {
            $.post("MyServlet", {
                mLivingDimmer : $("#tDimmer").val()
            });
        });
    });
    $(function Light() {
        $("#tLight").change(function() {
            $.post("MyServlet", {
                mLivingLight : $("#tLight").val()
            });
        });
    });
});

服务代码:

package com.linux;

import java.io.IOException;
import java.io.PrintWriter;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    //GpioController gpio;
    //GpioPinDigitalOutput mLight;
    String LightState = "on";
    String DimmerValue = "25";

    public MyServlet() {
                super();

    }


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

    }


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

        System.out.println("LivingLight = "+request.getParameter("mLivingLight"));  
        System.out.println("LivingDimmer = "+request.getParameter("mLivingDimmer"));    
    }
}

【问题讨论】:

    标签: javascript jquery jsp servlets


    【解决方案1】:

    System.out.println() 打印到控制台,即打印到 Tomcat 的控制台(或任何您的 servlet 容器),如果您在命令提示符下或通过单击批处理文件启动它。您想打印到响应。所以你需要:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.write("LivingLight = "+request.getParameter("mLivingLight"));  
        out.write("LivingDimmer = "+request.getParameter("mLivingDimmer"));    
    }
    

    然后在您的 Ajax 中,您需要一个成功函数,您将在其中使用字符串操作解析从服务器返回的响应:

                 ...,
                 success : function(data)
                 {
                    //Parse data
                 },
                 ....
    

    【讨论】:

    • 我的 servlet 中使用的 System.out.println() 只是为了查看我的 servlet 正在从网页接收 POST。正如我所说的我想要做什么,当我的网页准备好时,将 LightState 和 DimmerValue 字符串返回到网页,这样我就可以在更改之前刷新按钮和 sldier bar 值。
    • 无论如何,将它们写到响应中就是你如何将它们从 servlet 传递回客户端。
    • 所以我在我的 servlet 中添加了一个响应 PrintWriter out = response.getWriter(); out.write(LightState);但我不知道如何在我的页面首次加载时发送 POST 以获取此响应。
    • 重点是我的树莓派上运行的 java 应用程序会点亮 LED,我想用一个网络应用程序来控制它们,到目前为止这部分工作但我想刷新我的网页当我打开它时,我可以获得 LED 的最后一个值
    • 它工作了,谢谢,现在我只需要找到一种方法,用返回的 DATA 更​​改我的翻转开关值谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-05-01
    • 2012-08-08
    • 2015-07-26
    • 1970-01-01
    • 2014-03-07
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多