2

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>
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Camus
  • 827
  • 2
  • 20
  • 36

2 Answers2

1

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • This is a simple exercise that I'm doing. I'm not checking whether the email has @ or not. What I understood about mapping is that I can choose a name to be displayed on the URL, right? For security reasons, for example, I want to change the URL to be displayed through mapping. I'm using netbeans, so I reckon I don't need to deploy manually the war file. Am I wrong? Cheers – Camus Dec 09 '11 at 02:48
  • 1
    It's a simple exercise from a book that's ten years old; am I right? You should know that what they're asking you to do is considered an anti-pattern; no one would embed HTML in a servlet and write it to the output stream anymore. My point is that you can't pass that email in a query string without escaping it. – duffymo Dec 09 '11 at 10:09
  • Well, I did not find any recent book to study servlet. I know the best way to program is combining html, jsp and servlet and it is what I'm gonna learn in the next chapter. 2 questions. Do u know a good book for beginners(new version) and if you know how to solve this issue. – Camus Dec 10 '11 at 14:50
  • The important thing is not finding a 2011 book, because the best way to do things hasn't changed much in the last five years. The key is to never embed HTML inside a servlet to send down to a browser. It's standard practice to ues a templating solution like JSP using JSTL or Velocity or FreeMarker to marry data back from a service to dynamically generate the page. – duffymo Dec 10 '11 at 16:58
  • Thanks. I reckon the author's idea is to show that it is possible to embed html within the servlet. Then he will show how to use jsp and servile together, getting the benefits of all of them. I ran quickly through freemarker and I didn't get the real idea. This tool generate a text in a asp page instead of a java code? Cheers – Camus Dec 11 '11 at 04:28
0

Instead of giving specific file's url like admin.xhtml, create a new folder under "WebContent" directory. In that case lets say folders name is "secured", place admin.xhtml in that folder, then <url-pattern>/secured/*</url-pattern>

Worked for me, i hope this helps

mürşide
  • 31
  • 3