I am new to servlets. I have a servlet that i update the database in it. After clicking submit button, i want to show a message that tells whether update is succesfull or not. For this i am planning to put a label but label tag has no property like value, visible etc. How can i do that? Here is my code of 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
}
}
Can anyone help me with this? Thanks
解决方案
Avoiding printing out HTML from your servlets. It's deprecated.
Create a JSP page, say SignupStatus.jsp
Signup StatusHi ${username},
Your signup ${statusMsg}
Then change your servlet to
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);
}