File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Getting problem with post method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Getting problem with post method" Watch "Getting problem with post method" New topic
Author

Getting problem with post method

Mohammad Miazi
Greenhorn

Joined: Sep 09, 2004
Posts: 10
I was trying the the Mini MVC tutorial(chapter 3, BeerSelection) from Head First Servlet and Jsp book.
From the form when I select a color, and press "submit" button, a "File download" dialoguge box appears which asks me whehter I want save SelectBeer.do file.

The form's action is SelectBeer.do and method=POST.

Is this something related with security seeting in my computer?
I am very new to servlet programming. (using Tomcat, ant).
Your help is appreciated
Kumar Navin
Ranch Hand

Joined: May 27, 2004
Posts: 51
Mohammad,
What I feel like that you r using Sruts. You have action classes in it which is responsible for the processing of the request. You might have invoked the dialog box that you r referring to in the action class.
Check it out, and update it.
-Navin.
Mohammad Miazi
Greenhorn

Joined: Sep 09, 2004
Posts: 10
Thanks for your reply.
However I am not using struts.
I am using tomcat 5.0.28. Using ant build.xml, I deployed the into /myapp context path.
I am calling form.html using http://localhost:8080/myapp/form.html
So if I choose "dark" from this form, I should get the output
Got beer color dark
So the servlet is not run.

form.html
==========
<html><body>
<h1 align="center">Beer Selection page</h1>
<form method="POST"
action="SelectBeer.do">
Select bear characteristics<p>
Color:
<Select name="color" size="1">
<option>light
<option>amber
<option>dark
</select>
<br><br>
<center>
<input type="SUBMIT">
</center>
</form>
</body>
</html>

web.xml
========
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<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>
</web-app>

BeerSelect.java
================
package com.example.web;

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

public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest req,
HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("/text/html");
PrintWriter out = res.getWriter();
out.println("Beer selection advice<br>");
String c = req.getParameter("color");
out.println("<br>Got beer color " + c);
}
}
Mohammad Miazi
Greenhorn

Joined: Sep 09, 2004
Posts: 10
Ok, I was able to find out the reason.
in BeerSelect.java, I have to add:
out.println("<html>");
...........
...........
out.println("</html>");

After adding these two lines, it works as expected.
Ravinder S Edhan
Ranch Hand

Joined: Dec 15, 2003
Posts: 57
Hi Mohammad,

I don't think that's the real problem in that. Giving
out.println("<HTML>"); & out.println("</HTML>"); is not the real solution.
I'm also using the same book for the reference and learning. I don't have to gave that two statements.

Actually while reviewing the code which you had placed in the post, I found that in your servlet you have used following

res.setContentType("/text/html"); which is wrong, it should be
res.setContentType("text/html");

The "/"(forward slash) is creating the problem, i.e why you were getting the saving option.

Cheer
Ravinder S Edhan
SCJP 1.4

[ January 11, 2005: Message edited by: Ravinder S Edhan ]
[ January 11, 2005: Message edited by: Ravinder S Edhan ]
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
The problem sounds a lot like improper configuration of your webapp. It needs to be configured mapping the .do to a specific resource which should them be setting the Response Header appropriately. Because the server isn't configured right, the response header gets set to unknown for the Mime type which causes the browser to save the response instead of displaying it.

Check the web.xml in your web application to see it matches what the book requires.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Mohammad Miazi
Greenhorn

Joined: Sep 09, 2004
Posts: 10
Hi Ravinder:
After removing forward slash from contentType, it works.
Looks like that was the problem.
Thanks.
-- Mohammad
richard ladson
Greenhorn

Joined: Mar 07, 2003
Posts: 11
I have a similar problem with the same example, but my error is:
HTTP Status 405 - HTTP method POST is not supported by this URL

I checked the code discussed above in servlet:
response.setContentType("text/html");
I also added the folloing lines, with no effect:
out.println("<html>");
...
out.println("</html>");

I suspect the DD is bad (web.xml)
------------web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<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>
</web-app>
---------BeerSelect.java servlet
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("text/html");
PrintWriter out = resonse.getWriter();
out.println("Beer Seection Advice<br>");
String c = request.getParameter("color");
out.println("<br>Got beer color " + c);
}
}
-------------form.html
<html><body>
<h1 align="center">Beer Selection Page</h1>
<form method="POST"
action="SelectBeer.do">
Select beer characteristics<p>
Color:
<select name="color" size="1">
<option>light
<option>amber
<option>brown
<option>dark
</select>
<br><br>
<center>
<input type="SUBMIT">
</center>
</form>
</body></html>
-----------------http error
URL to C:\jakarta-tomcat-5.5.4\webapps\Beer-v1\form.html
http://localhost:8080/Beer-v1/form.html
URL from form SUBMIT
http://localhost:8080/Beer-v1/SelectBeer.do
HTTP Status 405 - HTTP method POST is not supported by this URL
richard ladson
Greenhorn

Joined: Mar 07, 2003
Posts: 11
I solved my problem (it was a typo error in my servlet code).

BTW, the code worked exactly as shown in the Headfirst Servlets book.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting problem with post method
 
Similar Threads
Servlet wants to be downloaded
Problem with servlets
Head First Servlet JSP: BeerSelect example: requested resource not available
Regarding to exercise in Head first Servlets & JSP
Resource not available