url-pattern Servlet Tomcat 7.0

时间:2011-12-09 01:40:09

标签: 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>

2 个答案:

答案 0 :(得分:1)

很多批评你正在做什么,但我会限制你自己的问题。

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

http://host:8080/foo/AddToEmailListServlet

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

答案 1 :(得分:0)

不要像admin.xhtml那样提供特定文件的URL,而是在“WebContent”目录下创建一个新文件夹。在这种情况下,让我们说文件夹名称是“安全的”,然后将admin.xhtml放在该文件夹中 <url-pattern>/secured/*</url-pattern>

为我工作,我希望这有帮助