RE: Method Post and Get with TiniHttpServer
"Kelly Smith" <[email protected]>
| Newsgroups | gmane.comp.hardware.microcontrollers.tini |
|---|---|
| Message-ID | <[email protected]> |
Dear Doc Gloumy - Here is a simple "GET" response method, to toggle two LEDs
in a servlet.
Essential, the "if" is (That is IF I understand your "if" question,
correctly!):
if ("button1".equals(query)) {
... THEN DO SOMETHING HERE ...
Best regards, (again) Kelly Smith
/**
* Responds to HTTP GET requests.
*
* @param request the HTTPServletRequest object
* @param response the HttpServletResponse object
*
* @throws ServletException
* @throws IOException
*/
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
//The query string sent by the client tells which button was clicked on
the Web page.
String query = request.getQueryString();
// Initialize the LED states.
boolean led1On; // DSTINIm400400/TStik400 board.
boolean led1Toggled;
led1Toggled=false;
boolean led1On_TINI; // DSTINI390 board.
boolean led2On; // DSTINIm400400/TStik400 board.
boolean led2Toggled;
led2Toggled=false;
// Read the current state of the LEDs (0=on, 1=off).
// and toggle the LED named in the query string.
if ("button1".equals(query)) {
// 1.2 - Modified December 18, 2003 by Kelly Smith. Added Telnet "ON/OFF"
LED state.
System.out.print("The Red Button 1 was clicked:");
// Toggle Red LED 1.
// A logic low turns the LED 1 on.
led1On = toggle(led1); // DSTINIm400400/TStik400 board.
// A logic low turns the LED 1 off.
led1On = toggle(led1_TINI); // DSTINI390 board.
led1Toggled=true;
} else {
// Don't toggle Red LED 1.
led1Toggled=false;
led1On_TINI = (led1_TINI.readLatch() != 0);
}
if ("button2".equals(query)) {
// 1.2 - Modified December 18, 2003 by Kelly Smith. Added Telnet "ON/OFF"
LED state.
System.out.print("The Green Button 2 was clicked:");
// Toggle Green LED 2.
// A logic low turns the LED 2 on.
led2On = toggle(led2); // DSTINIm400400/TStik400 board.
led2Toggled=true;
} else {
// Don't toggle Green LED 2.
led2Toggled=false;
}
// Set the images and text to match the current LEDs' 1 state.
// If led1State is true, LED1 is off.
String led1Image;
String led1State;
// Read the current LED1 hardware state and test the On/Off state.
led1On = (led1.readLatch() == 0);
if (led1On) {
led1Image= "/red-led-on.gif";
led1State = "ON";
if (led1Toggled) {
System.out.println(" ON");
}
else { }
}
else {
led1Image = "/red-led-off.gif";
led1State = "OFF";
if (led1Toggled) {
System.out.println(" OFF");
}
else { }
}
// Set the images and text to match the current LEDs' 2 state.
// If led2State is false, LED2 is off.
String led2Image;
String led2State;
// Read the current LED2 hardware state and test the On/Off state.
led2On = (led2.readLatch() == 0);
if (led2On) {
led2Image= "/green-led-on.gif";
led2State = "ON";
if (led2Toggled) {
System.out.println(" ON");
}
else { }
}
else {
led2Image = "/green-led-off.gif";
led2State = "OFF";
if (led2Toggled) {
System.out.println(" OFF");
}
else { }
}
// Return the Web page to the client.
SendWebPage (response, led1Image, led2Image,
led1State, led2State);
} //end doGet
/**
* Toggles the specified bitPort bit and returns the new state.
*
* @param bitPort a bit that controls an LED.
*/
private static boolean toggle(BitPort bitPort) {
if (bitPort.readLatch() == 0) {
// If it's 0, set the bit to turn the LED off and return true.
bitPort.set();
return true;
} else {
// If it's 1, clear the bit to turn the LED on and return false.
bitPort.clear();
return false;
}
} // end toggle
-----Original Message-----
From: tini-admin-6tN4nzCoH/[email protected] [mailto:tini-admin-6tN4nzCoH/[email protected]]On Behalf Of
Doc Gloumy
Sent: Thursday, April 29, 2004 1:36 PM
To: tini-6tN4nzCoH/[email protected]
Subject: [TINI]Method Post and Get with TiniHttpServer
I want to pass data to a servlet, and I dot understand how I can use the
post and get method with TiniHttpServer, I use two servlet, one with the
form, and the other wich display data,
This is the first :
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class HelloServlet extends HttpServlet {
public void service( ServletRequest request,ServletResponse
response) throws ServletException, IOException {
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
out.println( "<FORM Method=\"GET\"
Action=\"http://10.10.10.81/servlet/HelloServletDeux\">" );
out.println( "Nom :<INPUT type=text size=20 name=nom><BR>" );
out.println( "Prénom :<INPUT type=text size=20
name=prenom><BR>" );
out.println( "Age :<INPUT type=text size=2 name=age><BR>" );
out.println( "<INPUT type=submit value=Envoyer></FORM>" );
}
}
and the twice :
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class HelloServletDeux extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>\n<BODY>\n" +
"<H1>Recapitulatif des informations</H1>\n" +
"<UL>\n" +
" <LI>Nom: "
+ request.getParameter("Nom") + "\n" +
" <LI>Prenom: "
+ request.getParameter("Prenom") + "\n" +
" <LI>Age: "
+ request.getParameter("Age") + "\n" +
"</UL>\n" +
"</BODY></HTML>");
}
}
The compil is ok, and I arrive to display the html but the data are
"null", is there an good method to pass data enter two servlet ?
If not, can I put these two servlet in only one ? but i dont know how to
use the send button as a condition in a if structure
PS : Thanks to Kelly Smith and Shawn for their help with TiniHttpServer,
it run good now ;)
--
/**************************************************
GARCIA Patrick alias Doc Gloumy
BTS Informatique et Reseaux pour l'Industrie et les Services
Port : 06-89-51-54-01 Tel : 05-61-07-65-58
Trésorier, electricien,
Responsable logistique et materiel de PcRézoo
www.pcrezoo.net
[email protected]
ICQ : 94469133
MSN : [email protected]
**************************************************/
_______________________________________________
TINI mailing list
TINI-6tN4nzCoH/[email protected]
To UNSUBSCRIBE, edit your profile, or see list archives:
http://lists.dalsemi.com/mailman/listinfo/tini
_______________________________________________
TINI mailing list
TINI-6tN4nzCoH/[email protected]
To UNSUBSCRIBE, edit your profile, or see list archives:
http://lists.dalsemi.com/mailman/listinfo/tini