Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
Servlet学习笔记(三)-----用HTML页面访问Servlet

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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

 




相关文章

相关软件