我试图从原始servlet的html调用servlet

时间:2019-07-19 18:21:37

标签: html servlets

下面的代码是我的目录servlet代码,并且我试图在按下any按钮时调用Cartservletone(实际上,上面的servlet从数据库中提取数据并以html表形式显示,对于我添加的每一行按钮,现在,当按下任意行的按钮时,它应该调用cartservletone。 但是我得到404页面未找到状态。有没有办法做到这一点?

import javax.servlet.*;
import java.sql.*;
import java.io.*;
public class Catalogueservlet extends GenericServlet
{
    public void service(ServletRequest req,ServletResponse res)throws ServletException,IOException
    {

        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        Connection con=null;
        PreparedStatement st=null;
        ResultSet rs=null;
                //String name=null,pwd=null,email=null,phoneno=null,gender=null,languages=null;
        try
        {
                        String bookname; 
                        String category=req.getParameter("button_clicked");
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","Mary16541");
                        st=con.prepareStatement("select Bookname,Author,Price from Catalogue where Category=?");
                        st.setString(1,category);
                        rs=st.executeQuery();
                         //out.print(rs.next());
                          out.print("<form action='firstservlet/Cartservletone' method='POST'>");
                          out.print("<table border=1>");
                          out.print("<th>BookName</th>");
                          out.print("<th>Author/Publisher</th>");
                          out.print("<th>Price</th>");
                          out.print("<th>Add to cart</th>");
                        while(rs.next())
                         {
                          bookname=rs.getString(1); 
                          out.print("<tr>");
                          out.print("<td>"+bookname+"</td>");
                          out.print("<td>"+rs.getString(2)+"</td>");
                          out.print("<td>"+rs.getInt(3)+"</td>");
                          out.print("<td><input type='submit' name='button_pressed' value="+rs.getString(1)+"></input></td>");
                          out.print("</tr>");      

                         } 
                         out.print("</table> "); 
                         out.print("</form>"); 
                         st.close();
                         con.close();


        }

        catch(Exception e)
        {
            out.println("<h1>EXCEPTION  "+e+"</h1>");
        }

    }
    public static void main(String args[])
    {

    }
}   


<web-app>
  <servlet>
    <servlet-name>Loginservlet</servlet-name>
    <servlet-class>Loginservlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Loginservlet</servlet-name>
    <url-pattern>/firstservlet/Loginservlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>Registrationservlet</servlet-name>
    <servlet-class>Registrationservlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Registrationservlet</servlet-name>
    <url-pattern>/firstservlet/Registrationservlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>Catalogueservlet</servlet-name>
    <servlet-class>Catalogueservlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Catalogueservlet</servlet-name>
    <url-pattern>/firstservlet/Catalogueservlet</url-pattern>
  </servlet-mapping>
   <servlet>
    <servlet-name>Cartservletone</servlet-name>
    <servlet-class>Cartservletone</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Cartservletone</servlet-name>
    <url-pattern>/firstservlet/Cartservletone</url-pattern>
  </servlet-mapping>

  <welcome-file-list>

<welcome-file>mary2.html</welcome-file>

</welcome-file-list>
</web-app>






this is the error
HTTP Status 404 – Not Found
Type Status Report

Message /firstservlet/firstservlet/firstservlet/Cartservletone

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/9.0.16

1 个答案:

答案 0 :(得分:1)

更改此:

out.print("<form action='firstservlet/Cartservletone' method='POST'>");

进入此

out.print("<form action='/firstservlet/Cartservletone' method='POST'>");