1. 程式人生 > >Servlet學習筆記(三)-----用HTML頁面訪問Servlet

Servlet學習筆記(三)-----用HTML頁面訪問Servlet

1.使用者通過login.html頁面,輸入機票ID號就可以得到該航班的起飛城市和目的城市

2.login.html程式碼如下:

<HTML><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

<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

}