⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 locationimpl.java

📁 本书通过实例介绍了嵌入式编程的方法与技巧书中例子具有实用性
💻 JAVA
字号:
import java.net.*;
import java.util.*;
import java.io.*;
import hp.chaiserver.*;
import java.sql.*;

public class LocationImpl implements ILocation, IChaiServer {

	public void initWorkerArgs(String name, IDaemon daemon, ArgVector args) {
		System.out.println("What is sysLogComand\t" + Utils.getSysLogCmd() + "\n");
	}

    // locationRetrieve is an implementation of the method defined in ILocation
    public void locationRetrieve(String retFormat, ArgVector args, Environment env) {
        // The next two lines obtain utility objects from the environment
        // retStat is used to return status information
        // html is used to stream HTML data back to client
        HTMLStatus retStat = env.getErrResponder();
        HTMLHelper html = (HTMLHelper) env.getHTMLHelper();

		// The following lines extract an argument from the ArgVector
		// The LocationString argument contains the String entered into the
		// form.
		// If LocationString is null then an error is sent to the client
		String latitude = args.getArg("latitude");
		String longitude = args.getArg("longitude");
		String service = args.getArg("service");

		System.out.println("Latitude: "+latitude);
		System.out.println("Longitude: "+longitude);
		System.out.println("Service: "+service);

		if((latitude == null) || (longitude == null) || (service == null)) {
				retStat.sendError(400, "Not enough arguments");
				return;
		}

		// The following lines check the return format specified by
		// client. This implementation supports HTML and Text.
		// An error is sent to the client if return format is not supported
           try {
		if(retFormat.equalsIgnoreCase("html")) {
			// startHTMLDoc sends the Header information to the client
            html.startHTMLDoc();
            // send a <HR> Tag
            html.startTag("HR");
            // send a <CENTER> Tag
            html.startTag("CENTER");
            // send a </CENTER> Tag
            html.endTag("CENTER");
   			try{
		       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		       java.sql.Connection conn = DriverManager.getConnection("jdbc:odbc:test2000");
		       Statement st = conn.createStatement();
		       ResultSet rs1= st.executeQuery("select city from lalvalues where latitude = "+latitude+" and longitude = "+longitude);
		       String city=null;
		       int flag=0;
		       while(rs1.next())
		       {
				   city = rs1.getString(1);
				   flag=1;
			   }

			   rs1.close();
			   html.startTag("CENTER");
			   html.appendSimpleHTMLPara("Location Services", "H2");
			   html.endTag("CENTER");
			   ResultSet rs=null;
			   if(flag==0){
			      html.appendSimpleHTMLPara("Unable to locate", "H2");
			   }  else {
			   html.appendSimpleHTMLPara("You are located  in "+ city, "H2");
			   if (service.equalsIgnoreCase("Hospital"))
			   {
				  rs=st.executeQuery("select * from hospitals where city like '"+city+"'");
				  html.appendSimpleHTMLPara("Nearest Hospitals: ", "H3");
			   }
			   else if (service.equals("Restaurant"))
			   {
				   rs=st.executeQuery("select * from restaurants where city like '"+city+"'");
				   html.appendSimpleHTMLPara("Nearest Restaurants: ", "H3");
			   }

   			   while(rs.next())
				{
					String sname = rs.getString(2);
					String location = rs.getString(3);
					html.appendSimpleHTMLPara(sname+" at "+location, "H3");
					html.startNewHTMLParagraph();
				}
			   rs.close();
		       st.close();
		       conn.close();
			}
			}catch(Exception e) { e.printStackTrace(); }


            // send a <HR> Tag
            html.startTag("HR");
            // send a sequence of tags to create a link to
            // this server's homepage
            html.sendHomeLink();
            // send the end of document tags
            html.endHTMLDoc();
		} else if(retFormat.equalsIgnoreCase("txt")) {
            // send the MIME header for a text document and
            // append the contents of LocationString to the document
            html.sendText(service);
		} else {
            //retStat.send400("Return type " + retFormat + " not supported");
            retStat.sendError(400, "Return type" + retFormat + "not supported");
		}
            }catch(IOException IO){}
	}

