0

Here is my html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>Beer Selection Page</title>
    </head>
    <body >
        <form method = "post"
            action = "SelectBeer.do">
            Select beer Characteristics<br/>
            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>

Here is my xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0">
    <display-name>Test2</display-name>
    <servlet>
        <servlet-name>Ch3 beer</servlet-name>
        <servlet-class>BeerSelect</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Ch3 beer</servlet-name>
        <url-pattern>/SelectBeer.do</url-pattern>
    </servlet-mapping>
</web-app>

And here, is my servlet:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

/**
 * Servlet implementation class BeerSelect
 */

 public class BeerSelect extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * Default constructor. 
 */
public BeerSelect() {
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws       ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text.html");
    PrintWriter out = response.getWriter();
    out.println("Beer Selection Advice");
    String c= request.getParameter("color");
    out.println("Got beer color " + c);
}

}

And when I run it on the server, the response is

HTTP Status 404 - /Test2/


type Status report

message /Test2/

description The requested resource is not available.

Test2
.....Java Resources
...................src
......................default package
.....................................BeerSelect.java
.....WebContent
...............META-INF
...............WEB-INF
......................lib
......................web.xml
...............Beer.html

Also, does anybody know why the returned String "c" in the servlet is null??

  • 1
    Can you post like a tree of the folder structure, starting with webapps. It sound like to me you don't have a folder named Test2 under webapps with a WEB-INF containing a web.xml under it. – developerwjk Jun 16 '14 at 19:56
  • What is the URL of your page? – helderdarocha Jun 16 '14 at 20:03
  • Is `Test2` your context (application name?). Try running http://your-server:your-port/Test2/Beer.html . You can also try running the servlet at http://your-server:your-port/Test2/SelectBeer.do – helderdarocha Jun 16 '14 at 20:11
  • You solved the problem. I am just new to JEE, and it was my first app. I was running http://localhost:8080/Test2!! Thanks. – user3552862 Jun 16 '14 at 20:17
  • Does anybody know why the returned String "c" in the servlet is null?? – user3552862 Jun 16 '14 at 23:58

2 Answers2

0

The message: The requested resource is not available. is telling you that the resource (file) you tried to get via the URL /Test2/ is not available, that is, the server can't give it to you. Most probably it wasn't mapped.

In the context of your web application Test2 your servlet is mapped to:

/SelectBeer.do

That means, that outside the context of your web application your access URL should be something like:

http://server:port/Test2/SelectBeer.do

Assuming your HTML file is located in the root of your application context, you should be able to reference the servlet like this:

<form method = "post" action = "SelectBeer.do"> ...

since it is mapped relatively to the root, or using an absolute path like this:

<form method = "post" action = "/Test2/SelectBeer.do"> ...

Since you don't seem to have an index.html (or any welcome file configured in your web.xml), if you try accessing the root of your webapp Test2/ without explicitly including the file name, you will get a 404 error. To open the file correctly you will need to use:

http://server:port/Test2/Beer.html
helderdarocha
  • 23,209
  • 4
  • 50
  • 65
0

Just change to url patter to <url-pattern>/Test2/*</url-pattern>

You create another pattern and you request a diffrent request.. You also can use wildcard *. Not.

Once you create a call the url pattern is searched over the pattern.. For this example above you need to put the url pattern in your request

localhost:8080/Test2/

Something like that will be match to the url pattern

Aviad
  • 1,539
  • 1
  • 9
  • 24