java - url 模式 Servlet Tomcat 7.0

标签 java tomcat servlets url-pattern

我正在自学JSP/Servlet。我正面临一个我能够解决的问题。 我正在创建一个将请求 servlet 的简单表单。问题是当我将 web.xml 中的 url-pattern 更改为我想要的 url 时,Tomcat 给我一个错误 404。但是,当我将 url-pattern 更改为与它工作的 servlet 相同的名称时。我注意到的另一件事是,当我在 URL 上手动键入我想要的 url-pattern 时,它会起作用。 看来我没有被重定向到正确的地方。我已经多次检查 web.xml,但没有发现任何错误。这是 servlet 代码:

package email;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import business.User;
import data.UserIO;

/**
 * @author Joel Murach
 */
public class AddToEmailListServlet extends HttpServlet
{    
    int globalCount;

    public void init() throws ServletException{
        globalCount = 0;
    }
    protected void doPost(
        HttpServletRequest request, 
        HttpServletResponse response) 
        throws ServletException, IOException
    {
        //Global variable
        globalCount++;

        // get parameters from the request
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String emailAddress = request.getParameter("emailAddress");

        // get a relative file name
        ServletContext sc = getServletContext();
        String path = sc.getRealPath("/WEB-INF/EmailList.txt");

        // use regular Java objects to write the data to a file
        User user = new User(firstName, lastName, emailAddress);
        UserIO.add(user, path);

        // send response to browser
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();        
        out.println(
          "<!doctype html public \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"
        + "<html>\n"
        + "<head>\n"
        + "  <title>Murach's Java Servlets and JSP</title>\n"
        + "</head>\n"
        + "<body>\n"
        + "<h1>Thanks for joining our email list</h1>\n"
        + "<p>Here is the information that you entered:</p>\n"
        + "  <table cellspacing=\"5\" cellpadding=\"5\" border=\"1\">\n"
        + "  <tr><td align=\"right\">First name:</td>\n"
        + "      <td>" + firstName + "</td>\n"
        + "  </tr>\n"
        + "  <tr><td align=\"right\">Last name:</td>\n"
        + "      <td>" + lastName + "</td>\n"
        + "  </tr>\n"
        + "  <tr><td align=\"right\">Email address:</td>\n"
        + "      <td>" + emailAddress + "</td>\n"
        + "  </tr>\n"
        + "  </table>\n"
        + "<p>To enter another email address, click on the Back <br>\n"
        + "button in your browser or the Return button shown <br>\n"
        + "below.</p>\n"
        + "<form action=\"join_email_list.html\" "
        + "      method=\"post\">\n"
        + "  <input type=\"submit\" value=\"Return\">\n"
        + "</form>\n"
        + "<p>This page has been accessed "
        + globalCount + " times.</p>"
        + "</body>\n"
        + "</html>\n");
        System.out.println(globalCount);
        log("Global variable" +globalCount);
        out.close();
    }    

    protected void doGet(
        HttpServletRequest request, 
        HttpServletResponse response) 
        throws ServletException, IOException
    {
        doPost(request, response);
    }

}

这是 web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
    <!-- the definitions for the servlets -->
    <!-- the mapping for the servlets -->
    <servlet>
        <servlet-name>DisplayMusicChoicesServlet</servlet-name>
        <servlet-class>email.DisplayMusicChoicesServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>AddToEmailListServlet</servlet-name>
        <servlet-class>email.AddToEmailListServlet</servlet-class>
    </servlet>

    <!-- other configuration settings for the application -->
    <servlet-mapping>
        <servlet-name>DisplayMusicChoicesServlet</servlet-name>
        <url-pattern>/displayMusicChoices</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>AddToEmailListServlet</servlet-name>
        <url-pattern>/addToEmailList</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>join_email_list.html</welcome-file>
    </welcome-file-list>
</web-app>

最佳答案

很多人对你所做的事情提出批评,但我会限制自己回答你的问题。

如果您将应用程序部署到名为 foo.war 的 WAR 文件中的 Tomcat 7/webapps 目录,那么调用您的 AddToEmailListServlet 并在浏览器中显示该 HTML 页面的 URL 将是:

http://host:8080/foo/AddToEmailListServlet

我假设您在表单中发布这三个请求参数,因为您必须在发送之前对电子邮件地址中的 at 符号进行编码。

关于java - url 模式 Servlet Tomcat 7.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8440093/

相关文章:

java - 更改为 void 或保留返回值

java - KeyListener 事件显然没有在 Java 小程序中触发

java - 使用 CardScrollView 的 Google Glass 多嵌入式布局

java - 如何将 log4j 日志记录从 catalina.out 重定向到单独的文件?

java - 通过eclipse启动应用程序和服务器时不创建tomcat日志

java - mysql使用servlet更新用户信息的方法

java - isReady() 在关闭状态下返回 true - 为什么?

Java 字符串置换ExecutorService

java - Tomcat 为每个 HTTP 请求分配一个新线程,那么使用 future.get() 的动机是什么?

java - HttpServletResponse.resetBuffer() 不起作用