我想显示一个网页[索引页]有3种选择的汽车。汽车可以选择单选按钮或文本框。
当选择了特定的cars时,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>
文件目录设置:
汽车商店/tish/com.carservlet2
请帮助我理解我做错了什么?
谢谢
最佳答案
您将servlet映射到url模式:
<servlet-mapping>
<servlet-name>CarServlet2</servlet-name>
<url-pattern>/CarServlet2</url-pattern>
</servlet-mapping>
因此它被映射到绝对url
/CarServlet2
(因为/carShop/CarServlet2
是您的webapp的上下文路径)。但是,在您的servlet中,您使用相对url
/carShop
,解析为abswolute urltish.com.CarServlet2
,这显然不是servlet映射到的url。您应该改用相对url/carShop/tish.com.CarServlet2
。关于html - HTML单选按钮和Servlet,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16120238/