httprequest can't find servlet

G

gert

I am trying to do a httprequest to a servlet but i always get 404 from
the server ?

url = POST http://localhost:8001/appointment.do

servlet =

package response;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class appointment extends HttpServlet {

protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet appointment</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet appointment at " +
request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}

protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

public String getServletInfo() {
return "Short description";
}

}
 
D

derek

I am trying to do a httprequest to a servlet but i always get 404 from
the server ?

What server are you using? Tomcat?
Did you setup a context for you application?
Did you setup the servlet in your web.xml file?
 
L

Lew

gert said:
I am trying to do a httprequest to a servlet but i always get 404 from
the server ?

url = POST http://localhost:8001/appointment.do

Did you deploy the servlet to a web container, such as Tomcat?

Is the web server running on localhost, and listening to port 8001?
What do you see if you just browse http://localhost:8001/?

Navigating right directly to a ".do" URL is a bit rare; usually that is done
under the hood by Struts for you.

If you are using Struts, why are you putting HTML in a .java servlet instead
of a JSP?

Where is the application context in your URL? "appointment.do" is not the
name of your application, which name is absent from the URL you posted. What
happens if you open your browser to the raw application (which I'll call "foo"
for pedagogical purposes): http://localhost:8001/foo/ ?

If that works OK, and you've set up your app's deployment descriptor
correctly, then try http://localhost:8001/foo/appointment.do.

I'd try this without Struts first, though, and just mount the servlet
someplace reasonable, like http://localhost:8001/foo/appointment for example.
Struts is just a complication you don't need until you learn how to deploy a
servlet.
 
G

gert

gert wrote:
Did you deploy the servlet to a web container, such as Tomcat?
glassfishV2

Is the web server running on localhost, and listening to port 8001?
What do you see if you just browsehttp://localhost:8001/?
yep

Navigating right directly to a ".do" URL is a bit rare; usually that is done
under the hood by Struts for you.

I jus use plain xhtml with javascript doing a xhtml request to the
glassfish server ? I dont use struts i think ?
If you are using Struts, why are you putting HTML in a .java servlet instead
of a JSP?

Just for testing to see if i receive something back from the server.
I dont like jsp, i always sent xml back and forward to the server with
httprequest so i can strictly split up layout and data handling later
on.
Where is the application context in your URL? "appointment.do" is not the
name of your application, which name is absent from the URL you posted. What
happens if you open your browser to the raw application (which I'll call "foo"
for pedagogical purposes):http://localhost:8001/foo/?

static files are located at

http://localhost:8001/w3c/appointment/appointment.htm
If that works OK, and you've set up your app's deployment descriptor
correctly, then tryhttp://localhost:8001/foo/appointment.do.

I think it has something to do with the descriptor i think , because i
have no idea what a descriptor is :)
I'd try this without Struts first, though, and just mount the servlet
someplace reasonable, likehttp://localhost:8001/foo/appointmentfor example.
Struts is just a complication you don't need until you learn how to deploy a
servlet.

I dont now struts either only xhtml and javascript :) Only want a
response from the servlet without using anything else.
 
L

Lew

glassfishV2

You didn't answer my question.

Did you deploy the servlet to Glassfish?

Lew:
gert:
yep

Please answer the second question.
I jus use plain xhtml with javascript doing a xhtml request to the
glassfish server ? I dont use struts i think ?

Then why are you calling the URL "appointment.do"?

To put that another way, how exactly did you come up with that URL?
I dont like jsp [sic], i always sent xml back and forward to the server with
httprequest so i can strictly split up layout and data handling later
on.

JSP generates XHTML if that's what you want, and its purpose is to separate
"layout" from "data handling" and other logic.

You didn't answer the questions. I asked where you mounted the application,
and what happens if you navigate to its URL.

Please answer the questions.
I think it has something to do with the descriptor i think , because i
have no idea what a descriptor is :)

Then you need first to study how to deploy applications to Glassfish. Use the
product documentation.

Then come back and report what happens when you follow directions.
I dont now struts either only xhtml and javascript :) Only want a
response from the servlet without using anything else.

Then deploy your application in accordance with Glassfish's instructions.
 
D

derek

glassfishV2

Its been a little while since i looked at glassfish, but i believe it asks you for the name
of the application when you deploy it, or also in one of the .xml configuration files.
You will want to know this as it usually what your context is going to be.
Whats a context ?

read this:
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

Here is an example of what is in the web.xml file.
Basically it maps requests to servlets.

http://tomcat.apache.org/tomcat-5.5-doc/default-servlet.html


See the tag that says "servlet-mapping"
 
G

gert

