Search code examples
javatomcatservletsurl-pattern

url-pattern Servlet Tomcat 7.0


I'm studying JSP/Servlet by myself. And I'm facing a problem that I'm able to solve. I'm creating a simple form that will request a servlet. The problem is when I change the url-pattern in the web.xml to a url that I want, the Tomcat give me an error 404. However, when I change the url-pattern to the same name as the servlet it works. Another thing that I noticed is when I type manually the url-pattern that I want on the URL it works. It seems that I'm not being redirect to the right place. I've checked many times the web.xml and I could not find anything wrong. Here is the servlet code:

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);
    }

}

And here is the web.xml file:

<?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>

Solution

  • Lots of criticisms of what you're doing, but I'll restrict myself to your question.

    If you deploy your application to the Tomcat 7 /webapps directory in a WAR file named foo.war, then the URL to invoke your AddToEmailListServlet and display that HTML page in the browser would be:

    http://host:8080/foo/AddToEmailListServlet
    

    I'm assuming that you're POSTing those three request parameters in a form, because you have to encode the at-sign in the email address before sending it.