Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2008

Tip: Looking for answers? Try searching our database.

Trouble using POST method with servlet

Thread view: 
K Gaur - 22 Feb 2008 15:04 GMT
hello friends,

i have just started working with servlets and i stumbled upon this
problem

i was just testing servlet with GET and Post methods
the following html-servlet pair works fine with GET method in form
tag

but with POST method it goes awry

i m using Tomcat 6.0 , firefox browser

the code for html document (mail_list.html) is-->

__________________________________________________________________
<html>

<head>
    <title> E-mail list application</title>
<head>

<body>
    <h1> Join our mailing list</h1>
    <p> To join enter yor name and email id</p>

    <form action="http://localhost:8080/m_egs/EmailServlet"
method="post">
    <table cellspacing="5" border="0">
        <tr>
            <td align="right"> First name:</td>
            <td><input type="text" name="firstName"></td>
        </tr>
        <tr>
            <td align="right"> Last Name:</td>
            <td><input type="text" name="lastName"></td>
        </tr>
        <tr>
            <td align="right"> Email id:</td>
            <td><input type="text" name="emailId"></td>
        </tr>
        <tr>
            <td></td>
            <td><br><input type="submit" value="submit"></td>
        </tr>
    </table>
    </form>
</body>

</html>
_____________________________________________________________________

and that of the servlet (EmailServlet.java) is -->

_____________________________________________________________________
package servpack;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import business.*;
import data.*;

public class EmailServlet extends HttpServlet
{

    private int accessCount;

    public void init() throws ServletException{
        accessCount=0;
    }

    public void doGet(HttpServletRequest request, HttpServletResponse

response)
        throws ServletException, IOException{
            response.setContentType("text/html");
            PrintWriter out=response.getWriter();

            String

firstName=request.getParameter("firstName");
            String lastName=request.getParameter("lastName");
            String eID=request.getParameter("emailId");
            User user=new User(firstName, lastName, eID);
            UserIO.addRecord(user,"C:/Program Files/Apache

Software Foundation/Tomcat 6.0/webapps/m_egs/WEB-INF/etc/
UserEmail.txt");

            int localCount;
            synchronized (this){
                localCount=++accessCount;
            }
            out.println(
                "<html>\n<head>\n<title>a servlet was

called</title>\n</head>\n"
                +"<body>\n <h1> Thanks for joining our

mailing list </h1>\n"
                +"<p>Here is your info:</p>\n"
                +"<table border=\"0\" cellspacing=\"5\"

cellpadding=\"5\">\n"
                +"    <tr>\n    <td>FirstName:</td> \n

<td>" + firstName + "</td>\n</tr>\n"
                +"    <tr>\n    <td>LastName:</td> \n

<td>" + lastName + "</td>\n</tr>\n"
                +"    <tr>\n    <td>Email Address:</td> \n

 <td>" + eID + "</td>\n</tr>\n"
                +"</table>\n"
                +"<p> this page has been accessed " +

localCount + " times.</p>\n"
                +"</body>\n </html>");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse

response)
        throws ServletException, IOException {
            doGet(request,response);
    }
}
__________________________________________________________________________

the browser shows this -->
-------------------------------------------------------------------------------
HTTP Status 405 - HTTP method POST is not supported by this URL

type Status report

message HTTP method POST is not supported by this URL

description The specified HTTP method is not allowed for the
requested

resource (HTTP method POST is not supported by this URL).
Apache Tomcat/6.0.13
-------------------------------------------------------------------------------

please tell me where i m wrong .
Wojtek - 22 Feb 2008 17:37 GMT
K Gaur wrote :
> hello friends,
>
[quoted text clipped - 67 lines]
>         accessCount=0;
>     }

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

>     public void doGet(HttpServletRequest request, HttpServletResponse
>
[quoted text clipped - 70 lines]
>
> please tell me where i m wrong .

Signature

Wojtek :-)

Lew - 22 Feb 2008 23:46 GMT
>     public void doPost(HttpServletRequest request, HttpServletResponse
> response)
>          throws ServletException, IOException
>    {
>       doGet(request,response);
>    }

K Gaur wrote :
>>     public void doPost(HttpServletRequest request, HttpServletResponse
>>
[quoted text clipped - 3 lines]
>>     }
>> }

The difference escapes me.

Signature

Lew

Wojtek - 24 Feb 2008 00:30 GMT
Lew wrote :
>>     public void doPost(HttpServletRequest request, HttpServletResponse
>> response)
[quoted text clipped - 13 lines]
>
> The difference escapes me.

Um, yes, um.....

Ok, I had a blank moment and did not see that code

Signature

Wojtek :-)

K Gaur - 24 Feb 2008 14:34 GMT
> Lew wrote :

> > The difference escapes me.

> Ok, I had a blank moment and did not see that code
>
> --
> Wojtek :-)

friends please be lucid, the solution to my problem escaped me
Lew - 24 Feb 2008 15:50 GMT
> <form action="http://localhost:8080/m_egs/EmailServlet" method="post">

Is it true that when you submit the form as above but with 'method="get"', and
no other change, you get the markup specified by the out.println()?

I find my attention drawn to the deployment descriptor,
<project-web-dir>/WEB-INF/web.xml, informally called "the web.xml file".

Please provide the web.xml contents.

I observe that you elevated doPost() and doGet() to 'public' access.  You
don't really need to do that.

Tomcat is deployed to a directory on the web server host known as
$CATALINA_HOME in the UNIX style of environment variable, %CATALINA_HOME% to
Windows.  Web applications are deployed to Tomcat in
$CATALINA_HOME/webapps/<context-root>/, where "context-root" is the name of
the web app.

What is the full path of the file 'mail_list.html' within $CATALINA_HOME on
your Tomcat host?

Excellent use of instance variable 'accessCount', BTW.  The example of correct
synchronization and motivation for use of an instance variable was very
helpful to me.

Signature

Lew

K Gaur - 28 Feb 2008 15:14 GMT
> Is it true that when you submit the form as above but with 'method="get"', and
> no other change, you get the markup specified by the out.println()?

yes it's very true. i have just replaced 'get' by 'post'

> I find my attention drawn to the deployment descriptor,
> <project-web-dir>/WEB-INF/web.xml, informally called "the web.xml file".
>
> Please provide the web.xml contents.

The contents of web.xml file are :
_____________________________________________________________________
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed
with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version
2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<web-app 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"
  version="2.5">

 <servlet>
    <servlet-name>TryOne</servlet-name>
    <servlet-name>EmailServlet_2</servlet-name>
    <servlet-name>EmailServlet</servlet-name>

    <servlet-class>servpack.TryOne</servlet-class>
    <servlet-class>servpack.EmailServlet_2</servlet-class>
    <servlet-class>servpack.EmailServlet</servlet-class>
 </servlet>
 <servlet-mapping>
    <servlet-name>TryOne</servlet-name>
    <servlet-name>EmailServlet_2</servlet-name>
    <servlet-name>EmailServlet</servlet-name>

    <url-pattern>/TryOne</url-pattern>
    <url-pattern>/EmailServlet_2</url-pattern>
    <url-pattern>/EmailServlet</url-pattern>
 </servlet-mapping>

</web-app>
_______________________________________________________________

> I observe that you elevated doPost() and doGet() to 'public' access.  You
> don't really need to do that.

hmmm. ok.

> Tomcat is deployed to a directory on the web server host known as
> $CATALINA_HOME in the UNIX style of environment variable, %CATALINA_HOME% to
[quoted text clipped - 4 lines]
> What is the full path of the file 'mail_list.html' within $CATALINA_HOME on
> your Tomcat host?

here it is->
$CATALINA_HOME\webapps\m_egs\mail_list.html
K Gaur - 29 Feb 2008 01:50 GMT
> > Is it true that when you submit the form as above but with 'method="get"', and
> > no other change, you get the markup specified by the out.println()?
[quoted text clipped - 72 lines]
> here it is->
> $CATALINA_HOME\webapps\m_egs\mail_list.html
Lew - 29 Feb 2008 11:31 GMT
> The contents of web.xml file are :
> _____________________________________________________________________
[quoted text clipped - 25 lines]
>
> </web-app>

This is not a valid web.xml.  You're supposed to have multiple <servlet> tags
each with exactly one <servlet-name> and exactly one of either a
<servlet-class> or <jsp-file> element contained within.  Likewise, a
<servlet-mapping> tag takes exactly one <servlet-name> within, and every
<url-pattern> inside the one <servlet-mapping> refers to the one <servlet-name>.

Signature

Lew

K Gaur - 29 Feb 2008 12:58 GMT
> This is not a valid web.xml.  You're supposed to have multiple <servlet> tags

well that did it, thanks.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.