Nemin
Nemin

Reputation: 2037

How to compile servlet on remote server?

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

Answers (1)

Cyril Gavrailov
Cyril Gavrailov

Reputation: 466

answers one by one:

  1. 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).

  2. 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).

  3. It is not a good idea to transporting your war files manually to the server (with putty, remote desktop for windows, etc.), because you might someday need to deploy on a server to which you do not have remote access. For tomcat you can use the tomcat deploy manager (for remote hosts) or you IDE (for your localhost as shown in the video above). Here's the documentation for tomcat deployment. However, the dir you're looking home is I think "webapps" in the tomcat installation dir.
  4. For the changes in the web.xml... it depends if you're using servlet version less than 3.0, then yes, you should describe your servlet in your web.xml (the most IDEs do that for you, so I would propose to better first open your web.xml and see whether your servlet hasn't already been described in there). If you're using servlet 3.0 all you have to do is to annotate your servlet with the @WebServlet annotation.

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

Related Questions