java - servlets 连接和在 tomcat 上运行

标签 java tomcat servlets

如何使用此路径:"file:///E:/​​apache-tomcat-7.0.10/webapps/examples/WEB-INF/match.html"?这是正确的吗?

这是我的 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");
            }
        }
    }
}

最佳答案

How can I use this path: "file:///E:/apache-tomcat-7.0.10/webapps/examples/WEB-INF/match.html" ? Is this correct?

你的意思是,如何通过网络浏览器访问它?不,那条路不对。 Tomcat 只监听 HTTP 请求。正常启动后,它默认监听 http://localhost:8080。 .所有 webapps 都有自己的上下文名称,默认为 webapp 文件夹名称。所以你的 webapp 可以通过 http://localhost:8080/examples 访问.但是,不能直接访问 /WEB-INF 文件夹中的文件。您需要将 match.html 上移一层,在 /examples 文件夹中。然后你就可以通过 http://localhost:8080/examples/match.html 访问它了.


与具体问题无关,将连接和语句声明为 servlet 的实例变量是一种糟糕的做法。这不是线程安全的。您需要在方法 block 内声明它们,就在 try 语句之前。您的 HTML 也有一些语法错误。使用 http://validator.w3.org了解他们。最后,在 servlet 中发出原始 HTML 也是一种不好的做法,通常使用 JSP。但也许你目前只是在学习。只是想让你知道 :) 已经使用准备好的语句并在 finally 中关闭连接对于初学者来说非常好。

关于学习 JSP/Servlets,我建议也阅读我们的信息/wiki 页面。

关于java - servlets 连接和在 tomcat 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5508005/

相关文章:

java - 为什么这个循环只运行一次,而不再要求新的入口?

java - 什么是有用的抽象/契约来协助 Builder 模式构建 MVC UI?

java.lang.ClassCastException : List cannot be cast to java. lang.Comparable

java - Tomcat 未运行, "eval:/usr/lib/jvm/java-8-openjdk/bin/java: not found"

java - Hook 我的 Vaadin Web 应用程序的启动和停止吗?

redirect - 从Servlet到JSP的response.sendRedirect()似乎不起作用

java - 一个 native 方法可以从另一个 native 方法调用吗?

search - 如何防止 robots.txt 从暂存环境传递到生产环境?

java - 如何在不使用java停止tomcat的情况下清理数据库

tomcat - 如果缺少配置参数,则阻止 Java Servlet 启动