Java application post action works in Eclipse Tomcat but does not work on local Tomcat server

Question

I have developed a Java web application in Eclipse using Maven and the build goal of "tomcat7:run" to run the application within Eclipse. I have now started exporting my .war file from Eclipse and I am seeing some different behavior when attempting user-initiated post commands on my local Tomcat server. The first .jsp that users will access loads without issue when accessing http :// localhost:8080/qa (the .war file is called qa.war). When doing a post command, my application cannot find the "/login.do" file specified in the form tag.

My guess is that there is something that I am doing wrong with the servlet mapping. Note that I have been accessing my web application on my local Tomcat server by going to http :// localhost:8080/qa and have not been configuring it so that the default page appear when accessing http :// localhost:8080/, which may also be related.

Here is my form on login.jsp:

<form action="/login.do" method="post">
    Username: <input type="text" name="username">
    Password: <input type="password" name="password">
    <br>
    <input type="submit" value="Submit">
</form>

Here is my LoginServlet:

package com.XXXXX.qa.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

      @Override
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

          request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);

      }

      @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                request.getRequestDispatcher("/WEB-INF/views/runtest.jsp").forward(request, response);    

      }
}

My web.xml:

<!-- webapp/WEB-INF/web.xml -->
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <display-name>To do List</display-name>

    <welcome-file-list>
        <welcome-file>login.do</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>com.XXXXX.qa.servlets.LoginServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>login.do</url-pattern>
    </servlet-mapping>  

    <servlet>
        <servlet-name>RunTestServlet</servlet-name>
        <servlet-class>com.XXXXX.qa.servlets.RunTestServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>RunTestServlet</servlet-name>
        <url-pattern>/runtest.do</url-pattern>
    </servlet-mapping>

</web-app>

IMAGE: Accessing Tomcat server, performing post and file structure in Eclipse workspace


Show source
| java   | eclipse   | jsp   | tomcat   | servlets   2017-09-05 21:09 2 Answers

Answers to Java application post action works in Eclipse Tomcat but does not work on local Tomcat server ( 2 )

  1. 2017-09-06 01:09

    No wonder why the server returns 404. You are trying to get the /login.douri from your form, which turns into http://localhost:8080/login.do when navigating: This URL lacks the application context "qa". In brief: You must drop off the leading slash. Let it be:

    <form action="login.do" method="post">
    
  2. 2017-09-06 16:09

    I tried a few things which appears to have worked:

    1. I removed the servlet mapping from web.xml

    ..

    <!--<servlet-mapping>
      <servlet-name>LoginServlet</servlet-name>
      <url-pattern>login.do</url-pattern>
    </servlet-mapping>-->  
    

    ..

    <!--<servlet-mapping>
      <servlet-name>RunTestServlet</servlet-name>
      <url-pattern>/runtest.do</url-pattern>
    </servlet-mapping>-->
    

    This looks to have been causing a lot of problems

    1. Packaging my .war in Eclipse, then renaming it ROOT.war and placing it in the Tomcat web apps folder. One discrepancy between Tomcat in Eclipse and my local Tomcat server is that my app was running in Eclipse by accessing "http :// localhost:8080" rather than "http :// localhost:8080/qa", so doing this seemed to help

    2. After performing those two fixes, I had difficulty reproducing the issue, so I cannot confirm whether modifying the form action from "/login.do" to "login.do" fixes the issue.

Leave a reply to - Java application post action works in Eclipse Tomcat but does not work on local Tomcat server

◀ Go back