想将变量传递到jsp页面里。

yslinfeng_2015 2015-04-27 04:06:13

public void MySerialPort() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}

public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
}




public void serialEvent(SerialPortEvent event) {


switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[5]; //30个字节所以后面会有很多0 因为是空的
int getLength = 0;

try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
if(numBytes != 0){
getLength = numBytes; //numBytes是输入流接收到的字节数。
//getLength是我定义的变量
System.out.println(numBytes);

}

}

String str = new String(readBuffer);

if(getLength < 5 && times < 2){

if(times == 0){
str2 = toHexString(str,getLength);
times++;
}else if(times == 1){
str3 = toHexString(str,getLength);
times = 0;
str2 = str2 + str3;
System.out.println(str2);
// request.setAttribute("userList",str2);
}

}else if(getLength == 5) {
str2 = toHexString(str,getLength);
System.out.println(str2);
// request.setAttribute("userList",str2);
}



} catch (IOException e) {}
break;
}
}


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



portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
System.out.println("---------------"+portId.getName());
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {

if (portId.getName().equals("COM4")) {
MySerialPort reader = new MySerialPort();
reader.MySerialPort();

//reader.inputStream.toString();
System.out.println("---------OK");
response.sendRedirect("/ttt/index.jsp");
// RequestDispatcher dispatcher = request.getRequestDispatcher("/ttt/index.jsp");
System.out.println("lllllllllllllllllll");
}

}
}

}




我写了一个servlet,用tomcat运行,打开浏览器可以自动运行。也就是可以单纯打印串口数据(dopost里运行的MySerialPort方法执行了)。
但是我想把数据传递到index.jsp页面里。 在 public void serialEvent(SerialPortEvent event) 将数据进行传递,但是没有
request.setAttribute("userList",str2);这样的方法让我操作。因为serialEvent方法里没有request这个参数。不知道应该怎么处理?


...全文
56 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,639

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