Lew wrote:
You didn't answer my question.
Did you deploy the servlet to Glassfish?

No so that means i have to edit web.xml like this ?

<servlet>
<package-name>response<package-name>
<servlet-name>appointment</servlet-name>
<servlet-class>appointment</servlet-class>
</servlet>

Ps one thing i really like about this enterprise stuff is that
everything is in xml :)

I see

"Sun Java System Application Server 9.1 (build b58g-fcs)
Your server is up and running"
Then why are you calling the URL "appointment.do"?
To put that another way, how exactly did you come up with that URL?
http://www.ibm.com/developerworks/library/j-ajax1/

JSP generates XHTML if that's what you want, and its purpose is to separate
"layout" from "data handling" and other logic.

I like to write my own xhtml , css , javascript by hand. Really don't
like entering <jsp specifix xml tags>. I agree it makes life easier
but apache doesn't know jsp tags. And if you want ever static file to
be served by apache you need to stick with xhtml css and javascript.
You didn't answer the questions. I asked where you mounted the application,
and what happens if you navigate to its URL.

NetBeansProjects/w3c/src/java/response/appointment.java
Can't navigate to it with browser is not in the web folder ?
 
G

gert

I learn't i need to configure two files for my servlet to work

sun-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD
Application Server 9.0 Servlet 2.5//EN" "http://www.sun.com/software/
appserver/dtds/sun-web-app_2_5-0.dtd">
<sun-web-app error-url="">
<context-root>/w3c</context-root>
<servlet>
<servlet-name>appointment</servlet-name>
</servlet>
<class-loader delegate="true"/>
</sun-web-app>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>appointment</servlet-name>
<servlet-class>response.appointment</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>appointment</servlet-name>
<url-pattern>/appointment</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>

But i am still forgetting something here because i still get 404
 
L

Lew

gert said:
I like to write my own xhtml , [sic] css , [sic] javascript [sic] by hand. Really don't
like entering <jsp specifix xml [sic] tags>. I agree it makes life easier
but apache [sic] doesn't know jsp [sic] tags. And if you want ever static file to
be served by apache [sic] you need to stick with xhtml css and javascript.

If by "apache" you mean their Web Server (httpd), then it doesn't know
servlets either. Anything that can deliver a servlet can deliver a JSP.

Don't compare writing JSPs to writing static pages, compare writing JSPs to
writing servlets in .java files.
gert:
NetBeansProjects/w3c/src/java/response/appointment.java
Can't navigate to it with browser is not in the web folder ?

That isn't where you mounted the application, that's where you developed it.

You apparently did not deploy the application to Glassfish. The Glassfish
manager application has the means for you to deploy your WAR file. NetBeans
also knows how to deploy an application to a Glassfish server.

(Right-click on the project and select "Deploy" or "Run". You have to have
set up the project properties to have Glassfish as the server.)

Others have directed you to other links. I add,
<http://java.sun.com/javaee/5/docs/tutorial/doc/>

Measure twice, cut once.
 
G

gert

Wait a minute trying /w3c/appointment instead of /appointment

WHOHOOOO it wroks :)

with a response time of 32ms :)

PS do i actualy need both sun-web.xml and web.xml ?
 
D

derek

I learn't i need to configure two files for my servlet to work
sun-web.xml

excellent you are making progress!
<context-root>/w3c</context-root>

That was what i was looking for.

<servlet-name>appointment</servlet-name>
</servlet>

I dont remember off the top of my head the "servlet-name" usually being in this file, but hey
i could be wrong. You might want to double check it.






web.xml
<servlet>
<servlet-name>appointment</servlet-name>
<servlet-class>response.appointment</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>appointment</servlet-name>
<url-pattern>/appointment</url-pattern>
</servlet-mapping>


This is also looking better.
This was where i was expecting to see the "servlet-name"
The only thing that stands out to me is the name
of your servlet class. Its possible to use the name you have given.
Just not typical. Most people follow the following formats:

com.companyname.somepackage.Someclass (Notice the capitalization
and use of com and use of a company name.)

There is nothing that says you HAVE to do it this way, just most
people do, so when i see you have "response.appointment" it is
another area i would suggest you look at.

Also since you didnt respond with the URL you are attempting this time
I will assume it is still this:

http://localhost:8001/appointment.do

Which would not work, since you specified this path in your config:

http://localhost:8001/w3c/appointment

That is assuming the port is 8001, which i actually beleive glassfish
defaults to 8080 on installation. You can check in the admin console i believe.

http://localhost:4848/ i think.
 
G

gert

thanks all it works perfect at blazing response times i didn't
expected from a java server :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top