更新来自Servlet的数据

时间:2019-06-16 17:14:15

标签: java html servlets

尝试更新servlet页面上的信息。但是获得HTTP 404响应(Apache tomcat)。

不知道问题出在哪里

import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

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

            out.println("<html>");
            out.println("<head>");
            out.println("<title>");
            out.println("Update Page");
            out.println("</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<table>");
        Connection connection = null;
        Statement statement = null;
        ResultSet resultset = null;
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/userdb";
        DriverManager.getConnection(url,"root","root123");
        connection = DriverManager.getConnection(url,"root","root123");
        statement = connection.createStatement();
        System.out.println("Database Successfully Connected");
        resultset = statement.executeQuery("select * from user_table");
    while(resultset.next())
    {
        out.println("<form action='update' method='post'>");
        out.println("<tr>");
        out.println("<td>");
        out.println("<input type='text' name='name' value='" + resultset.getString(1) +"' / >");
        out.println("<td>");
        out.println("<input type='text' name='age' value='" + resultset.getString(2) +"' / >"); 
        out.println("<td>");
        out.println("<input type='text' name='height' value='" + resultset.getString(3) +"' / >"); 
        out.println("<td>");
        out.println("<input type='text' name='weight' value='" + resultset.getString(4) +"' / >");
        out.println("<td>");
        out.println("<input type='submit' value='Update' / >");
        out.println("</form>");
    }
    }
    catch(ClassNotFoundException ce)
    {
        out.println("Error " + ce);
    }
    catch(SQLException se)
    {
        out.println("Error " + se);
    }
    finally
    {
    try
    {
        connection.close();
    }
    catch(Exception e)
    {
    }
    }
    out.println("</table>");
    out.println("</body>");
    out.println("</html>");
    }
}



public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>");
out.println("Select Page");
out.println("</title>");
out.println("</head>");
out.println("<body>");
Connection connection = null;
Statement statement = null;

String name = req.getParameter("name");
String age = req.getParameter("age");
String height = req.getParameter("height");
String weight = req.getParameter("weight");

try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/userdb";
connection = DriverManager.getConnection(url,"root","root123");
statement = connection.createStatement();

String query = "update user_table set age='" + age + "', height='" + height + "', weight='" + weight + "' where name='" + name + "'";

int result = statement.executeUpdate(query);
out.println(result + " row(s) updated.");
}
catch(ClassNotFoundException ce)
{
out.println("Error " + ce);
}
catch(SQLException se)
{
out.println("Error " + se);
}
finally
{
try
{
connection.close();
}
catch(Exception e)
{
}
}
out.println("</body>");
out.println("</html>");
}
}


“更新”按钮应直接指向已更新1行的页面。 我认为它是查询部分或声明部分。 编译时没有错误,只是结果与预期不符。 希望有人能解决这个问题

1 个答案:

答案 0 :(得分:0)

我认为您需要将servlet映射到url

通过注释

@WebServlet("/update")
public class Update extends HttpServlet{
 //...
{

或通过在 web.xml

中进行设置
<web-app>
  <servlet>
    <servlet-name>Update</servlet-name>
    <servlet-class>com.something.Update</servlet-class> <!-- Your package --> 
  </servlet>

  <servlet-mapping>
    <servlet-name>Update</servlet-name>
    <url-pattern>/update</url-pattern>
  </servlet-mapping>
</web-app>