1.       用户通过login.html页面,输入机票ID号就可以得到该航班的起飞城市和目的城市 
2.       login.html代码如下: 
<HTML> 
<HEAD> 
<TITLE>Welcome to the online reservation system </TITLE> 
</HEAD> 
<BODY> 
<CENTER> 
<TABLE> 
<FORM method=post action="http://127.0.0.1:8000/servletcontext/flightalias"> 
<TR> 
<TD>Your ticket number ID here </td> <td><input type=text name=numid> </TD> 
</TR> 
<TR> 
<TD>Your password here </td> <td><input type=text name=password> </TD> 
</TR> 
</TABLE> 
<CENTER> <INPUT type=submit> 
</FORM> 
</CENTER> 
</BODY> 
</HTML> 
注:servletcontext是Web Context, flightalias是Component Aliases 
  
3.       Servlet代码如下:(flight.java) 
  
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.io.*; 
  
import java.util.*; 
import java.sql.*; 
  
public class flight extends HttpServlet 
{ 
       Connection dbcon; 
       PreparedStatement s; 
       ResultSet result; 
       public void service(HttpServletRequest req, HttpServletResponse res)throws IOException 
       { 
              //连接数据库 
              try 
              {                    
                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
                     dbcon=DriverManager.getConnection("jdbc:odbc:MyDataSource","sa","");                                 
              } 
              catch(ClassNotFoundException e) 
              { 
                     System.out.println("Database driver not found"); 
                     System.out.println(e.toString()); 
           } 
              catch (Exception e) 
              { 
                     System.out.println("UNKNOWN!?"); 
              } // end catch 
  
  
           //boolean cookieFound = false; 
           boolean valuefound=false; 
  
           //Cookie thisCookie = null; 
           String numid=req.getParameter("numid"); 
           String password=req.getParameter("password");    
  
           String fromCity,toCity; 
  
           fromCity=new String(); 
           toCity=new String(); 
            
           res.setContentType("text/html"); 
           PrintWriter out = res.getWriter(); 
           /*Cookie[] cookies = req.getCookies(); 
           try 
           { 
                  for(int I=0;I<cookies.length;I++)  
                  { 
                        System.out.println("Inside for"); 
                        thisCookie = cookies[I]; 
                        if(thisCookie.getName().equals("pnrnum"))  
                            { 
                                   System.out.println("Cookie found"); 
                             cookieFound = true; 
                               break; 
                        } 
                  } 
           } 
           catch(NullPointerException e) 
           { 
                  cookieFound=false; 
           }*/ 
             try 
        { 
               s=dbcon.prepareStatement("select * from Flight where numid=?"); 
                  s.setString(1,numid); 
            result=s.executeQuery(); 
                  if (result.next()) 
               { 
                       
                   valuefound=true;                    
                            //System.out.println(result.getString(0));        
            } 
         
               else 
              {  
                valuefound=false;                 
            } //end else 
  
         } // end try 
         catch(Exception e) 
         { 
                      System.out.println(e.toString()); 
         } 
          
          if (valuefound==true) 
          { 
                
            out.println("<HTML>"); 
                    out.println("<BODY>"); 
            out.println("The booking details are as follows\n"); 
            out.println("<table>");  
        
                try 
              { 
                   try 
                            { 
                            fromCity=result.getString(2); 
                            toCity=result.getString(3); 
                     } 
                     catch (Exception e) 
                     { 
                           System.out.println("Error"); 
                                 System.out.println(e.toString()); 
                     } 
  
                     out.println("<tr>"); 
                     out.println("<td>"); 
                     out.println("From City: "); 
                     out.println("</td>"); 
                     out.println("<td>"); 
                     out.println(fromCity); 
                           out.println("</td>"); 
                     out.println("</tr>"); 
          
                     out.println("<tr>"); 
                     out.println("<td>"); 
                     out.println("To City: "); 
                     out.println("</td>"); 
                     out.println("<td>"); 
                     out.println(toCity); 
                           out.println("</td>"); 
                     out.println("</tr>"); 
  
                     } 
                     catch(Exception ev) 
                     { 
                            System.out.println("Error"); 
                     } 
       
         } // end if 
              if (valuefound==false)           
         {  
                     out.println("The number ID that you have specified does not exist. "); 
                     out.println("Please check if you have entered the correct number ID.");         
              } //end if 
         try 
         {     
                  dbcon.close(); 
         }  
              catch(Exception e) 
         { 
                 System.out.println("Error closing database"); 
         } 
   
       } // end service 
} 
  
4.启动J2ee服务器后客户端浏览器输入http://127.0.0.1:8000/servletcontext/login.html 
  
  
 
  
 
  |