aspose file tools*
The moose likes Servlets and the fly likes Servlet wants to be downloaded Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet wants to be downloaded" Watch "Servlet wants to be downloaded" New topic
Author

Servlet wants to be downloaded

Mark Vilrokx
Greenhorn

Joined: Apr 03, 2006
Posts: 3
Hi,

Just got the book "Servlets and JSP" and I got stuck on the first example (that's gotta be some kinda record). I'm pretty sure it is a setup issue in Tomcat, I just don't know where to look. Basically I have a very simple .html form that that POSTs a request to Tomcat for SelectBeer.do:

<form method="post" action="SelectBeer.do">

I have this name mapped in my DD like so:

<servlet>
<servlet-name>Ch3 Beer</servlet-name>
<servlet-class>com.example.web.BeerSelect</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Ch3 Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>

BeerSelect.java is again a simple servlet that returns one of the inputs on the form, just to prove that the servlet got instanciated.

package com.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("test/html");
PrintWriter out = response.getWriter();
out.println("Beer Selection Advice<br>");
String c = request.getParameter("color");
out.println("<br>Got beer color " + c);
}
}

Now when I hit the submit button, my browser (tried bth IE and Firefox) tries to download the file BeerSelect.do, it doesn't execute it. I tried messing with the xml file to see if it gets picked up by Tomcat, and I am pretty sure it does. E.g. when I screw up the mapping on purpose, I do get an error back from Tomcat saying that it cannto find that file. However if I set the mapping correctly, my browser askes me if I want to download the file (OK/Cancel) and that's it.

Help please!

Cheers,
Mark.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

BeerSelect.do should be SelectBeer.do, or the opposite.
(You set the url-pattern to /SelectBeer.do)
Either the url pattern or the action of your html form is wrong.


[My Blog]
All roads lead to JavaRanch
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Double check your content type, you should be sending text
Mark Vilrokx
Greenhorn

Joined: Apr 03, 2006
Posts: 3
Thank you David!

Indeed my Servlet had this:

response.setContentType("test/html");

After changing it to

response.setContentType("text/html");

it works a charm.

Cheers,
Mark.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Yep, I saw that Glad you got it fixed.

Dave
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlet wants to be downloaded