0

I'm building a java web app using tomcat 8.5 + JSP files.

When I'm running the servlet on my local windows machine everything is working fine, but when I deploy it to a remote Linux (ubuntu 16.4) machine, I can only get to the home page - every link I click on it that's supposed to go through the controller is not working.

My controller class:

public class Controller extends HttpServlet {

    private static final long serialVersionUID = 102831973239L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Controller() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            request.getRequestDispatcher("/home.jsp").forward(request, response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String path = request.getPathInfo();

        switch (path)
        {
            case "/login":
                String res = "test";

                HttpSession session = request.getSession(false);
                session.setAttribute("result", res);
                response.sendRedirect(request.getContextPath() + "/home.jsp");
                break;
        }
    }
}

home.jsp:

<header id="header" >
    <h1>My site</h1>
     <br/>

    <form action="/controller/login" method="post">
        Enter ASOS link:<br>
        <input type="text" name="pdrUrl" required>
        <br><br>
        <input type="submit" value="Compare">
    </form>

    ${result}
</header>

When I click the submit button on the form element I'm redirected to http://:8080/controller/login

Why doesn't it going through the controller like my local windows machine?

Thanks.

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">

    <servlet>
        <servlet-name>myServlet</servlet-name>
        <servlet-class>controller.Controller</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myServlet</servlet-name>
        <url-pattern>/controller/*</url-pattern>
    </servlet-mapping>
    <error-page>
        <error-code>404</error-code>
        <location>/controller/error</location>
    </error-page>


</web-app>
Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
Dan
  • 71
  • 7

2 Answers2

0

Try adding ${pageContext.request.contextPath} in your form action

   <form action="${pageContext.request.contextPath}/controller/login" method="post">
        Enter ASOS link:<br>
        <input type="text" name="pdrUrl" required>
        <br><br>
        <input type="submit" value="Compare">
    </form>
shazin
  • 21,379
  • 3
  • 54
  • 71
0

your servlet-class package name is Controller however, the servlet-class class-name is also controller.

  1. Try changing your controller name to something else other than package name like MyController.

  2. If it doesn't work in your controller class, try to log "path" variable after the String path = request.getPathInfo(); line.

Check that it is equal to "/login" on Linux system too.

Your servlet-controller only works if URL starts with "controller/"

Hope this helps.

Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30