简体   繁体   中英

Returning a value from servlet to jsp page using ajax

First thanks for spending time on my question.

Here what I wanna do: When my jsp page load, I want to get a value from my servlet to set the actual state of my button and the actual state of my slider value. and after that is updated on my page I would like to change its value. I can already pass the JSP page value to my servlet but I'm kinda stuck on passing value from the servlet to the jsp page.

Here's some code to help

Regards

JSP FILE

<!--<!--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 CODE

//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 CODE:

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() prints to the console, ie to the console of Tomcat (or whatever your servlet container is) if you started it on a command prompt or by clicking a batch file. You want to print to the response. So you need:

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

Then in your Ajax, you need a success function, in which you will parse the response that came back from the server using string manipulations:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM