Java error - Servlet 404

I am new to servlet programming.

Below is my example 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 a server, it asks me to select a server and then I select a server to run. An eclipse window opens a browser window with the url http: // localhost: 8080 / Sample /

enter image description here

When I click login

it gives me an error like this,

HTTP Status 404 - / Sample / Greetings


type Status report

message / example / greeting

description The requested resource is not available.


Apache Tomcat / 7.0.62

Screenshot

enter image description here

Why am I getting an error?

Other information

Server: Apache-7.0.62-Cat IDE: Eclipse Java EE kepler

Steps I tried to solve the problem

I have tried these,

Result

I have not seen files .class

in the folder WEB-INF

.

  1. Tomcat gives 404 error with servlets

Result On this path, the "tomcat7 \ work \ Catalina \ localhost \ your application" folder work

was empty.

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

Directory structure from IDE

enter image description here

+3


source to share


2 answers


Your code works for me in tomcat 7 and eclipse. Maybe your index.html is not the same as the one you are posting because it targets Sample / Welcome instead of Sample / Sample.

Tips:



  • Make sure the published version of index.html is the same as your code. Change the content and make sure it is published correctly.
  • Download http: // localhost: 8080 / Sample / Sample? Name = Random directly into your browser to make sure the servlet is working fine.
  • 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 class should be there) double check if you have permissions in these folders.
  • Export your project as a WAR and deploy directly to tomcat without eclipse.
  • Clear / create project
  • In the package explorer under the servers /server.xml a you should have a declared context eg. <Context docBase="Sample" path="/Sample" reloadable="false" source="org.eclipse.jst.jee.server:Sample"/></Host>

    Unless you double check the permissions on these folders. Add tomcat7 server from the beginning.

enter image description here

+2


source


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

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

Only if you are working on an 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.

0


source







All Articles