0

我是 servlet 的新手。我有一个 servlet,我更新其中的数据库。单击提交按钮后,我想显示一条消息,告知更新是否成功。为此,我打算放置一个标签,但标签标签没有值、可见等属性。我该怎么做?这是我的servlet代码:

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //System.out.println("editservlet");

    try
    {
        connect();
        int result=-1;
        PreparedStatement checkDB = (PreparedStatement) con.prepareStatement(
        "UPDATE users set username=?,password=?,name=?,surname=?,phone=?,address=?," +
        "email=? where username=?");
        checkDB.setString(8,request.getParameter("tUserName"));
        checkDB.setString(1,request.getParameter("tUserName"));
        checkDB.setString(2,request.getParameter("tPassword"));
        checkDB.setString(3,request.getParameter("tName"));
        checkDB.setString(4,request.getParameter("tSurName"));
        checkDB.setString(5,request.getParameter("tPhone"));
        checkDB.setString(6,request.getParameter("tAddress"));
        checkDB.setString(7,request.getParameter("tEmail"));
        result= checkDB.executeUpdate();
                    if(result>0)
            //SHOW THE MESSAGE THAT TELLS UPDATE SUCCESFUL

    }
    catch(Exception e)
    {
        e.printStackTrace();
                    //SHOW THE MESSAGE THAT UPDATE UNSUCCESFUL
    }


}

谁能帮我这个?谢谢

4

2 回答 2

2

避免从您的 servlet 中打印出 HTML。它已被弃用。

创建一个 JSP 页面,比如SignupStatus.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<html>
 <head>
  <title>Signup Status</title>
 </head>
 <body>
  <p>Hi <b>${username}<b>,<br />
    Your signup ${statusMsg}
  </p>
 </body>
</html>

然后将您的servlet更改为

protected void service(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {

  // request attributes to be sent to JSP
  String username = null;
  StringBuilder statusMsg = new StringBuilder();

    try
    {
        connect();
        int result=-1;
        PreparedStatement checkDB = (PreparedStatement) con.prepareStatement(
        "UPDATE users set username=?,password=?,name=?,surname=?,phone=?," +
        "address=?,email=? where username=?");

        // set username value
        username = request.getParameter("tUserName");

        // set username as request attribute
        request.setAttribute("username", username);

        checkDB.setString(8,username);
        checkDB.setString(1,username);

        checkDB.setString(2,request.getParameter("tPassword"));
        checkDB.setString(3,request.getParameter("tName"));
        checkDB.setString(4,request.getParameter("tSurName"));
        checkDB.setString(5,request.getParameter("tPhone"));
        checkDB.setString(6,request.getParameter("tAddress"));
        checkDB.setString(7,request.getParameter("tEmail"));
        result= checkDB.executeUpdate();

        if(result>0)
            statusMsg.append("was successful!"); // set a success message

    }
    catch(Exception e)
    {
        e.printStackTrace();
        // set a failure message along with error details
        statusMsg.append("failed with the error: ").append(e.getMessage());
    }

    // set status message as request attribute
    request.setAttribute("statusMsg", statusMsg.toString());

    // forward request (along with its attributes) to the status JSP
    RequestDispatcher rd = response.getRequestDispatcher("SignupStatus.jsp");
    rd.forward(request, response);
}
于 2013-07-02T08:24:07.717 回答
0

好的 。这是做你想做的事情的坏方法。正确而简单的方法是forward在DB操作后到一个JSP页面,在其中设置一些属性为成功或失败,request并在JSP中显示出来。

您当前的方法,您可以执行以下操作:

protected void service(HttpServletRequest request,
                       HttpServletResponse response) 
throws ServletException, IOException {
   String message = "";
   try
   {
      // DB related code
      message = "Successful";

   }
   catch(Exception e)
   {
      e.printStackTrace();
      message = "Failure";
   }
   response.getWriter().println(message);
}
于 2013-07-02T08:10:02.083 回答