L
L
lopatin_ay2015-07-19 11:17:58
Java
lopatin_ay, 2015-07-19 11:17:58

How to run servlet on vps?

Hello, I decided that learning Java would be more productive and enjoyable on a VPS, and I ran into a problem that I can’t find a solution to. In fact, the problem is not complicated, but I'm missing something, help, please.

There is a VPS (ubuntu), it has tomcat7 and nginx. The /var/lib/tomcat7/webapps/simple folder contains the project itself. /var/lib/tomcat7/webapps/simple/WEB-INF/classes/Servlets contains the file

MyServlet.java after clicking on it, it redirects to the myServlet page with a 404 error. On the local machine (win8) everything works. Do I need to compile anything at all before running it on the server? Maybe I misconfigured something?

servlet listing MyServlet.java

package Servlets;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
 
public class MyServlet extends HttpServlet {
    public void doGet(HttpServletResponse response,
                      HttpServletRequest) throws IOException, ServletException {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        PrintWriter out = response.getWriter();
        out.write("<h1>Hello!</h1>");
        out.flush();
        out.close();
    }
}


web.xml listing:
<servlet>
     <servlet-name>myServlet</servlet-name>
     <servlet-class>Servlets.MyServlet</servlet-class>
 </servlet>
<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/myservlet</url-pattern>
</servlet-mapping>


upd: I found a description of Java Server-Side Programming , everything worked, it remains to deal with nginx, for some reason it does not want to work with servlets.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
lopatin_ay, 2015-07-19
@lopatin_ay

Everything worked.
Perhaps someone will need to configure nginx to work with apache and tomcat servers... ;
And a description of the creation of the Java Server-Side Programming project

T
Timur, 2015-07-19
@timych

I have a suspicion that you do not know about WAR archives.
WAR
stackoverflow.com/questions/5108019/how-to-make-wa...
stackoverflow.com/questions/5109112/how-to-deploy-...
PS: .java files are not executable - they are just a text file on the basis of which the executable code is compiled - files with the .class extension.
Yes, and packages are usually called with a small letter.

E
Eugene, 2015-07-19
@zolt85

Yes, here you need to learn the basics first, and then write servlets.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question