Reputation: 1
How can I use this path: "file:///E:/apache-tomcat-7.0.10/webapps/examples/WEB-INF/match.html"
? Is this correct?
This is my html file:
<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"/> 
<input type="reset" value="reset"/>
</body>
</html>
My servlet code:
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");
}
}
}
}
Upvotes: 0
Views: 283
Reputation: 1109432
How can I use this path: "file:///E:/apache-tomcat-7.0.10/webapps/examples/WEB-INF/match.html" ? Is this correct?
You mean, how to access it by a webbrowser? No, that path is not correct. Tomcat listens on HTTP requests only. When started properly, it by default listens on http://localhost:8080. All webapps get their own context name which defaults to the webapp folder name. So your webapp is accessible by http://localhost:8080/examples. However, files in /WEB-INF
folder are not directly accessible. You need to move the match.html
one level up, in the /examples
folder. Then you'll be able to access it by http://localhost:8080/examples/match.html.
Unrelated to the concrete problem, declaring connection and statement as instance variable of the servlet is a poor practice. This is not threadsafe. You need to declare them inside the method block, right before the try
statement. Your HTML has also some syntax errors. Use http://validator.w3.org to learn about them. Finally, emitting raw HTML in servlets is also a poor practice, there you normally use JSPs for. But maybe you're currently just learning. Just to let you know :) Already using preparedstatements and closing the connection in finally is very good for a starter.
As to learning JSP/Servlets, I'd suggest to read our info/wiki pages as well.
Upvotes: 2
Reputation: 40391
match.html
, don't use match1
in the form action. match.html
file under the war
folder, not web-inf
. web.xml
configuration file.I did not even look yet at the servlet class, but I would suggest first to test some System.out.println()
output to make sure it is even executed.
<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>
Upvotes: 0