HTML单选按钮和Servlet

时间:2013-04-20 12:14:51

标签: html forms servlets input

我想显示一个网页[索引页面],其中有3个选项可供选择。可以通过单选按钮或TextBox选择汽车。

当选择特定汽车时,servlet应响应显示用户选择的客户端。此外,servlet应计算所选汽车的价格。但就目前而言,我没有进入价格,只是在浏览器上显示用户的选择。

我收到错误类型状态报告 消息/carShop/tish.com.CarServlet2 description所请求的资源(/carShop/tish.com.CarServlet2)不可用。 可能是错的: -

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<TITLE> Welcome to CarShop </TITLE> 
</HEAD> 

<BODY> 

<FORM Action = "tish.com.CarServlet2"  METHOD=POST> 

<BR>Select a Type of Car:<BR> 


Brand <INPUT TYPE=text NAME="Brand" > <br>

Year <INPUT TYPE=text NAME="Year" > <br> 

<input type="checkbox" name="car" value="honda">Honda<br>
<input type="checkbox" name="car" value="nissan">Nissan<br>

<INPUT TYPE= SUBMIT NAME= "Submit " Value = "Submit the Selection">
<br>
<INPUT TYPE= RESET NAME = "RESET" Value = "Reset">


</FORM> 
</BODY>
</HTML>

的Servlet: -

package tish.com;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


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


    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("Testing my Car Sevlet");
        //System.out.println(arg0.getInitParameter("Year"));
        //config =arg0;


                String [] car = req.getParameterValues("car");

                PrintWriter writer = resp.getWriter();

            resp.setContentType("text/html");
            writer.print("<html> <body>");
            writer.print("<hl> Your choice is </hl>");

            for (String s: car)
            {

                writer.print("<l1>" + s+ "/l1>");


            }
            writer.print("</body></html> ");
            }   


    }

的web.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>Welcome.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet-mapping>
    <servlet-name>CarServlet2</servlet-name>
    <url-pattern>/CarServlet2</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>CarServlet2</servlet-name>
    <servlet-class>tish.com.CarServlet2</servlet-class>
  </servlet>

</web-app>

文件目录设置:

carShop / tish / com.CarServlet2

请帮我理解我做错了什么?

由于

2 个答案:

答案 0 :(得分:1)

您将servlet映射到URL模式/CarServlet2

<servlet-mapping>
    <servlet-name>CarServlet2</servlet-name>
    <url-pattern>/CarServlet2</url-pattern>
</servlet-mapping>

因此它被映射到绝对URL /carShop/CarServlet2(因为/carShop是您的webapp的上下文路径)。

在您的servlet中,您使用相对URL tish.com.CarServlet2,解析为绝对URL /carShop/tish.com.CarServlet2,这显然不是servlet映射到的URL。您应该使用相对网址CarServlet2

答案 1 :(得分:0)

在.html页面中,查看您提供的操作。把它命名为。我认为在你这样做之后,它会没事......即行动=&#34; ./ CarServlet2&#34;不是行动=&#34; tish.com.CarServlet2&#34;。