Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Having a problem with displaying error messages simultaneously.

Options
  • 10-03-2006 1:28am
    #1
    Posts: 0


    Hi i am having a problem with displaying two separate error strings on my jsp and mutual exclusion seems to be occuring.

    For an online form (Register.jsp) i have 4 fields. for each field there is a number of different error that can occur for example...

    the username may be already taken

    "Re-enter fields, remember all fields must be filled in"



    and other errors which i havent quite got down to inputting into my code. beside the point if i was to enter a username which i know does exist on my DB and leave all the other fields empty it will only display Re-enter fields, remember all fields must be filled in" message. as it is declared first it overrides the other. i know how to put messages on request and take them off etc but i dont know its not displaying as many messages as the eroors that occur.

    here is the code from my RegisterActionServlet

    package pff.actionServlets;

    import java.io.*;
    import java.sql.Connection;
    import java.sql.DriverManager;

    import javax.servlet.*;
    import javax.servlet.http.*;
    import pff.Dao.DatabaseDAOforUserImpl;
    import pff.entities.Current_User;


    public class RegisterAction extends HttpServlet {


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


    DatabaseDAOforUserImpl dbDaoU = new DatabaseDAOforUserImpl();


    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String email = request.getParameter("email");
    String confirm = request.getParameter("confirm");


    Current_User user = dbDaoU.verifyUser(username, password);

    if(username.length() == 0 || password.length() == 0 || email.length() == 0 || confirm.length() == 0) {

    String message = "Re-enter fields, remember all fields must be filled in";
    request.setAttribute("message",message );

    ServletContext sctx = this.getServletContext();


    RequestDispatcher rd = sctx.getRequestDispatcher("/Register.jsp");

    rd.forward(request, response);

    }

    if(username.equals(user.getUsername())){

    String alreadyTaken = "this username is already in use";
    request.setAttribute("alreadyTaken",alreadyTaken );

    ServletContext sctx = this.getServletContext();


    RequestDispatcher rd = sctx.getRequestDispatcher("/Register.jsp");

    rd.forward(request, response);
    }

    else{
    try{

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    Connection conn = DriverManager.getConnection("jdbc:odbc:football", "guest" , "guest");
    java.sql.Statement stmt = conn.createStatement();

    String query = "INSERT INTO User_details(username,password,email, confirmedPassword) Values ('"+ username + "','" + password + "','" + email + "','" + confirm + "')";
    stmt.executeUpdate(query);

    }catch(java.lang.ClassNotFoundException cnfe){
    System.out.println("Class not Found");
    }catch(java.sql.SQLException sqle){
    System.out.println("SQLException");
    }
    }
    }

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

    processRequest(request, response);
    }

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

    processRequest(request, response);
    }
    }

    and a snippet from my jsp
    Register.jsp

    <html>

    <body bgcolor="#FFFFFF">
    <table width="770" border="0" cellspacing="0" cellpadding="0" height="555">
    <tr bgcolor="#6633FF">
    <td height="100" colspan="4" bgcolor="#3399FF"> </td>
    </tr>
    <tr>
    <td width="100" bgcolor="#3399FF" rowspan="11"> </td>
    <td height="56" colspan="3"><font color="#333333"></font></td>
    </tr>

    <tr>
    <td height="325" colspan="2" rowspan="5" valign="top">
    <%
    String message = (String)request.getAttribute("message");




    if(message == "Re-enter fields, remember all fields must be filled in"){
    %>

    <p><font size="2" color="red"><%=message%></font></p>

    <% }else{

    }%>


    <% String alreadyInUse =(String)request.getAttribute("alreadyInUse");
    if(alreadyInUse == "this username is already in use"){


    %>

    <p><font size="2" color="red"><%=alreadyInUse%></font></p>

    <% }
    else{


    } %>


