File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Starting out on Servlets - from Head First Servlets and JSP

 
Bala Sundaram
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem:

User Grade: Newbie Problem Grade: Looks like "simple"

Description: Not able to see servlet output in browser. Can't even figure out whether the HTML form is posting to the servlet.

Problem Context:

I am going through the first servlet tutorial in Chapter 3 in the Head First book and have been able to get the BeerSelect servlet to compile properly.

Am running Tomcat 5.5.7 in debug mode on Windows XP and using IE6.0 as browser. Am able to get "form.html" served successfully by Tomcat.

"form.html" is a simple html posting color of beer to "BeerSelect" via a mapped name. I have "web.xml" under WEB-INF and "BeerSelect.class" file under "Tomcat 5.5/webapps/beerV1/WEB-INF/classes/com/example/web". The only change that I have done from the text in the book is to name the deployment directory "beerV1" and not "Beer-v1".

Troubleshooting steps taken so far:

Checked log files - other than start and top information with some [debug] tags nothing with regards to application.

Have stopped and started Tomcat.

Removed files from the "work" directory under Tomcat, cleared out temporary files from the browser but don't see anything happening.

Also tried to access the servlet by using "http://localhost:8080/beerV1/servlet/com.example.web.BeerSelect" - but this gives me a 404 error.

Help appreciated.

Thanks,
Bala.
 
Annie Smith
Ranch Hand
Posts: 172
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bala Sundaram:

"form.html" is a simple html posting color of beer to "BeerSelect" via a mapped name. I have "web.xml" under WEB-INF and "BeerSelect.class" file under "Tomcat 5.5/webapps/beerV1/WEB-INF/classes/com/example/web". The only change that I have done from the text in the book is to name the deployment directory "beerV1" and not "Beer-v1".

*SNIP*

Also tried to access the servlet by using "http://localhost:8080/beerV1/servlet/com.example.web.BeerSelect" - but this gives me a 404 error.


Did you specify the servlet configuration and servlet mapping in web.xml?



Here's a sample web.xml

To access the HelloWorld servlet, I use the URL http://localhost:8080/testapp/HelloWorld (servlet mapping is intended to take care of that )
 
Bala Sundaram
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response Annie. I do have the servlet configuration and mapping done. Here is my "web.xml" under "Tomcat 5.5/webapps/beerV1/WEB-INF":

<web-app xmlns="http://java.sun.com/xmls/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>
</serlvet>

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

</web-app>

The following is "form.html" under "Tomcat 5.5/webapps/beerV1":

<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
<option> pale
</select>
<br> <br>
<center>
<input type="SUBMIT">
</center>
</form>
</BODY>
</HTML>

And here is "BeerSelect.java" with "BeerSelect.class" file under "Tomcat 5.5/webapps/beerV1/WEB-INF/classes/com/example/web":

package com.example.web;

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

public class BeerSelect extends HttpServlet
{

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<body>");
out.println("Beer Selection Advice <br>");
String c = request.getParameter("color");
out.println("Got beer color " + c + "<br>");
out.println("</body>");
out.println("</html>");

}

}


Thanks,
Bala.
 
Vani Kadur
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming you have done a copy/paste of your DD, I notice that the element </serlvet> has incorrect spelling. Try correcting this.
It is always a good idea to make sure that your web.xml is well formed. One simple way to do it is to open it in a browser and make sure it opens without any errors.
 
Bala Sundaram
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vani - that was very sharp of you to notice that, but I had fixed the "servlet" spelling in the deployment directory when I couldn't even get "form.html" to be served up.

(I did a mistake of cutting & pasting from the development directory without realizing that I had not fixed it in the dev environment).

Here is the final "web.xml" that I am using and the problem still persists:

<web-app xmlns="http://java.sun.com/xmls/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>

Thanks,
Bala.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the address you are using to bring up the HTML page with the form in it?
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bala -

Were you able to get the servlet from Chapter 1 running? If you didn't try, run through it quickly (it should take all of ten minutes) and let us know. If that servlet doesn't compile and run, then there is a problem with your tomcat.
 
Bala Sundaram
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben - I am using "http://localhost:8080/beerV1/form.html". I have tried this with the loopback address too - 127.0.0.1 without success.

Paul - I tried the Chapter 1 Servlet and it works. Does it mean there is some issue with the Tomcat config for handling GET and POST?

Thanks,
Bala.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bala,
If you'd like to test your install of Tomcat, dowload SimpleServlet from http://simple.souther.us to your webapps directory and try hitting it.

If it works, you'll have a working app with which you can compare to your own.
If not, it may be an issue with either your install of Tomcat or with your machine.
 
Bala Sundaram
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben - thanks for the URL you sent. I am able to run another servlet which uses the "doGet()" method and it works fine. It is this BeerSelect servlet which is causing problems. To make things worse, I don't see anything written to the log files. Is there some way to make the servlet write out some log so that I can see that the request is being received by the container?

Thanks,
Bala.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println("message");
 
Bala Sundaram
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben - the System.out.println() doesn't seem to write any output in the log file when added to the servlet which has the doPost() method but works in the one with the doGet() method.

I am thinking that the servlet is OK but there could be some issue with Tomcat handling the POST requests. Is there something special in the Tomcat 5.5.7 config makes it handle POST requests differently?

Am not able to proceed further in the book just because of this roadblock -

Thaks,
Bala.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zip the whole thing up and send it to me (I'll PM you with an address).
I'll try to run it on my machine.
 
Bala Sundaram
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben - thanks for that note. I just sent you a PM with my email address.

Thanks,
Bala.
 
Bala Sundaram
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben - I am cutting and pasting the files "form.html", "web.xml" and "BeerSelect.java" here. I am using Tomcat 5.5.7.

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>

web.xml
-------

<web-app xmlns="http://java.sun.com/xmls/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
----------------
package com.example.web;

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

public class BeerSelect extends HttpServlet
{

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{

System.out.println("Iam here");

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<body>");
out.println("Beer Selection Advice <br>");
String c = request.getParameter("color");
out.println("Got beer color " + c + "<br>");
out.println("</body>");
out.println("</html>");


}

}


Thanks,
Bala.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm willing to drop this in my server and try running it for you but I'm not going to build the whole thing first.
I IMed you with an address that you can send the project to.
Check your messages.

Zip everything up from the docBase (the directory just over WEB-INF) and email it to me.
 
Bala Sundaram
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben - I have mailed the zip file to you.

Thanks,
Bala.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you see the problem you will either laugh or throw your computer out the window.

Line 5 in the form.html file has an unterminated literal.


Once I added the closing quote, your app worked like a charm.
 
Bala Sundaram
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think all programmers need an emoticon/instant graemlin for this feeling:
"Aahh.... - how the hell did I miss this out"

For now I will just do with this -

Thanks Ben - I didn't even bother to look at the HTML form while troubleshooting this - that taught me a lesson.

Thanks a ton,
Bala.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. Nice catch Ben. Don't be too hard on yourself Bala... the rest of us missed it too! You had the html code posted in an earlier post, complete with missing quote and all!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tip:
I had the Live HTTP Headers window open and running when I tried to submit your form.
With it, I was able to see right away that the browser wasn't submitting anything to the server.

It's a dissapointment that neither browser blew up before rendering the page.
 
Bala Sundaram
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben - thanks for that tip and your persistent help.

Paul - thanks for that nice note.

You guys are good and committed to the forum. Hope I can assist you in the future.

Thanks,
Bala.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic