|
老师你看,我对您的myservlet2代码做出的改动只是改了一下名字(改成了responseServlet),然后砍掉了两个灯,只剩两个灯,增加了两行声明数据库的代码,其中我最有疑问的是 那行 response.setContenType("text/text"); 为甚么我只见过 "text/html",我查了方法的文档貌似没找到“text/text”作为参数的呀?[mw_shl_code=java,true] protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String a2 = "0";
String a3 = "0";
String n = "0";
response.setContentType("text/text");
try (PrintWriter out1 = response.getWriter())
{
Connection conn = null;
Statement stmt = null;
ResultSet rs1 = null;
int s = 0;
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功加载数据库");
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/mydata?" +
"user=root&password=521314");
stmt = (Statement) conn.createStatement();
System.out.println("==");
System.out.println("查询最后一条记录...");
rs1 = stmt.executeQuery("SELECT * FROM t1 ORDER BY aid DESC LIMIT 1;");
while(rs1.next())
{
n = rs1.getString(2);
if (n.equals("1"))
{
a2 = "1";
}
else
{
a2 = "0";
}
n = rs1.getString(3);
if (n.equals("1"))
{
a3 = "1";
}
else
{
a3 = "0";
}
}
}
catch (SQLException ex)
{
System.out.println("Error in connection: " + ex.toString());
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
/* TODO output your page here. You may use following sample code. */
//out1.println("<!DOCTYPE html>");
// out1.println("<html>");
// out1.println("<head>");
// out1.println("<title>Servlet myservlet2</title>");
// out1.println("<meta charset=\"UTF-8\">");
// out1.println("</head>");
// out1.println("<body>");
out1.println("A2=" + a2 + ",A3=" + a3 );
// out1.println("</body>");
// out1.println("</html>");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
processRequest(request,response);
}
public String getServletInfo() {
return "Short description";
}// </editor-fold>[/mw_shl_code]
就这个servlet的运行结果来看,它成功显示了当前数据库里的小灯信息,然而我的板子没有收到啊。。。。
我再把我的板子的代码贴出来,也是做了改动(简化)的
[mw_shl_code=cpp,true]#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress dnserver(192, 168, 0,105);
IPAddress ip(192, 168,0,111);
EthernetClient client;
char c;
String result;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
delay(3000);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
Serial.println("Ethernet connecting...");
}
void loop() {
if (client.connect(dnserver, 80))
{
Serial.println("Connected");
client.println("POST http://192.168.0.105:80/remoteControlByYuancong/ResponseServlet HTTP/1.1");
client.println("Host:192.168.0.105");
client.println();
Serial.println(client.available());
while(client.available())
{
c = client.read();
result += c;
}
Serial.println("==");
Serial.println(result.substring(0,8));
if (result.substring(0,3).equals("A2=1"))
{
digitalWrite(2, HIGH);
}
else
{
digitalWrite(2, LOW);
}
if (result.substring(5,8).equals("A3=1"))
{
digitalWrite(3, HIGH);
}
else
{
digitalWrite(3, LOW);
}
Serial.println("end");
client.stop();
result = "";
}
else
{
Serial.println("connection failed");
}
}[/mw_shl_code]
串口监视器输出的信息是是连接成功然而输出的client.avaliable();的返回值为0.
板子什么也没收到,跪求老师解答,您的帖子是两年前的,希望您还有印象。。。
|
-
这是那一个servlet的运行结果
|