简体   繁体   English

Java应用程序后操作可在Eclipse Tomcat中使用,但不适用于本地Tomcat服务器

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

I have developed a Java web application in Eclipse using Maven and the build goal of "tomcat7:run" to run the application within Eclipse. 我已经使用Maven在Eclipse中开发了一个Java Web应用程序,其构建目标为“ tomcat7:run”,以便在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. 我现在已经开始从Eclipse导出.war文件,在本地Tomcat服务器上尝试用户启动的发布命令时,我看到一些不同的行为。 The first .jsp that users will access loads without issue when accessing http :// localhost:8080/qa (the .war file is called qa.war). 用户访问http:// localhost:8080 / qa(.war文件称为qa.war)时,用户将访问的第一个.jsp不会出现问题。 When doing a post command, my application cannot find the "/login.do" file specified in the form tag. 当执行发布命令时,我的应用程序找不到在form标记中指定的“ /login.do”文件。

My guess is that there is something that I am doing wrong with the servlet mapping. 我的猜测是,servlet映射存在某些问题。 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. 请注意,我已经通过访问http:// localhost:8080 / qa访问了本地Tomcat服务器上的Web应用程序,并且尚未对其进行配置,以便在访问http://:// localhost:8080 /时显示默认页面,可能也有关系。

Here is my form on login.jsp: 这是我在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: 这是我的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: 我的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 图片:访问Tomcat服务器,在Eclipse工作区中执行发布和文件结构

No wonder why the server returns 404. You are trying to get the /login.do uri from your form, which turns into http://localhost:8080/login.do when navigating: This URL lacks the application context "qa". 难怪服务器为什么返回404。您试图从表单中获取/login.do uri,因此在导航时会变成http://localhost:8080/login.do :此URL缺少应用程序上下文 “ qa”。 In brief: You must drop off the leading slash. 简而言之:您必须放下斜杠。 Let it be: 随它去:

<form action="login.do" method="post">

I tried a few things which appears to have worked: 我尝试了一些似乎有效的方法:

  1. I removed the servlet mapping from web.xml 我从web.xml中删除了servlet映射

.. ..

<!--<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. 将我的.war打包在Eclipse中,然后将其重命名为ROOT.war并将其放置在Tomcat Web应用程序文件夹中。 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 Eclipse中的Tomcat与我的本地Tomcat服务器之间的差异是,我的应用程序通过访问“ http:// localhost:8080”而不是“ http:// localhost:8080 / qa”在Eclipse中运行,因此这样做似乎有所帮助

  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. 执行完这两个修复程序后,我很难重现问题,因此无法确认将表单操作从“ /login.do”修改为“ login.do”是否可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM