Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help needeed....

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone..im a newbie to this site and new to servlets aswell..
my problem is..

how to submit a form data using html as frontend into a servlet on the apachetomcat server..

i wrote a simple servlet as shown below

file name HelloServlet.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloServlet extends HttpServlet
{
// doGet needs to overridden to handle empty data requests.
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
PrintWriter out = response.getWriter();
response.setContentType("text/html");

String name=request.getParameter("name");
//PrintWriter out = response.getWriter();
String doctype ="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";

// response.setContentType("text/html");

out.println(doctype+"<html><body>");

out.println("<H3>");

out.println("hello"+name);
out.println("</html></html>");
out.close();

}
public void doPost(HttpServletRequest request,
HttpServletResponse response)

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

}
and the html file for this servlet is shown below:
file name my.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>
<BODY>
<FORM ACTION="/test/HelloServlet" METHOD="post">
entername
<INPUT TYPE="TEXT" NAME = "name">
<INPUT TYPE="submit" value="SUBMIT ORDER">
</FORM>
</BODY>
</HTML>
and i placed the html file in a directory called test in webapps folder of tomcat and i placed the servlet class in the class folder of webinf folder of the test directory..
but wen i run the html file on my browser as
http://localhost:8080/test/my.html
it does display my html page but wen i enter the text and click on submit i get the following error..
http://localhost:8080/test/HelloServlet



HTTP Status 404 - /test/HelloServlet

--------------------------------------------------------------------------------

type Status report

message /test/HelloServlet

description The requested resource (/test/HelloServlet) is not available.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.27
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check for servlet mapping in web.xml.
If servlet is not geting called then may be a problem in mapping file.
 
syed rizwaan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there could you please tel me how to do the mapping wen i call my servlet using html or is this the proper way of calling that web.xml file on html as mentioned below
<form action ="/test/my" method= post>
where my is the name of the url mapping for my servlet

i did try this way but still i get the same error..
so could you plese write down the whole web.xml and the corrosponding html file please and do mention how to include or call it in the html file .
and my other question is
IS is must to do the mapping for my servlet can't i call my servlet just from the html file in this way
<form action ="/test/HelloServlet" method= post>
where helloservlet is the name of my servlet
your help is much appritiated
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somehow your DD(Deployment Descriptor) web.xml should contain your servlet mapping.

<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>
HelloServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/test/HelloServlet</url-pattern>
</servlet-mapping>


Points to ponder:
1) Your servlet class (HelloServlet) should be inside the package. The use of default package is discouraged.

2) In your view(HTML/JSP) , in form tag, the value of action attribute should not start with '/'. It should be like

Because if it starts with '/' means from the root of your application. And if you trust me and don't put '/' that means relative path. But if you are stubborn than you can try something like '/YourApplicationName/test/HelloServlet'. But the better way is to keep relative path.

3)Whenever you paste any code than use CODE tag.

4) Use meaningful topic name so that other can have some idea from the heading itself.Something like "How to map Servlet in web.xml" and not like "help needed"

Update your code and tell me "Yupee Now it works"
[ December 04, 2008: Message edited by: Vishal Pandya ]

[ December 04, 2008: Message edited by: Vishal Pandya ]

[ December 04, 2008: Message edited by: Vishal Pandya ]

[ December 04, 2008: Message edited by: Vishal Pandya ]
[ December 04, 2008: Message edited by: Vishal Pandya ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic