5

I am a newbie in servlet programming.

The following is my sample servlet code

Sample.Java

public class Sample extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");  
        PrintWriter pw=res.getWriter();  

        String name=req.getParameter("name");//will return value  
        pw.println("Welcome "+name);  
        pw.close();  
    }
}

Web.xml

<web-app>
<servlet>
<servlet-name>sampleservlet</servlet-name>
<servlet-class>Sample</servlet-class>
<load-on-startup>1</load-on-startup> 
</servlet>
<servlet-mapping>
<servlet-name>sampleservlet</servlet-name>
<url-pattern>/Sample</url-pattern>
</servlet-mapping>
</web-app>

Index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="Sample" method="get">  
Enter your name<input type="text" name="name"><br>  
<input type="submit" value="login">  
</form>  
</body>
</html>

My problem

When I right click on my project and then click Run as -> Run on server, I select the server, it asks me for to select a server and then I select the server to run. In the eclipse window a browser window is opened which has the address URL as http://localhost:8080/Sample/

enter image description here

When I click on login it gives me an error like this,

HTTP Status 404 - /Sample/welcome


type Status report

message /Sample/welcome

description The requested resource is not available.


Apache Tomcat/7.0.62

Screenshot

enter image description here

Why am I getting an error like this ?

Other details

Server:apache-tomcat-7.0.62 IDE: Eclipse Java EE kepler

Steps I tried to solve the problem

I tried these,

  1. Getting HTTP Status 404 error when trying to run servlet

Result

I did not see the .class files in WEB-INF folder.

  1. Tomcat giving 404 error with servlets

Result In this path "tomcat7\work\Catalina\localhost\your app",the work folder was empty.

Please let me know if I need to post any more details.

Directory structure from IDE

enter image description here

Community
  • 1
  • 1
oldcode
  • 1,669
  • 3
  • 22
  • 41
  • It would be helpful to see project structure/tree. Maybe by screenshot from IDE – Stan Jun 16 '15 at 11:56
  • Did you make an entry in the `web.xml` file? – Uma Kanth Jun 16 '15 at 11:57
  • @Stan I have added the screenshot of project structure. – oldcode Jun 16 '15 at 12:01
  • Thanks. So 'Sample' is your application name and servlet is mapped to '/Sample' URL inside your application. I think you should try to submit your form to the url 'Sample/Sample' (put it to Action attribute). First you can simply try in browser http://localhost:8080/Sample/Sample – Stan Jun 16 '15 at 12:04
  • 3
    It is weird. You have written `form action = Sample` but it is looking for `Sample/welcome`. Try to first rename your Servlet or Project, don't keep their name same. And then lets see what happens. – gprathour Jun 16 '15 at 12:09
  • 1
    by clicking on login the url would be like http://localhost:8080/Sample/Sample?name=StackOverflow because your form action is sample.. – Hiren Jun 16 '15 at 12:10
  • @GPRathour I changed the servlet name from `sampleservlet` to `newserv` but I am getting the above same error.Please let me know what other details can I provide you ? – oldcode Jun 16 '15 at 12:20
  • Add to the META-INF folder a context.xml file with this content: – Zelldon Jun 16 '15 at 12:22
  • No no. I din't mean that. I meant either change your Project name from `Sample` to like `SampleProject` or change the name of servlet class from `Sample` to `MyServlet` etc. – gprathour Jun 16 '15 at 12:22
  • @GPRathour I renamed the project from `sample` to `Sampleproject` . I am still getting the same error. Please see [this](http://postimg.org/image/rm7ch4td5/). – oldcode Jun 16 '15 at 12:41
  • @Zelldon I have added the `context.xml` file. Please see [this](http://postimg.org/image/rm7ch4td5/) – oldcode Jun 16 '15 at 12:42

2 Answers2

2

Your code does work for me in tomcat 7 and eclipse. Perhaps your index.html is not the same as the one your are posting because it's aimnig at Sample/welcome instead of Sample/Sample.

Tips:

  • Make sure your index.html published version is the same as the one you have in your code. Modify the content and make sure it's publishing properly.
  • Load http://localhost:8080/Sample/Sample?name=Random directly in your browser to see that the servlet is working propertly.
  • Double click on Tomcat7 on your servers. Go to your Server path. e.g. C:\Java\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\Sample\WEB-INF\classes (your Sample class should be there) double check if you have permissions in those folders.
  • Export your project as a WAR and deploy it directly in your tomcat without eclipse.
  • Clean/Build your project
  • On your Package Explorer under Servers/server.xml a you should have a declared context e.g. <Context docBase="Sample" path="/Sample" reloadable="false" source="org.eclipse.jst.jee.server:Sample"/></Host> If you don't double check permisions in those folders. Add your tomcat7 server from the start again.

enter image description here

PbxMan
  • 7,525
  • 1
  • 36
  • 40
  • I have changed `
    ` to `
    `, I am getting the above same error. Please let me know if I am doing something wrong.
    – oldcode Jun 16 '15 at 12:52
  • I tried your second tip. I am still getting the same error. What could be wrong ? – oldcode Jun 16 '15 at 12:56
  • @Codester Use
    Please, Enter your name
    instead, let me know if you see the "Please, Enter..." I think the server is not refreshing your changes properly.
    – PbxMan Jun 16 '15 at 12:57
  • `
    Please, Enter your name
    ` Still I am getting the same error.
    – oldcode Jun 16 '15 at 13:15
1

Try this and you don't need to write the XML file for any servlet ;)

Just add the annotation @WebServlet("/urlpattern") and call this URL-pattern inside the JSP from <form action="urlpattern">

Only if you work on Eclipse.

@WebServlet("/Sample")
public class Sample extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    res.setContentType("text/html");  
    PrintWriter pw = res.getWriter();  

    String name = req.getParameter("name"); //will return value  
    pw.println("Welcome "+name);  
    pw.close();  
}}

Hope it helps.

Mike
  • 14,010
  • 29
  • 101
  • 161
CocoCrisp
  • 807
  • 1
  • 9
  • 26