Results 1 to 6 of 6

Thread: alert boxes

  1. #1
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default alert boxes

    hello there,
    i am trying to work with jsp and servlets and i am new to this.
    i m nt able to understand how to implement alert boxes through jsp. this is a part of my project,where i want to get alert boxes in three situations:
    1. if i leave "action" field blank
    2. if record is succesfully inserted in Database
    3. if not inserted

    the problem with the code is that its not generating alert box.
    please tell me what is wrong with this code? and if not this what is the right way to achieve this?
    in the servlet i have simply written doPost mehod, where data is being updated.
    here is the code:-

    WorkingOnComplaintsServlet.jsp
    <%@ page import = "java.sql.*" %>
    <%@ page import = "java.io.*, javax.servlet.*,javax.servlet.http.*, java.util.*;" %>
    <%--
    Document : WorkingOnComplaintsServlet
    --%>

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <script type="text/javascript">

    function displ()
    {
    if (document.form.action.equals(""))
    {
    alert ( "Action Field cannot be left blank" );
    document.loginform.action.focus();
    return false;
    }
    else
    {
    <% int m = updaterecord(request,response); %>
    if(m==1)
    {alert ("Record Inserted");}
    else
    {alert ("unsuccesful");}

    return true;
    }
    }
    </script>

    <body>
    <form action="WorkingOnComplaintsServlet">
    <% HttpSession session1= request.getSession();
    String dept= (String)session1.getAttribute("dept_name");

    out.println("<center><h1 style=color:red>"+dept+" Department"+"<h1></center>");
    String c_id = request.getParameter("id");
    try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mydb","root","root");
    Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SEN SITIVE,ResultSet.CONCUR_UPDATABLE);
    String sql="select * from complaint where complaint_code='"+c_id+"'";
    ResultSet rs = stmt.executeQuery(sql);
    if(rs.next())
    { String date = rs.getString("dat_comp");
    String sub = rs.getString("subject");
    String des = rs.getString("description");

    out.println("<BR>");
    out.println("<TABLE BORDER=0>");
    out.println("<TR>");
    out.println("<TD><B>COMPLAINT_CODE : </B></TD>"); out.println("<TD>"+c_id+"</TD>");
    out.println("<TD><B>DATE and TIME : <B></TD>"); out.println("<TD>"+date+"</TD>");
    out.println("</TR>");
    out.println("<TR>");
    out.println("<TD><B>SUBJECT : <B></TD>"); out.println("<TD>"+sub+"</TD>"); out.println("</TR>");
    out.println("<TR>");
    out.println("<TD><B>DESCRIPTION: <B></TD>"); out.println("<TD>"+des+"</TD>"); out.println("</TR>");
    out.println("</table>");
    out.println("<HR width=95% size=3 NOSHADE><h3><EM>WORKVIEW :-</EM></h3>");


    out.println("<TABLE BORDER=0>");

    out.println("<TR>");
    out.println("<TD><B>STATUS : </B></TD>");
    out.println("<TD>");
    out.println("<select name=\"status\">\n" +
    "<option value=\"SOLVED\">solved</option>\n" +
    "<option value=\"UNSOLVED\">unsolved</option>\n"+
    "<option value=\"PROCESSING\">processing</option>\n"+
    "</select>");
    out.println("</TD>");
    out.println("</TR>");

    out.println("<TR>");
    out.println("<TD><B>ACTION </B></TD>");
    out.println("<TD><TEXTAREA name=action ROWS=5 cols=25></textarea></td>");
    out.println("</TR>");

    out.println("<TR>");
    out.println("<TD><INPUT TYPE=submit name=UPDATE value =UPDATE onclick=displ()></td>");
    out.println("</TR>");

    out.println("<TR>");
    out.println("<TD><INPUT TYPE=hidden name=id1 value ="+c_id+"></td>");
    out.println("</TR>");

    out.println("</TABLE>");
    out.println("</form>");
    stmt.close();
    con.close();
    }
    } catch(Exception e)
    {out.println(e.getMessage());}

    out.println("<BR>");

    %>

    <%!int updaterecord(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
    {
    PrintWriter out = response.getWriter();
    String action = request.getParameter("action");
    String status = request.getParameter("status");
    String c_id = request.getParameter("id1");

    try{
    String sql = "update complaint set action= '"+action+"',"+"status ='"+status+"'"+"where complaint_code="+c_id;
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mydb","root","root");
    Statement stmt = con.createStatement();

    int i = stmt.executeUpdate(sql);
    if(i==1)
    {
    return 1;
    }

    con.close();

    } catch(Exception e)
    {out.println(e.getMessage());
    }
    return 0;

    }

    %>
    </body>
    </html>
    Last edited by solutionseeker; 03-26-2013 at 11:04 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,997
    Rep Power
    19

    Default Re: alert boxes

    Please wrap your code in [code] tags [/code].
    Unformatted code is hard to read.

    Java Code:
    <% int m = updaterecord(request,response); %>
    if(m==1)
    {alert ("Record Inserted");}
    else
    {alert ("unsuccesful");}
    You are making a classic mistake here.
    The code in the '<%...%>' is Java and is run on the server.
    The variable 'm' only exists on the server.

    The rest of that code is Javascript and runs on the client browser.
    It does not know about the variable 'm'.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default Re: alert boxes

    You are confusing Java and HTML. Alert boxes are HTML-based. Try building your page entirely in HTML, using dummy values for any data supplied by the JSP. Once the page is working, then you can copy it into a JSP file.
    The Java Tutorial. Read it.

  4. #4
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default Re: alert boxes

    sir, i have made these changes now,
    action1 is the name of my textbox , and f1 is form name..
    function displ()
    { var a=document.f1.action1.value;
    if (a==""||a==null)
    { alert("Action Field cannot be left blank");
    document.f1.action1.focus();
    return false;
    }

    <% String m = updaterecord(request,response);
    %>
    var b = '<%=m%>';
    alert(b);
    return true;
    }
    and i have made the update function like this :-

    String updaterecord(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
    {
    PrintWriter out = response.getWriter();
    String action = request.getParameter("action");
    String status = request.getParameter("status");
    String c_id = request.getParameter("id1");
    String msg = "";

    try{
    String sql = "update complaint set action= '"+action+"',"+"status ='"+status+"'"+"where complaint_code="+c_id;
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/jansunvai","root","root");
    Statement stmt = con.createStatement();

    int i = stmt.executeUpdate(sql);
    if(i==1)
    msg="SUCCESS";
    else
    msg="FAIL";

    con.close();
    } catch(Exception e)
    {out.println(e.getMessage());
    }
    return msg;
    }


    Sir, i m getting two alert boxes , one in case of null value, and the another as "FAIL" always , earlier atleast there was an update in database, but now its not happening.. please help sir.
    Last edited by solutionseeker; 03-27-2013 at 07:23 AM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,229
    Rep Power
    19

    Default Re: alert boxes

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,997
    Rep Power
    19

    Default Re: alert boxes

    Time for my usual post in these cases.
    Remove all Java code from that JSP and stick it in a servlet.
    You should not be doing any of that stuff in the JSP.

    All the JSP should be doing is handling the result of the processing done by the servlet.

    That would remove a load of unecessary code in the JSP that is only confusing matters.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. How do I ignore an alert?
    By kkid in forum NetBeans
    Replies: 4
    Last Post: 11-09-2012, 12:42 PM
  2. Java Alert Script
    By harry2509 in forum New To Java
    Replies: 1
    Last Post: 04-05-2012, 05:21 PM
  3. Javascript Alert
    By mutuah in forum New To Java
    Replies: 4
    Last Post: 03-25-2010, 05:28 PM
  4. Using Alert in MIDlet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-22-2007, 10:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •