#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    New Jersey
    Posts
    100
    Rep Power
    12

    Tomcat 4 - Problem Executing Servlet from HTML page


    I am having considerable difficulty with the web.xml file.

    The servlet below is a modified version of a servlet from Beginning JSP Web Development (Wrox Press). I am in a testing phase and would simply like to execute the servlet from an html page but am getting the message:

    The requested resource (/begjsp-ch13/DisplayServlet) is not available.

    My html file is under:

    d:/jwsdp-1_1/webapps/begjsp-ch13

    My web.xml file is under:
    d:/jwsdp-1_1/webapps/begjsp-ch13/WEB-INF

    My servlet is under:
    d:/jwsdp-1_1/webapps/begjsp-ch13/WEB-INF/classes/servlets/DisplayServlet
    *******************************************************
    The html page, nameregister.html, has this code:

    <HTML>
    <HEAD>
    <TITLE>Register Page</TITLE>
    </HEAD>

    <BODY >
    <CENTER>
    <TABLE>
    <TR><TD><FONT COLOR="black" SIZE="7"> Registration</FONT></TD>
    </TR>
    </TABLE>
    </CENTER>

    <CENTER>
    <FORM name="registerform"
    METHOD="Post"
    ACTION="DisplayServlet">

    <TABLE BORDER="3" CELLPADDING="1" WIDTH="600" ALIGN="CENTER">
    <TR> <TH COLSPAN="2">Please Register<P></TH> </TR>
    <TR> <TD><I><B>Name:</B></I></TD>
    <TD><INPUT TYPE="text" NAME="firstname" SIZE="30">
    <INPUT TYPE="text" NAME="lastname" SIZE="27"></TD>
    </TR>
    <TR> <TD COLSPAN="2" ALIGN="CENTER">
    <INPUT TYPE="SUBMIT" VALUE="Submit">
    </TD>
    </TR>
    </TABLE>
    </FORM>

    </CENTER>
    <P>
    </BODY>
    </HTML>
    *******************************************************
    The web.xml file has this code:

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

    <web-app>
    <display-name>A Simple Application</display-name>

    <servlet>
    <servlet-name>DisplayServlet</servlet-name>
    <servlet-class>servlets.DisplayServlet</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>DisplayServlet</servlet-name>
    <url-pattern>/Display/*</url-pattern>
    </servlet-mapping>

    <session-config>
    <session-timeout>30</session-timeout>
    </session-config>
    </web-app>
    ******************************************************
    The servlet has this code:

    package servlets;

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

    public class DisplayServlet extends HttpServlet
    {

    public void doPost(HttpServletRequest req,
    HttpServletResponse res)
    throws ServletException, IOException
    {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    out.println
    ("<html><head><title>Example</title></head><body>");
    out.println("Parameter String being processed:<p>");
    out.println(req.getParameter("firstname").trim());
    out.println(req.getParameter("lastname").trim());
    out.println("<p>");
    out.println("Request Parameters:<p>");

    Enumeration enumParam = req.getParameterNames();

    while (enumParam.hasMoreElements())
    {
    String paramName = (String) enumParam.nextElement();
    String paramValues[] = req.getParameterValues(paramName);

    if (paramValues != null)
    {
    for (int i = 0; i < paramValues.length; i++ )
    {
    out.println(paramName + " (" + i + "): " + paramValues[i]
    + "<p>");
    }
    }
    }
    out.println("</body></html>");
    out.close();
    }

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

    }
    ********************************************
    There shouldn’t be anything wrong with the servlet code. Except for using req.getParameter instead of req.getQueryString and having both a doPost and doGet, it is the same as the book.

    How do I execute a servlet from an HTML page? What do I need to include in the web.xml file that is not already there?

    Thanks for any help you can offer.

    Robin
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    First I want to say Thank You Robin for being so detailed in your question. Providing path locations where the files are located and the complete code of the relevant files makes things much easier.

    Having said that, your problem is in your web.xml (or the link you are using. Either or). The part of the web.xml you should focus on when making a mapping for a servlet is this:
    Code:
    <servlet-mapping>
    <servlet-name>DisplayServlet</servlet-name>
    <url-pattern>/Display/*</url-pattern>
    </servlet-mapping>
    This is saying that the servlet named "DisplayServlet" can be accessed using a url pattern of <context-path> (in this case begjsp-ch13) followed by /Display/ and then anything else (* is a wilcard). So given that, the action in your form could be "begjsp-ch13/Display" or "begjsp-ch13/Display/thisIsABunchOfGarbage". Once it finds a match in a servlet mapping it will run that servlet.

    Having said THAT, I would say this is probably not how you would want to do it. Usually for a servlet mapping you would give a absolute name and not use wildcards. For instance, if you wanted to be able to call the servlet using an alias that is the same as the name of the servlet, you could make the servlet-mapping like this:
    Code:
    <servlet-mapping>
    <servlet-name>DisplayServlet</servlet-name>
    <url-pattern>/DisplayServlet</url-pattern>
    </servlet-mapping>
    Now you can call the servlet using <context-path>/DisplayServlet where <context-path> is the name of your current web app, in this case begjsp-ch13. If you change your web.xml as I have described and change the html page so the action is "begjsp-ch13/DisplayServlet" it should work.

    Hope that helps.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    New Jersey
    Posts
    100
    Rep Power
    12
    Nemi,

    Thanks for responding. When I posted the message, I didn't understand the purpose of the URL pattern in the web.xml file. I now know it is to indicate the files that will call the servlet. I actually needed to modify both nameregister.html, which calls the servlet, and the web.xml file.

    I have made the following changes:
    ****************************
    nameregister.html


    <FORM name="registerform"
    onSubmit="return validateRegister();"
    METHOD="Post"
    ACTION="servlet/DisplayServlet">

    ***************************
    web.xml

    <servlet-mapping>
    <servlet-name>DisplayServlet</servlet-name>
    <url-pattern>/*register.html</url-pattern>
    </servlet-mapping>
    **************************

    The above successfully executes the servlet.

    However, for some reason which I don't understand, the following doesn't work:

    <servlet-mapping>
    <servlet-name>DisplayServlet</servlet-name>
    <url-pattern>/nameregister.html</url-pattern>
    </servlet-mapping>
    *******************************
    Without the asterisk in the url-pattern, I get the following error message just from trying to get to the html page:

    HTTP Status 500
    The server encountered an internal error that prevented it from fulfilling this request.

    java.lang.NullPointerException

    Robin
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    I think you misunderstand the purpose of the servlet-mapping. There are two ways of requesting a servlet - using the servlet invoker (<hostname>/<context>/servlet/package.ServletClass) and making a servlet-mapping. You are using the first in your forms action. If you are going to use this way, you don't even need to make a servlet-mapping. Just delete it.

    However, if you want to give the servlet an alias, you make the servlet-mapping like I described before.
    Code:
    <servlet-mapping>
    <servlet-name>DisplayServlet</servlet-name>
    <url-pattern>/DisplayServlet</url-pattern>
    </servlet-mapping>
    Then your form will read
    Code:
    <FORM name="registerform" 
    onSubmit="return validateRegister();"
    METHOD="Post"
    ACTION="DisplayServlet">
    No "servlet/" in front of it. If you give an alias to the servlet that is the same as your html file, how are you going to request your html file?

    If you are still confused I suggest you delete the servlet-mapping for that servlet. Your stuff should work then without changing anything else.

IMN logo majestic logo threadwatch logo seochat tools logo