jakarta-ee - 在Tomcat6.0中出现 "The requested resource () is not available."

标签 jakarta-ee tomcat servlets tomcat6

昨天我在远程服务器上用 java servlet 编程时遇到一个问题。

我们的任务是构建一个简单的购物车,用户可以通过输入名称并单击“提交”来添加商品,然后将调用 servlet 并显示购买的商品。

在我编码之后,服务器告诉我错误“请求的资源 () 不可用。”每次。我搜索并尝试了所有我能找到的方法,但仍然无效。

代码如下:

HTML(/~usr/public_html/market.html)

<html>
<head>
<title>Online Shopping</title>
</head>

<body>
<h1>Online Shopping</h1>
Welcome to the online market!<br>
<br>
Please input the item you want to buy:<br>
<form action="http://localhost:8080/~usr/cart" method="get">
<input type="text" name="item"><br>
<input type="submit" value="Submit">
</form>

</body>

</html>

web.xml(/~usr/public_html/WEB-INF/web.xml)

<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>cart</servlet-name>
<servlet-class>cart</servlet-class>

<servlet-mapping>
<servlet-name>cart</servlet-name>
<url-pattern>/cart</url-pattern>
</servlet-mapping>
</web-app>

Java代码(编译后放到/~usr/public_html/WEB-INF/classes/cart.class)

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.io.*;


public class cart extends HttpServlet
{   
    public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();

        HttpSession session = req.getSession(true);

        //get the previous number of items in cart. If none, creat it and initialize to zero
        Integer count = (Integer)session.getAttribute("count");
        if(count == null)
            count = new Integer(0);

        //declare a string reference to get the new added item, if it's null, don't add it
        String[] item_name;

        item_name = req.getParameterValues("item");
        if(item_name != null)
        {           
            count ++;
            session.setAttribute("count",count);    
            session.setAttribute("items"+count,item_name[0]);
        }

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Items in Shopping Cart</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("New item successfully added to shopping cart.<br>");
            out.println("Your cart contains X items.<br><br>");
            out.println("<center><h1>The items you purchased:</h1>");

            for(int i=1; i<=count; i++)
                {
                    String goods = (String)session.getAttribute("items"+i);
                    out.println("Item #"+i+": "+goods+"<br>");
                }


            out.println("</center></body>");
            out.println("</html>");

            out.close();
    }



}

Tomcat 和服务器由教授预先配置,我只是按照规则将这些文件放在哪里等等。

最佳答案

你不需要完整路径,在下面替换

<form action="http://localhost:8080/~usr/cart" method="get">

跟随

<form action="cart" method="get">

关于jakarta-ee - 在Tomcat6.0中出现 "The requested resource () is not available.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20050849/

相关文章:

java - 在哪里可以找到 ConnectivityMonitor()?

jakarta-ee - 如何制作 Eclipselink 的 moxy marshal HashMap<String, String>?

javascript - spring boot 运行但独立的 tomcat 不工作

eclipse - 如何在Eclipse Juno中运行Servlet?

java - 使 servlet 过滤器适用于所有 Web 应用程序

spring - 下载文本/html文件时如何防止servlet响应编码? Spring +JSF

java - NetBeans EE 包括 Java EE?

linux - Tomcat 在端口 80 上工作,但不在 8080 上工作。防火墙已关闭

java - 使用cookies API获取sessionId而不访问session

java - WEB-INF/lib 目录与 Java 9 模块