Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet wants to be downloaded

 
Mark Vilrokx
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Double check your content type, you should be sending text
 
Mark Vilrokx
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, I saw that Glad you got it fixed.

Dave
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic