2011-04-01 68 views
0

我該如何使用此路徑:"file:///E:/apache-tomcat-7.0.10/webapps/examples/WEB-INF/match.html"?它是否正確?servlets連接和在tomcat上運行

這是我的html文件:

<html> 
    <form method=post action="../classes/match1"> 
    <body bgcolor="powderblue"> 
     <center><h1>MATCH</h1> 
     <hr/> 

     MATCH NO   <input type="text" name="op1"/><br/><pre> 
     DATE    <input type="text" name="op2"/><br/><pre> 
     CITY   <input type="text" name="op3"/><br/><pre> 
     TEAM1   <input type="text" name="op4"/><br/><pre> 
     TEAM2   <input type="text" name="op5"/><br/><pre> 
     STADIUM   <input type="text" name="op6"/><br/><pre> 
     WINNER   <input type="text" name="op7"/><br/><pre> 
     MAN OF THE MATCH <input type="text" name="op8"/><br/><pre> 
     <input type="submit" value="submit"/>&nbsp 
     <input type="reset" value="reset"/> 
    </body> 
</html> 

我的servlet代碼:

import javax.servlet.http.HttpServlet; 
import java.io.*; 
import java.util.*; 
import java.sql.*; 
import java.sql.Date.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
public class match1 extends HttpServlet { 
    Connection con; 
    PreparedStatement pst; 

    public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException { 
     try { 
      PrintWriter out=res.getWriter(); 
      con=null; 
      pst=null; 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      con=DriverManager.getConnection("jdbc:odbc:gan","scott","tiger"); 
      int count=0; 
      String op1=req.getParameter("op1"); 
      String op2=req.getParameter("op2"); 
      String op3=req.getParameter("op3"); 
      String op4=req.getParameter("op4"); 
      String op5=req.getParameter("op5"); 
      String op6=req.getParameter("op6"); 
      String op7=req.getParameter("op7"); 
      String op8=req.getParameter("op8"); 
      pst=con.prepareStatement("insert into matchdetails values(?,?,?,?,?,?,?,?)"); 
      pst.setString(1,op1); 
      pst.setString(2,op2); 
      pst.setString(3,op3); 
      pst.setString(4,op4); 
      pst.setString(5,op5); 
      pst.setString(6,op6); 
      pst.setString(7,op7); 
      pst.setString(8,op8); 
      out.println("<html><center><body>matched</body></center></html>"); 
      int count1=pst.executeUpdate(); 
      if(count1==0) { 
       out.println("<html><center><body>ENTER ALL FIELD VALUES</body></center></html>"); 
      } else { 
       out.println("<html><center><body>INSERTION SUCCESFUL</body></center></html>"); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (con!=null) con.close(); 
      } catch(Exception e) { 
       System.out.println("error"); 
      } 
     } 
    } 
} 

回答

2

我如何使用這個路徑:「文件:/// E:/ Apache的tomcat- 7.0.10/webapps/examples/WEB-INF/match.html「?它是否正確?

你的意思是,如何通過網頁瀏覽器訪問它?不,那條路是不正確的。 Tomcat僅偵聽HTTP請求。正確啓動時,它默認在http://localhost:8080上偵聽。所有webapps都獲得自己的上下文名稱,默認爲webapp文件夾名稱。所以你的webapp可以通過http://localhost:8080/examples訪問。但是,/WEB-INF文件夾中的文件不能直接訪問。您需要將match.html一級移到/examples文件夾中。然後你可以通過http://localhost:8080/examples/match.html訪問它。


無關的具體問題,聲明連接和語句作爲servlet的實例變量是一個貧窮的做法。這不是線程安全的。您需要在方法塊內聲明它們,就在try聲明之前。你的HTML也有一些語法錯誤。使用http://validator.w3.org瞭解它們。最後,在servlet中發佈原始的HTML也是一個不好的習慣,你通常使用JSP來進行。但是,也許你現在只是在學習。只是爲了讓你知道:)已經使用preparedstatements並最終關閉連接對於初學者非常有用。

至於學習JSP/Servlets,我也建議閱讀我們的info/wiki頁面。

0
  • 首先,你的HTML需要一點 清潔劑(見下文)。
  • 二,如果您要 想要match.html,請不要在表單動作中使用match1
  • 三,將 match.html文件置於war 文件夾下,而不是web-inf
  • 四,你需要 設置配置文件爲web.xml

我甚至沒有看到servlet類,但我會建議先測試一下System.out.println()輸出以確保它甚至被執行。

<html> 
    <body bgcolor="powderblue"> 
     <center> 
      <h1>MATCH</h1> 
      <form method="post" action="/match.html"> 
       <pre>MATCH NO   <input type="text" name="op1"/></pre> 
       <pre>DATE    <input type="text" name="op2"/></pre> 
       <pre>CITY    <input type="text" name="op3"/></pre> 
       <pre>TEAM1   <input type="text" name="op4"/></pre> 
       <pre>TEAM2   <input type="text" name="op5"/></pre> 
       <pre>STADIUM   <input type="text" name="op6"/></pre> 
       <pre>WINNER   <input type="text" name="op7"/></pre> 
       <pre>MAN OF THE MATCH <input type="text" name="op8"/></pre> 
       <pre><input type="submit" value="submit"/></pre> 
       <input type="reset" value="reset"/> 
      </form> 
     </center> 
    </body> 
</html>