aspose file tools*
The moose likes Servlets and the fly likes help needeed.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "help needeed...." Watch "help needeed...." New topic
Author

help needeed....

syed rizwaan
Greenhorn

Joined: Dec 03, 2008
Posts: 2
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
Patel Chintan
Ranch Hand

Joined: Mar 01, 2007
Posts: 80
Check for servlet mapping in web.xml.
If servlet is not geting called then may be a problem in mapping file.


Chintan Patel, SCJP1.5, http://datewithjava.blogspot.com
syed rizwaan
Greenhorn

Joined: Dec 03, 2008
Posts: 2
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
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help needeed....