简体   繁体   中英

Servlet not available - HTTP Status 404

I am new to EJB and trying to develop a Currency Converter application in EJB..In my index.jsp page, I am inputting the amount(in Dollar) and the currency to which it is to be converted abd sending it to a servlet page Convert.java where it needs to fetch the convertion from my database table by matching the inputted currency with the help of my entity class currency.. When I run click on the button on my JSP page, I am getting the following error

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

Here is the code for my 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>

The code for my servlet page Convert.java is as follows :

 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);
    }
}

Here is my entity class 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 + " ]";
}

}

My web.xml file is as follows :

<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>

Where have you bind the Servlet Convert with an URL pattern?

How HTML know about the Servlet class?


Find a sample code on The Hello World Java EE application .


Try with

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

or try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM