首先感谢您花时间在我的问题上。
这里我想做的事情:当我的jsp页面加载时,我想从我的servlet中获取一个值来设置我的按钮的实际状态和滑块值的实际状态。然后在我的页面上更新,我想改变它的价值。我已经可以将JSP页面值传递给我的servlet,但我仍然坚持将值从servlet传递到jsp页面。
以下是一些帮助
的代码此致
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代码
//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"));
}
}
答案 0 :(得分: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
},
....