     // homePage is an implementation of a method declared in hp.chaiserver.IChaiServer
     public void homePage(OutputStream out, Environment env){
        // get HTMLHelper object from Environment. Used for streaming HTML
        HTMLHelper html = (HTMLHelper) env.getHTMLHelper();
		//System.out.println("What is sysLogComand\t" + Utils.getSysLogCmd() + "\n");
      try {
        // send HTML document header
        html.startHTMLDoc();
        // send <CENTER> Tag
        html.startTag("CENTER");
        // send Heading String
        //html.appendSimpleHTMLPara("Location Object", "H1");
        html.appendSimpleHTMLPara("Location Services", "H1");
        // send </CENTER> Tag
        html.endTag("CENTER");
        // send <HR> Tag
        html.startTag("HR");
        // send Tag sequence to start a form
        html.startForm("GET", "/this.location");
        // send <P> Tag
        html.startNewHTMLParagraph();
        // add Hidden field called locationRetrieve
        // Note this represents the Method name to be
        // called when the form is submitted. See the
        // locationRetrieve method above.

        html.addHiddenField("locationRetrieve", "html"); // Method and return type

        html.appendSimpleHTMLPara("Latitude: ", "H3");
        html.addTextField("latitude", 8, null);
        html.startNewHTMLParagraph();
        html.appendSimpleHTMLPara("Longitude: ", "H3");
        html.addTextField("longitude", 8, null);
        html.startNewHTMLParagraph();
        html.appendSimpleHTMLPara("Recommend a ", "H3");
        /*html.startTag("SELECT");
		html.addSelectOption("service","Hospital",true);
		html.addSelectOption("service","Restaurant",false);
		html.endTag("SELECT"); */

		html.startSelect("service",1,false);
		html.addSelectOption("Hospital","Hospital",true);
		html.addSelectOption("Restaurant","Restaurant",false);
		html.endSelect();

        // send <P> Tag
        html.startNewHTMLParagraph();
        // add a Submit button and reset button
        html.addSubmitButton("Submit");
        html.addResetButton();
        // send the </FORM> tag
        html.endForm();
        // send <HR> Tag
        html.startTag("HR");
        // send a sequence of Tags for a link to
        // the home page of the local server

		html.sendHomeLink(); // send end of document Tags
		html.endHTMLDoc();
        }catch (IOException io){}
      }

	// Export is an implementation of a method declared in hp.chaiserver.IChaiServer
	public void exportMethods(OutputStream out, String str, Environment env) {
		// get HTMLHelper object from Environment. Used for streaming HTML
		HTMLHelper html = (HTMLHelper) env.getHTMLHelper();
		if(str.equalsIgnoreCase("html")) {
                  try {
			// send HTML document header
			html.startHTMLDoc();
			// send <CENTER> Tag
			html.startTag("CENTER");
			// send Heading String
			html.appendSimpleHTMLPara("Location ChaiService", "H1");
			// send </CENTER> Tag
			html.endTag("CENTER");
			// send <HR> Tag
			html.startTag("HR");
			html.appendSimpleHTMLPara("Method support: locationRetrieve", "H3");
			html.appendSimpleHTMLPara("Argument: LocationString", "H3");
			html.sendHomeLink(); // send end of document Tags
			html.endHTMLDoc();
                     }catch(IOException io) {}
		} else if(str.equalsIgnoreCase("txt")) {
                      try {
			html.sendText("Method: locationRetrieve\nArgument: LocationString");
                      } catch(IOException io) {}
		} else {
			HTMLStatus retStat = env.getErrResponder();
			retStat.sendError(400);
		}
     }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -