• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet Doesn't appear to be working.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?




 
Ranch Hand
Posts: 32
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Chris Greenleaf
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic