I was trying some basic Servlet Programming. I had created a basic Html form and on submitting it, wanted to invoke a servlet which prints the name entered by the user. I am posting the code below.
HTML: (it is in rootfolder -- webapps\helloworld\hello.html)
<html>
<body>
<form action="./hello" method="get">
Name: <input type="text" name="P1">
<input type="submit" value="Submit">
</form>
</body>
</html>
> web.xml (Location: webapps\helloworld\WEB_INF\web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>hs</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hs</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
> Servlet Class (Location: webapps\helloworld\WEB_INF\classes\HelloWorld.class
import javax.servlet.*;
import java.io.*;
public class HelloWorld implements Servlet{
public void init(ServletConfig sc)throws ServletException{
//initialization code
}
public ServletConfig getServletConfig(){
return null;
}
public void service(ServletRequest request, ServletResponse response)throws ServletException,IOException{
String name=request.getParameter("P1");
PrintWriter out = response.getWriter();
out.println("Hello: "+name);
}
public String getServletInfo(){
return null;
}
public void destroy(){
}
}
So whenever I type a name and click on submit, I am getting 'HTTP Status 404' error. Could you please tell me what I am doing wrong! Any help would be much appreciated. Thanks!