如何在JSP页面中重新加载页面时更改标签的文本? [英] How to change the text of a label when a page is reloaded in JSP page?

查看:640
本文介绍了如何在JSP页面中重新加载页面时更改标签的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

Avoiding printing out HTML from your servlets. It's deprecated.

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

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>

然后将 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);
}

这篇关于如何在JSP页面中重新加载页面时更改标签的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