问题
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
回答1:
Avoiding printing out HTML from your servlets. It's deprecated.
Create a JSP page, say 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>
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);
}
回答2:
Ok . This is bad way of doing what you want. The correct and simple way is forward
to a JSP page after the DB operation , set some attribute as success or failure in request
and display them in the JSP.
Your current approach , you can do something like this :
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);
}
来源:https://stackoverflow.com/questions/17420392/how-to-change-the-text-of-a-label-when-a-page-is-reloaded-in-jsp-page