2013-04-20 101 views
3

我想顯示一個網頁[索引頁面],有3個選擇汽車的選項。汽車可以通過單選按鈕或TextBox進行選擇。HTML單選按鈕和Servlet

當選擇一個特定的汽車時,servlet應該響應顯示用戶選擇的客戶端。另外,servlet應該計算所選汽車的價格。但現在,我沒有進入價格,只是顯示用戶的選擇回到他的瀏覽器。

我收到錯誤類型的現狀報告 消息/carShop/tish.com.CarServlet2 描述所請求的資源(/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

請幫我理解我在做什麼錯了?

感謝

+0

查看Web服務器日誌。這應該給你一些指示。 – suspectus 2013-04-20 12:25:18

回答

1

你映射你的servlet URL模式/CarServlet2

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

所以它被映射到絕對URL /carShop/CarServlet2(因爲/carShop是你的web應用的上下文路徑)。

但是,在您的servlet中,您使用相對URL tish.com.CarServlet2,解析爲abswolute URL /carShop/tish.com.CarServlet2,這顯然不是該servlet映射到的URL。您應該使用相對URL CarServlet2

0

在您的.html頁面中,查看您給出的操作。只是把它作爲名稱。我認爲在你做完之後,它會好起來的。即action =「./ CarServlet2」not action =「tish.com.CarServlet2」。