It's not a secret anymore!*
The moose likes Servlets and the fly likes Servlet Doesn't appear to be working. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Doesn Watch "Servlet Doesn New topic
Author

Servlet Doesn't appear to be working.

Chris Greenleaf
Greenhorn

Joined: Aug 08, 2011
Posts: 4
I'm trying to get a servlet to work and I'm having some issues. I have the following form.html in the C:\tomcat\webapps\Beer-v1\ directory:

<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 value="light"> light </option>
<option value="amber"> amber </option>
<option value="brown"> brown </option>
<option value="dark"> dark </option>
</select>
<br><br>
<center>
<input type="SUBMIT">
</center>
</form></body></html>

I have the following DD (web.xml) file in the following directory: C:\tomcat\webapps\Beer-v1\WEB-INF\ directory:

<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>

Here is my source code:



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 = response.getWriter();
out.println("Beer Selection Advice<br>");
String c = request.getParameter("color");
out.println("<br>Go beer color " +c);
}
}

I put the BeerSelect.class file in the following directory:

C:\tomcat\webapps\Beer-v1\WEB-INF\classes\com\example\web\ directory.

When I bring up the URL http://localhost:8080/Beer-v1/form.html my page displays correctly but when I click on the submit button the servlet doesn't work. I suspect it can't find it but I can't figure out what I'm doing wrong.

I've installed tomcat in the C:\tomcat directory.

Any Ideas why this isn't working?




Marcin Kwiatkowski
Ranch Hand

Joined: Aug 06, 2007
Posts: 32

Code would be easier to read if you placed it in appropriate tag.

What you mean by servlet doesn't work? Does it throw any exceptions?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Your form action URL is incomplete. It needs to start with the context path.

In the future, please be sure to UseCodeTags when posting code to the forums.e


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chris Greenleaf
Greenhorn

Joined: Aug 08, 2011
Posts: 4
I assume you mean the context path in the form.html?

So, should the context path be:

"http://localhost:8080/BeerV1/SelectBeer.do"

Thanks,
Chris
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

The context path is just the /Beer-v1 part. You should not have the rest of the absolute URL in there.

The context path can be obtained programmatically from the request and should not be hard-coded.
Chris Greenleaf
Greenhorn

Joined: Aug 08, 2011
Posts: 4
I still can't get this to work. I understand the context path shouldn't be hard coded but I'm new to servlets and trying to do an example in the Head First Servlets book. I changed my form.html to the following and I still can't get this to work:





I've also tried the following:


This is frustrating.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The "action" of the form is treated as relative to the URL of the document in which the form is located, unless it's an absolute URL. You didn't say anything about where that HTML was loaded from.

You could start by putting the absolute URL into your HTML for now. (The absolute URL would start with "http://".)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Paul Clapham wrote:The "action" of the form is treated as relative to the URL of the document in which the form is located

That is only true if the URL is page-relative. Starting the URL with the context path makes the action URL non-relative to the page and independent of the current URL.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Is the app running?

What happens? Just saying "it doesn't work" isn't helpful. What's the full text of the error message?

Anything in the logs?
Chris Greenleaf
Greenhorn

Joined: Aug 08, 2011
Posts: 4
There's no reference to my BeerSelect servlet in any of the log files. The funny thing is the servlet in the first chapter works fine. The only difference in this one is that I'm using a package and doing a form action which I think it can't find. The code compiled fine but I think it just can't find the servlet.

What's the best way to debug this?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Bear Bibeault wrote:
Paul Clapham wrote:The "action" of the form is treated as relative to the URL of the document in which the form is located

That is only true if the URL is page-relative. Starting the URL with the context path makes the action URL non-relative to the page and independent of the current URL.


Sorry to butt in to the thread, but now I'm confused. Given the code

How does the browser know whether "Beer-v1" is a context path or not? Or if that's the wrong question, how does the browser know what host to send the request to if the action is independent of the page's URL?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Paul Clapham wrote:How does the browser know whether "Beer-v1" is a context path or not? Or if that's the wrong question, how does the browser know what host to send the request to if the action is independent of the page's URL?

The leading slash tells the browser that its a server-relative URL.
Sujata Samal
Greenhorn

Joined: Jan 29, 2008
Posts: 20
Hi Chris,


I would say add doGet(..) in you servlet and put some SOPs/log messages and acess the servlet directly "http://localhost:8080/Beer-v1/SelectBeeer.do" from the IE's address bar and verify if that hits the servlet.

When you submit the html see the url in the addressbar ,i think that will tell you the servlet location which the html is trying to hit.

-Sujata
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Doesn't appear to be working.
 
Similar Threads
error for first web application : The requested resource (/Beer-v1/SelectBeer.do) is not available.
Headfirst ch3 Beerselect submit button not working
Resource not available
Cannot bring up a html page using tomcat 6.0
Problems with the Example from HF Servlets & JSP in Chapter 3