如何使用AJAX分隔servlet响应字符串

时间:2014-04-18 04:10:54

标签: java javascript jquery ajax servlets

我想处理多个Ajax请求和servlet响应。 首先,当我第一次加载我的网页时,我首先加载1个请求和1个响应它工作正常,但我现在卡住了,当我想更新我的网页上的2个单独元素时(例如我的实际情况)当网页首次加载时,灯状态和我的滑块值)。我可以看到网页何时加载显示我的servlet响应的ALERT(例如on25),但我无法弄清楚如何将这两个字符串分开,以便我的ajax代码可以刷新我的按钮和我的滑块。 谢谢你的帮助。

AJAX代码:

//Living room Server $POST
$(document).ready(function() {
    //Request to the server and refresh the value of the slider with the returned data
    $.ajax({
        type : "GET",
        url : "MyServlet",
        success : function(data) {
            var tDimmer = data;
            $("#tDimmer").val(tDimmer);
            $("#tDimmer").slider("refresh");
            alert("Dimmer value= "+data);
        }
    });

    $.ajax({
        type : "GET",
        url : "MyServlet",
        success : function(data) {
            var tlight = data;
            $("#tLight").val(tlight);
            $("#tLight").slider("refresh");
        }
    });

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

/*$(document).ready(function(){

 state = $("#livingLight").val();
 $('#inpshow').text(state.toString());
 $(document).ready(function(){
 $("#slider").change(function() {
 sendSlider();
 });

 $("#toggle").change(function() {
 sendToggle();
 });
 });    
 function sendToggle(){
 var light = $("#toggle").val();
 $.ajax({
 type: "POST",
 url: "MyServlet",
 data: {toggle : light}
 });
 };

 function sendSlider(){
 var dimmer = $("#slider").val();
 $.ajax({
 type: "POST",
 url: "MyServlet",
 data: {slider : dimmer}
 });
 }});*/

Servlet代码:

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;

    String LightState = "on";
    String DimmerValue = "25";

    public MyServlet() {
                super();

    }


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

        PrintWriter dimmer = response.getWriter();
        dimmer.write(DimmerValue+LightState);
    }


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

        String tValue = request.getParameter("mLivingDimmer");
        DimmerValue = tValue;
        if( tValue !=null)
        {

        System.out.println("Dimmer % = "+tValue);   
        }
        String tState = request.getParameter("mLivingLight");
        LightState = tState;
        if( tState !=null)
        {

        System.out.println("Light state = "+tState);    
        }
    }
}

0 个答案:

没有答案