0

I created one java web project with the name first-web-application. then I created one servlet having doGet method.

@WebServlet(urlPatterns = "/login.do")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
    ------------------
    -----------------
}
}

When I am running and accessing this application it is coming

http://localhost:8080/first-web-application/login.do

But the video I am following is not using application name. output is simply coming by using

http://localhost:8080/login.do

If I removed application name it gives 404 error. I mean how can we use or remove application name from URL.

Sorry to ask such basic question but I could not understand the same

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Answer depends on server used. You forgot to tell which one you're using. If you know the server name, then you can easier search for answers. E.g. when you're using Tomcat (you originally tagged "Spring MVC" on the question even though you aren't using anywhere Spring MVC code within the question, but this way of thinking strongly suggests that you're not using a stock JEE server, but a servletcontainer such as Tomcat), then this is a duplicate of your question: https://stackoverflow.com/q/5328518 – BalusC Aug 06 '20 at 21:15
  • I am simple creating servlet in eclipse and running it. I am using tomcat. I tagged spring-mvc because people who use spring know java. I am using tomcat and eclipse so does in video the trainer is using. but our url is generating different to access the page. –  Aug 07 '20 at 06:40
  • You're not using Spring MVC here. You're using a plain vanilla Servlet. Tagging [servlets] is perfectly fine. – BalusC Aug 07 '20 at 08:18
  • Thank you @BalusC .Can you help me to understand the same? –  Aug 07 '20 at 11:49

1 Answers1

0

That's because you're application is probably packaged as first-web-application.war and your root context name becomes hostname:port/first-web-application/.

Easiest way to resolve this is to package your web application as ROOT.war, and as ROOT is the default name mapped to the root / context path on the server, your application will be accessible at root of the Web Container.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66