簡體   English   中英

使用ajax從servlet返回值到jsp頁面

[英]Returning a value from servlet to jsp page using ajax

首先感謝您花時間在我的問題上。

我想做的是:加載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()
            });
        });
    });
});

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;
    //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"));    
    }
}

如果在命令提示符下或通過單擊批處理文件啟動, 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
             },
             ....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM