2

Here is my Java Class by which I am calling simple servlet and passing the data, I am
using URL and HttpURlConnection class.What should be path of url for the servlet

public class TestJava
{
    public static void main(String[] args) 
{
        try 
    {
     URL url=new URL("http://localhost:9865/TestingServlet/PushServlet");

    HttpURLConnection http_url =(HttpURLConnection)   
                                            url.openConnection();
     http_url.setRequestMethod("POST");
     http_url.setDoOutput(true); 
     http_url.setDoInput(true);
     InputStream response = http_url.getInputStream();
     System.out.println(" " +response);
     ObjectOutputStream objOut = new    
             ObjectOutputStream(http_url.getOutputStream());
     objOut.writeObject("hello");
     objOut.flush();
     objOut.close();

    } 

            catch (IOException e) {

        e.printStackTrace();
    }
       }
   }

Here is the servlet code, I am receiving the object from the java code and displaying
it on the console.

  public class PushServlet extends HttpServlet 
   {
        public void doPost(HttpServletRequest request, HttpServletResponse response) 
          throws  ServletException, IOException
         {

          try 
               {

                    System.out.println("HELLO This is servlet");
                ObjectInputStream objIn = new 
                    ObjectInputStream(request.getInputStream());
                TestJava p = null;
                p = (TestJava) objIn.readObject();
                System.out.println("Servlet received p: "+p);       
              } 
                 catch (Throwable e) 
                 {
                    e.printStackTrace(System.out);
             }
    }

My web.xml is like this

 <welcome-file-list>
 <welcome-file>
 Customer_Servlet
  </welcome-file>
  </welcome-file-list>
    <servlet>
    <servlet-name>PushServlet</servlet-name>
    <servlet-class>com.servlet.PushServlet</servlet-class>
   </servlet>
    <servlet-mapping>
     <servlet-name>PushServlet</servlet-name>
     <url-pattern>/PushServlet</url-pattern>
     </servlet-mapping>
     <session-config>
      <session-timeout>50</session-timeout>
      </session-config>
      </web-app>

When i m trying to run the java code on server i.e Apache server, I'm getting
error HTTP Status 404 i am not able to find why i am getting this server error My code is all about invoking the servlet from java application

 please help me guys .
LetsCode
  • 23
  • 7

2 Answers2

0

So to answer your question : "What should be path of url for the servlet?" I think you should know the following :

The URL should be something like :

http://localhost:portNumber/nameOfTheContext/MappingOfTheServlet WHERE

portNumber = the port number of the server ( Apache has it by default to 80 but Apache Tomcat has it to 8080, so it's not 9865 unless you changed it from configuration file)
nameOfTheContext = the name of the folder/archive( with .WAR or .EAR extension) which you deployed to the server
MappingOfTheServlet = the mapping of the servlet which you put in web.xml. So if you have <url-pattern>/PushServlet</url-pattern> then the mapping is PushServlet.

So having that said the URL for you should be :

http://localhost:8080/NameOfTheContext/PushServlet

If it's unclear where you can find the NameOfTheContext tell us how did you deployed the servlet , so we may help you.

Later Edit: the name of the context should be : JavaToServlet because that's the name of your .WAR archive . So your URL should be : http://localhost:9865/JavaToServlet/PushServlet

Daniel
  • 1,861
  • 1
  • 15
  • 23
  • @ Daniel : port number i had changed from congfiguration file , everything is all right but i m not able to clarify what is "NameOfTheContext". my java code and servlet code are in the same package, and my project is dynamic web project. I am running the java code only on apache server. – LetsCode Feb 04 '14 at 10:40
  • @LetsCode what exactly is the server you use: Apache Http Server or Apache Tomcat server ? I guess that you run(start up / shut down) your server from your IDE(Eclipse, NetBeans, IntelliJ etc.) – Daniel Feb 04 '14 at 10:51
  • @ Daniel :I m using Apache Tomcat Server, yeah i am starting and stopping server from Eclipse IDE – LetsCode Feb 04 '14 at 11:01
  • @LetsCode So I'll guess that you use some Apache Tomcat 7.x version . If that's correct then there should be a folder located at: `$CATALINA_HOME/webapps` where $CATALINA_HOME is the location of the installed Apache Tomcat . So there( inside the webapps folder ) should be your folder/archive deployed. The name of the folder represents the nameOfTheContext. – Daniel Feb 04 '14 at 11:14
  • @ Daniel : thanks , i followed the same procedure as u had mentioned ,but still getting the same error ,"Http 404 status" i m running on Apache Tomacat 6.0.32. – LetsCode Feb 06 '14 at 03:03
  • @LetsCode Would you describe me the browsed steps you made in order to deploy the servlet? I think that you start your Apache Tomcat server from Eclipse, but you missed to **deploy** your servlet. In order to deploy a servlet you have to : **compile** the `XXX.java` Java class to `XXX.class`, **archive** it into an archive(usually with the `.WAR` extension or a normal folder and **move** that folder(or archive) into the `webapps` folder from Apache Tomcat installation folder. – Daniel Feb 06 '14 at 07:53
  • @ Daniel : What i m doing is first building project then creating XXX.war file --> then copying in the Apache tomcat folder as "C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\JavaToServlet\JavaToServlet.war",after doing this, starting apache server ,after executing getting the same error.The path i m geeting in apache server is "http://localhost:8080/JavaToServlet/WEBINF/classes/pack/exp/JavaCode.java". – LetsCode Feb 06 '14 at 09:20
  • @LetsCode in C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ you should put your JavaToServlet.war directly( not within a folder). After doing so , _start_ Apache Tomcat and then check the console for something like : _context path : [JavaToServlet] succesfully deployed_. That means you have succesfully deployed the _war_ on Apache. If not, check for _[ERROR]_ in the console and let me know what it says. If everything goes just fine with the deployment, then your URL should be : `http://9865/JavaToServlet/PushServlet` – Daniel Feb 06 '14 at 11:43
  • @ Daniel : Thank you so much , i did exactly what u had told , but when i m trying to run this TestJava Class i m getting the same error, means its not invoking servlet class ,when i run servlet class i m getting println message to console i.e my servlet is okey , there is something wrong in my code. can you run this code in ur system?? Also i m not getting any "context path : [JavaToServlet] succesfully deployed" to the console. – LetsCode Feb 07 '14 at 04:30
  • This is what i m getting on console while starting apache server :::INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.7.0\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Java\jdk1.7.0\bin;;. Feb 07, 2014 9:49:27 AM org.apache.tomcat.util.digester.SetPropertiesRule begin WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:JavaToServlet' did not find a matching property. – LetsCode Feb 07 '14 at 04:33
  • It seems to me that you are getting a Warning. Check here : [link](http://stackoverflow.com/questions/104854/setpropertiesrule-warning-message-when-starting-tomcat-from-eclipse) for how to solve that warning. After you solve that Warning, could you please add `doGet` method with the following : `response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("HELLO from SERVLET");` and try to access the Servlet from the broswer(ie: Mozilla, Chrome , Internet Explorer) on the same url : [link](http://localhost:9865/JavaToServlet/PushServlet) – Daniel Feb 07 '14 at 07:47
0

From the reference on web for 404 Error WiKi

The web site hosting server will typically generate a "404 Not Found" web page when a user attempts to follow a broken or dead link;

You can more about 404 error from below link
404 Not Found Error (What It Is and How To Fix It) .

Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50
  • @ Yagnesh : I have gone through this 404 Error wiki , but i m easily able to start and stop the server but not able to invoke the servlet , while executing its shows this error . – LetsCode Feb 04 '14 at 11:05
  • See your server log while starting server if there any error for application or is your application deployed correctly. – Yagnesh Agola Feb 04 '14 at 11:13