Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can run Tomcat servlet examples, but not mine 2

Status
Not open for further replies.

12345671

IS-IT--Management
May 24, 2003
106
AU
I have redhat9,Apache2,Tomcat4.1.30,jdk1.2.2. I use webapp as connector between APache and Tomcat.

I can view means Apache is running
I can view the servlet example from Tomcat.

Then I put my own hello.class at $CATALINA_HOME/webapps/examples/WEB-INF/classes, the same place as HelloWorldExample.class.

I try:
it didn't show the Hello message but ask me to download the file.
What shall I do? Please advise!!!!
I check the /webapps/examples/WEB-INF\web.xml, it has

<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>

The followign is my own hello.class.

import...

public class hello extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
PrintWriter output;
res.setContentType("test/html");
output=res.getWriter();
output.println("<HTML><HEAD><TITLE>Hello</TITLE></HEAD>");
output.println("<BODY>");
output.println("Hello Test");
output.println("</BODY></HTML>");
}
}
 
Its best to map your servlets like this in web.xml :

<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>hello</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>hello</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
 
I added it, try on a WinXP and my Linuk box, both still ask me to download or Open, then I click Open, then it shows Windows cannot open this file hello, windows needs to know what program crested it,search on the web to...seems the computer cannot run this class? What is worng? Too high Java servlet version?
 
Put your hello.class like this:
%CATALINA_HOME%/webapps/ROOT/classes/hello/hello.class

Then change the %CATALINA_HOME%/webapps/ROOT/web.xml to have the following as subelements to the <web-app> tag:

<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>hello.hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/examples/servlet/hello</url-pattern>
</servlet-mapping>
 
You are using a wrong html tag (res.setContentType("test/html")).

You must write the ContentType like "text/html".

That is the reason which your browser is requesting you download this file.
 
You are totally right, that is the reason, after I corrected it, it is working. Sigh, the mistake took me so long time. Maybe because I am using nodtpad, I am thinking NetBeans maybe can prevent this kind of problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top