在servlet上显示警报重定向到另一个jsp

时间:2015-03-10 16:59:39

标签: java hibernate jsp servlets

我正在做一个关于hibernate和jsp以及servlet也是数据库的最终项目 我的主题是制作食谱的网络应用程序

现在,我想注册

我使用此方法检查uname是否已被使用(此方法运行良好,没有错误)

public boolean unameMember (String namaTabel,String uname)
{
    this.session = NewHibernateUtil.getSessionFactory().openSession();
    ArrayList<Member> hasil = null;
    boolean uyey = true;
    Transaction tx = session.beginTransaction();
    Query q = session.createQuery("from "+namaTabel);
    hasil = (ArrayList<Member>) q.list();
    for (Member m : hasil) 
    {
        if(m.getIdMember().equals(uname))
            uyey=false;
    }
    session.close();

    return uyey;
}

这是register.jsp的表格

<form action="MemberServlet" method="post" id="regform">


                    <div class="form" >

                        <div class="form_row">

                            <label>Username:</label>
                            <input type="text" name="id_member" required />
                        </div>

                        <div class="form_row">
                            <label>Password:</label>
                            <input type="password" name="pw"  required />
                        </div> 


                        </div> 

                        <div class="form_row">
                            <label>Name:</label>
                            <input type="text" name="nama" required/>
                        </div>

                        <div class="form_row">
                            <label>Alamat:</label>
                            <input type="text" name="alamat" required/>
                        </div>




                        <input type="submit" value="Register" /> 
                    </div>
                </form>

现在这是MemberServlet.java中的问题

String id_member = request.getParameter("id_member");
    String pw = request.getParameter("pw");
    String nama = request.getParameter("nama");
    String alamat = request.getParameter("alamat");
    DataAkses da = new DataAkses();
try {

        out.println("<html>");
        out.println("<head>");
        out.println("</head>");
        out.println("<body>");

            if (da.unameMember("Member", nama)) {
                da.addMember(id_member, pw, nama, alamat);

                out.println("<script type=\"text/javascript\">");
                out.println("alert('You can sign in with your new account');");
                out.println("</script>");
                request.getRequestDispatcher("home.jsp").include(request, response);
            } else {
                out.println("<script type=\"text/javascript\">");
                out.println("alert('User or password incorrect');");
                out.println("location='register.jsp';");
                out.println("</script>");
            }  } catch (Exception e) {

        out.println("<html>");
        out.println("<head>");
        out.println("</head>");
        out.println("</b>");
        out.println("<body>");
        out.println("<center>");
        out.println("<h1>haha ga bisa </h1>");
        out.println("</center>");
        out.println("</body>");
        e.printStackTrace();
    } 

如果我想添加具有不同uname的成员,那么它的效果很好 除此以外 它总是在捕获异常中显示“haha ga bisa”

请帮助我

1 个答案:

答案 0 :(得分:0)

在如此多的级别上代码是错误的我觉得你会更好地采取替代方法。基本上,检查详细信息,如果错误然后返回到寄存器页面,则显示适当的错误消息。否则,转到另一页。

大致看起来像这样:

表单处理程序:

public void doPost(HttpServletRequest request, HttpServletResponse response){

    String nextPage;

    if(problemWithDetails){
        nextPage = "register.jsp";
        request.setAttribute("errorMessage", "Invalid Details");
    }
    else{
        nextPage = "success.jsp";
    }

    request.getRequestDispatcher(nextPage).forward(request, response);
}

register.jsp

<!-- Import JSTL tag library >
<%@ taglib prefix="c" 
           uri="http://java.sun.com/jsp/jstl/core" %>   
<html>

    <!-- Use JSTL to check for the error message we set on form submission if invalid data -->
    <c:if test="${not empty errorMessage}">
        There was an error: ${errorMessage}.
    </c:if>

    <form action="MemberServlet" method="post" id="regform">

        <!-- form as before -->

    </form>
</html>