Comments

  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    Look more closely at this:
    String message = blah;
    if(message == "poop"){
    

    Spot a problem? Ever heard of String.equals()?


  • Posts: 0 [Deleted User]


    bpmurray wrote:
    Look more closely at this:
    String message = blah;
    if(message == "poop"){
    

    Spot a problem? Ever heard of String.equals()?

    look at the RegisterAction you'll see ive used .equals() in one of the if statements.

    Nevertheless shouldnt what ive done amount to the same thing??? And that it should work too as i am creating 2 different strings to be put on 2 different requests for 2 different errors. As the errors are found it should output a different string on the jsp which corresponds to each error. Maybe im wrong. its obviously got to be something simple wrong. anyways all im really doing with the if condition on the jsp is preventing 'null' from displaying otherwise hence the if else condition


  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    Try this instead:
     <%
    String message = (String)request.getAttribute("message");
    if (null != message && message.length() > 0){
       request.setAttribute("message", null);
    %>
    
    <p><font size="2" color="red"><%=message%></font></p>
    
    <% }
    String alreadyInUse = (String)request.getAttribute("alreadyInUse");
    if (null != alreadyInUse && alreadyInUse.length() > 0) {
       request.setAttribute("alreadyInUse", null);
    %>
    
    <p><font size="2" color="red"><%=alreadyInUse%></font></p>
    
    <% } %>
    

    There are probably typos, but it should be pretty close. The idea is that when you test for the contents of the attribute, you should both test for its existence (non-null) and for it actually containing something. If both of these are true, display it, and nuke its current value.

    Your if-statements could never possibly be true, since you are comparing strings using "==". What really confuses me is that you are suggesting that yuu actually occasionally get one of the messages to display, somthing that seems really bizarre.


  • Posts: 0 [Deleted User]


    bpmurray wrote:
    Try this instead:
     <%
    String message = (String)request.getAttribute("message");
    if (null != message && message.length() > 0){
       request.setAttribute("message", null);
    %>
    
    <p><font size="2" color="red"><%=message%></font></p>
    
    <% }
    String alreadyInUse = (String)request.getAttribute("alreadyInUse");
    if (null != alreadyInUse && alreadyInUse.length() > 0) {
       request.setAttribute("alreadyInUse", null);
    %>
    
    <p><font size="2" color="red"><%=alreadyInUse%></font></p>
    
    <% } %>
    

    There are probably typos, but it should be pretty close. The idea is that when you test for the contents of the attribute, you should both test for its existence (non-null) and for it actually containing something. If both of these are true, display it, and nuke its current value.

    Your if-statements could never possibly be true, since you are comparing strings using "==". What really confuses me is that you are suggesting that yuu actually occasionally get one of the messages to display, somthing that seems really bizarre.




    Exact Same result.

    im pretty sure the answer lies in my servlet.


    All that code in my jsp is doing is getting an attribute, casting it as a string and then the if/else statement simply tests is if the string contains that sentance.

    if it contains this sentance (on condition that an error is made) then display it, else display nothing(otherwise null is displayed on view by default).

    It works fine on my login page. The problem is that no matter how many individual attributes i set and later, get in the jsp, it will only ever display one regardless of if they are separate variables or not. for example 'alreadyInUse' and 'message' are two individual messages that are set individually and that are got individually.


  • Registered Users Posts: 4,188 ✭✭✭pH


    Does .forward(request, reponse) not send the page back on for processing straight away without continuing processing?

    You are going to have to keep a flag if errors have occured, set the attributes as you go, and at the end (once forward the page if errors have occureed) with all the attributes set, else do your dB work


  • Advertisement
  • Posts: 0 [Deleted User]


    Does .forward(request, reponse) not send the page back on for processing straight away without continuing processing?

    your right it does.

    i solved the problem since simply by nesting individual error messages in if loops within an if loop which catches all errors then on my jsp i put

    if(message==null){

    }
    else

    <p><%=message%></P>

    <% } %>


    bpmurray was sort of right and there is no need to check the value of the message pointer


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    look at the RegisterAction you'll see ive used .equals() in one of the if statements.

    Nevertheless shouldnt what ive done amount to the same thing???

    Thought javascript was a bit more loose and allowed this but in Java no, .equals() and == are different. The latter doesn't gurantee a valid result when comparing string objects.


Advertisement