Reputation: 2037
I want to upload my servlet file on my remote Apache Tomcat server and want to compile it. I wanted to know under which directory should I keep this file and how do I compile it? Should I use Putty? I am relatively new with Servlets. Here is my servlet code:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet(name= "db-connect", urlPatterns="/db-connect")
public class DBConnect extends HttpServlet{
private Connection con = null;
private PreparedStatement preparedStatement = null;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://http://localhost:3307/abc?autoReconnect=true");
preparedStatement = con.prepareStatement("SELECT * FROM Person");
ResultSet rs = preparedStatement.executeQuery();
while(rs.next())
{
String email = rs.getString("email");
System.out.print(email);
System.out.println("in");
}
}
catch(Exception e)
{
}
}
Also do I need to make changes to the web.xml?
Upvotes: 1
Views: 1403
Reputation: 466
answers one by one:
You cannot deploy the servlet only, you have to pack it into a war file then deploy it. Now, how to pack something in a war file? Well either you let the IDE you're using to do it for you (see the help of the corresponding IDE you're using) or you build it with some project management tool such as maven or ant to do it for you. I would propose to start with the exporting war file from the IDE. If you're using Eclipse, I made a fast google and found this video you can watch it(I haven't watched it end to end but it seems right).
Exporting your web project to a war file, means that your servlet classes will be compiled and packed in it, so there's no need to compile them on the server. If you need something that to be compiled automatically when invoked, you'll need a jsp it is compiled the first time when it is invoked (again no efforts from your side).
In order to better understand the servlets I would propose to read this or this(depending on the Java EE version you're using) getting started on Oracle's web site.
Hope that helps.
Upvotes: 3