2

i'm learning html and sevlets,i wrote small aplication but i'm not getting any output when i click submit button from from.html page: below is my code

<html>
<body>
<h1 align="center>Color Selection Page</h1>
<form method="POST" action="/SelectColor.do" >
Select Color Charecterstics<p>
Color:
<select name="color" size="1">
<option>light
<option>amber
<option>brown
<option>dark
</select>

<br><br>

<center>
<input type="submit" value="Submit">
</center>

</form>
</body>
</html>

web.xml file

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

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">

   <servlet>
    <servlet-name>ColorServlet</servlet-name>
    <servlet-class>com.example.web.ColorServlet</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>ColorServlet</servlet-name>
    <url-pattern>/SelectColor.do</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
    <welcome-file>form.html</welcome-file>
    </welcome-file-list>

</web-app>

servlet

package com.example.web;

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

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

public class ColorServlet extends HttpServlet {
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
        res.setContentType("text/html");
        try {
            PrintWriter out = res.getWriter();
            out.println("Beer Selection ADvice<br>");
            String c=req.getParameter("color");
            out.println("<br>Got beer color "+c);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
4
  • 1
    What server or servlet container are you using? Is there anything showing in the server logs? Commented Jan 5, 2010 at 10:17
  • All Option is not ended and it should be <option>light</option> Commented Jan 5, 2010 at 10:24
  • When you submit do you get an error page? A blank page? Does the server disappear off and not come back?... Commented Jan 5, 2010 at 10:31
  • apache tomacat server i'm using, whwn i click button it is not taking any actions Commented Jan 5, 2010 at 10:43

2 Answers 2

6

I've tested your code and the problem comes from this line:

<h1 align="center>Color Selection Page</h1>

The align attribute is not closed, you need to add a double quote to close it.

<h1 align="center">Color Selection Page</h1>

This won't make your HTML valid - Eclipse still complains about "Invalid location of tag (center)" - but, at least, you'll be able to submit the form.

Actually, I'd recommend to write valid HTML or XHTML even if your code is working fine (note that you may have to use action="SelectColor.do" instead of action="/SelectColor.do" depending on the context path of your webapp but this is another story). Writing "bad" HTML will lead you to weird rendering issues and unexpected errors. You should learn to write HTML the right and good way.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow, what a catch. It was not that obvious, though ;). +1
At the time I started to type my answer you didn't mention about the wrong form action at all. *Slaps with a large trout.
@BalusC LOL! My initial focus was indeed the submit problem (the wrong action being another one). Sorry for that :)
4

The relative form action URL is likely wrong. Remove the leading slash / so that it becomes:

<form method="POST" action="SelectColor.do">

Otherwise it will become relative to the domain root. Starters often create web projects with a context name, e.g. http://localhost:8080/contextname/page.jsp. The servlet would be available by http://localhost:8080/contextname/servleturlpattern. Thus, in perspective to the JSP the form action should be action="servleturlpattern". But if you add a leading /, then it would in fact point to http://localhost:8080/servleturlpattern, which doesn't exist at all.

That said, I strongly recommend you to go through a decent HTML book/tutorial first. Your HTML is cluttered of syntax errors and you're also using a <center> element which is deprecated since 1998. There's a basic HTML tutorial/reference at w3schools.com. There's a HTML validator at w3.org. Further on there are many articles about writing semantic HTML.

With regard to JSP/Servlets, I know that you're learning, but you should in fact not use Servlets to output HTML. The JSP is meant for that. You need to forward the request to some result JSP page and access the data with help of EL.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Do some business logic here? Then forward to some result JSP page.
    request.getRequestDispatcher("/WEB-INF/result.jsp").forward(request, response);
}

The JSP should look like:

<!doctype html>
<html lang="en">
    <head><title>Result</title></head>
    <body>
        Beer Selection ADvice<br>
        <br>Got beer color ${param.color}
    </body>
</html>

The ${param.color} is Expression Language and this particular line does in fact sort of out.print(request.getParameter("color")) behind the scenes.

If you aren't doing any postprocessing logic in servlet at all (storing in DB, doing some business logic/validation, etcetera), then you in fact also don't need a servlet at all ;)

To learn more about JSP/Servlets, I can recommend you Marty Hall's Coreservlets tutorials.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.