Note that there are some explanatory texts on larger screens.

plurals
  1. POurl-pattern Servlet Tomcat 7.0
    primarykey
    data
    text
    <p>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:</p> <pre><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( "&lt;!doctype html public \"-//W3C//DTD HTML 4.0 Transitional//EN\"&gt;\n" + "&lt;html&gt;\n" + "&lt;head&gt;\n" + " &lt;title&gt;Murach's Java Servlets and JSP&lt;/title&gt;\n" + "&lt;/head&gt;\n" + "&lt;body&gt;\n" + "&lt;h1&gt;Thanks for joining our email list&lt;/h1&gt;\n" + "&lt;p&gt;Here is the information that you entered:&lt;/p&gt;\n" + " &lt;table cellspacing=\"5\" cellpadding=\"5\" border=\"1\"&gt;\n" + " &lt;tr&gt;&lt;td align=\"right\"&gt;First name:&lt;/td&gt;\n" + " &lt;td&gt;" + firstName + "&lt;/td&gt;\n" + " &lt;/tr&gt;\n" + " &lt;tr&gt;&lt;td align=\"right\"&gt;Last name:&lt;/td&gt;\n" + " &lt;td&gt;" + lastName + "&lt;/td&gt;\n" + " &lt;/tr&gt;\n" + " &lt;tr&gt;&lt;td align=\"right\"&gt;Email address:&lt;/td&gt;\n" + " &lt;td&gt;" + emailAddress + "&lt;/td&gt;\n" + " &lt;/tr&gt;\n" + " &lt;/table&gt;\n" + "&lt;p&gt;To enter another email address, click on the Back &lt;br&gt;\n" + "button in your browser or the Return button shown &lt;br&gt;\n" + "below.&lt;/p&gt;\n" + "&lt;form action=\"join_email_list.html\" " + " method=\"post\"&gt;\n" + " &lt;input type=\"submit\" value=\"Return\"&gt;\n" + "&lt;/form&gt;\n" + "&lt;p&gt;This page has been accessed " + globalCount + " times.&lt;/p&gt;" + "&lt;/body&gt;\n" + "&lt;/html&gt;\n"); System.out.println(globalCount); log("Global variable" +globalCount); out.close(); } protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } } </code></pre> <p>And here is the web.xml file:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;!-- the definitions for the servlets --&gt; &lt;!-- the mapping for the servlets --&gt; &lt;servlet&gt; &lt;servlet-name&gt;DisplayMusicChoicesServlet&lt;/servlet-name&gt; &lt;servlet-class&gt;email.DisplayMusicChoicesServlet&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet&gt; &lt;servlet-name&gt;AddToEmailListServlet&lt;/servlet-name&gt; &lt;servlet-class&gt;email.AddToEmailListServlet&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;!-- other configuration settings for the application --&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;DisplayMusicChoicesServlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/displayMusicChoices&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;AddToEmailListServlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/addToEmailList&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;session-config&gt; &lt;session-timeout&gt;30&lt;/session-timeout&gt; &lt;/session-config&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;join_email_list.html&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;/web-app&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload