Servlet不可用 - HTTP状态404

时间:2014-05-01 18:37:06

标签: java jsp servlets ejb

我是EJB的新手并尝试在EJB中开发一个货币转换器应用程序。在我的index.jsp页面中,我输入的金额(以美元为单位)和将其转换的货币abd将其发送到一个servlet页面Convert.java,它需要通过在我的实体类货币的帮助下匹配输入的货币来从我的数据库表中获取转换。 当我运行单击JSP页面上的按钮时,我收到以下错误

HTTP Status 404 - Servlet Convert is not available

type Status report

messageServlet Convert is not available

descriptionThe requested resource (Servlet Convert is not available) is not available.

GlassFish Server Open Source Edition 3.1.2.2

这是我的index.jsp

的代码
<body>
    <h1>Currency Converter</h1>
    <form action="Convert" method="get">
        Enter Amount In Dollar <input type="text" name="amt"/>
        Enter Currency <input type="text" name="curr"/>
        <input type="submit" name="conv" value="Convert"/>
    </form>
</body>

我的servlet页面Convert.java的代码如下:

 public class Convert extends HttpServlet {
@EJB
private currencyFacade cf1;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        String curr;
        curr=request.getParameter("curr");
        Long cv =null,fin,amt;
        amt=Long.parseLong(request.getParameter("amt"));
        currency c1=new currency();
        List s=cf1.findAll();
        for (Iterator it = s.iterator(); it.hasNext();)
        {

           currency ac1 = (currency) it.next();
           if(curr.equals(ac1.getCurrto()))
           {
               cv=c1.getValue();
        }}
        fin=amt*cv;
        out.println("Value of "+amt + "Dolar is " + fin+" "+curr);
    }
    catch(Exception ex)
    {
        out.println("Error " + ex);
    }
}

这是我的实体类currency.java

@Entity
public class currency implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String currto;
private Long value;
public Long getId() {
    return id;
}

public String getCurrto() {
    return currto;
}

public void setCurrto(String currto) {
    this.currto = currto;
}

public Long getValue() {
    return value;
}

public void setValue(Long value) {
    this.value = value;
}

public void setId(Long id) {
    this.id = id;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof currency)) {
        return false;
    }
    currency other = (currency) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "entity.currency[ id=" + id + " ]";
}

}

我的web.xml文件如下:

<servlet>
    <servlet-name>Convert</servlet-name>
    <servlet-class>Convert</servlet-class>
</servlet>
<servlet>
    <servlet-name>addcur</servlet-name>
    <servlet-class>addcur</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Convert</servlet-name>
    <url-pattern>/Convert</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>addcur</servlet-name>
    <url-pattern>/addcur</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

1 个答案:

答案 0 :(得分:0)

您将Servlet Convert与URL模式绑定在哪里?

HTML如何了解Servlet类?


The Hello World Java EE application 上找到示例代码。


尝试

@WebServlet("/Convert")
public class Convert extends HttpServlet {
   // ...
}

或尝试

@WebServlet(name = "ConvertServlet", urlPatterns = "/Convert")
public class Convert extends HttpServlet {
   // ...
}