This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet not being called from html

 
tubu Ray
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i am using eclipse 3.2 tomcat project to call a servlet from a html page.(code follows), but it does not get the servlet.please help.

/*******HTML:********SERVLET is the name of my project/
<html>
<body>
<h3>Enter your user id and password</h3><p>
<form action = "/SERVLET/ray.servlet.RequestServlet" method = "POST">
UserId: <input type = "text" name= "userid"><br><br>
<input type = submit value ="show statement">
</form>
</body>
</html>

/****Servlet****/
package ray.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestServlet extends HttpServlet
{
private PrintWriter out;
private String name;

public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
out = res.getWriter();
out.println("<html><body>");
out.println("hello");
name = req.getParameter("userid");
out.println("Welcome "+name+"<br>");
}

}
/*******WEB.xml *********/
<?xml version = "1.0"?>
<web-app>
<servlet>
<servlet-name>REQ</servlet-name>
<servlet-class>ray.servlet.RequestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>REQ</servlet-name>
<url-pattern>/servlet/login.html</url-pattern>
</servlet-mapping>
</web-app>
/********************ERROR MESSAGE**************************************/

TTP Status 404 - /SERVLET/ray.servlet.RequestServlet

type Status report

message /SERVLET/ray.servlet.RequestServlet

description The requested resource (/SERVLET/ray.servlet.RequestServlet) is not available.
Sun-Java-System/Web-Services-Pack-1.4
/***********************************************/

please help
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't seem to understand what servlet mapping is.
Your form action must match one of the url pattern in your web.xml.
You've got only one pattern linked to your servlet : /servlet/login.html
 
tubu Ray
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how do i do this
 
Ganesh Chinni
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change ur web.xml as follows,

<url-pattern>/SERVLET/*</url-pattern>
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Match your form action (in the html file) to the appropriate url pattern.
You should add a url pattern in your web.xml.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<form action = "/SERVLET/ray.servlet.RequestServlet" method = "POST">


You got problem here. Modify this to


<form action = "servlet/login.html" method = "POST">
 
vjy chin
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also in the servlet you have implemented only the doGet(), but in the html you are using POST method, so you have to implement doPost() also.

Hope this helps.
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic