0

i got some values in HTML table from database in a Java Server Page, in this page i have one text field to take input from user. Now if the user entered value is not available in HTML table or database table i.e, not duplicate, i would add that value in database table using another page. And if the value is already available in HTML table or database i.e, duplicate, this value should not be added in database table. I have to do this using j query. And i am not using j son.

Please help me out anybody.

GetUserType.jsp


    <%@ page import="java.util.*"%>
    <%@ page import="com.nacre.lawfirm.dto.UserTypeTO"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core"   prefix="c" %>
    <c:set var="path" value="${pageContext.servletContext.contextPath}"></c:set>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>DisplayUserTypes</title>
    <script type="text/javascript" src="js/jquery-1.7.2.js"></script>
    <script type="text/javascript" language="javascript">
    $(document).ready(function(){

        $("td").each(function(){

            arr.push($(this).text());
            });

        $.each(arr,function(index,value){

            var v=$("#usertype").val();
            alert(v);

            });

    });
    </script>
    </head>
    <body bgcolor="orange">
    <br>
    <center>
    <div id="wrapper">
    <fieldset>
    <legend style="background-color: #FFFF00;" ><font color="red" size="9">Available Users</font></legend>
    <%-- getting the attribute from Action servlet --%>
     <% 
    Object object=request.getAttribute("arraylist");
    %>

    <%-- type casting the object to arrayList,  iterating values using iterator and printing on browser--%> 
    <%ArrayList arraylist=(ArrayList)object;%> 

    <TABLE BORDER="1"  id="usertypeId" class="userClass" cellspacing="0">
    <thead>
    <tr> 
        <th>UserType</th>  
    </tr> 
    </thead>
    <tbody>
    <%
    ListIterator listIterator=arraylist.listIterator();
    while(listIterator.hasNext()) {
        Object obj=listIterator.next();
        UserTypeTO userTypeTO=(UserTypeTO)obj;
    %>
        <TR class ="rowId">
        <TD class ="user" id="<%= userTypeTO.getUserTypeName()%>"><%= userTypeTO.getUserTypeName() %></TD>
        <%      }%>
        </TR>
    </tbody> 

    </table> <br>
    </fieldset>

    </div> 
    <form action="${path}/AddUserTypeAction" method="post">
    User Type Name<input type="text" name="usertype" class="usertype" id="usertype"  ><br>
    <input type="submit" name="Add-User" value="AddUserType"/>
    </center>
    </form>
    </body>
    </html>




GetUserTypeAction.java

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nacre.lawfirm.delegate.LawFirmDelegate;
import com.nacre.lawfirm.dto.UserTypeTO;

/**
 * Servlet implementation class GetUserTypeAction
 */
@WebServlet("/GetUserTypeAction")
public class GetUserTypeAction extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public GetUserTypeAction() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        LawFirmDelegate lawFirmDelegate=new LawFirmDelegate();

        //creating array list reference variable with null
        List<UserTypeTO> userTypeList=null;
        try {
            //getting array list object from delegate class using that class object
            userTypeList = lawFirmDelegate.getUserType();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println(e);
        }

        request.setAttribute("arraylist", userTypeList);

        //sending control to GetUserType.jsp
        RequestDispatcher requestDispatcher=request.getRequestDispatcher("/UI/COMMONUI/GetUserType.jsp");
        requestDispatcher.forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doGet(req, resp);
    }

}




AddUserTypeAction.java


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nacre.lawfirm.delegate.LawFirmDelegate;
import com.nacre.lawfirm.exception.DatabaseException;



/**
 * Servlet implementation class AddUserTypeAction
 */
@WebServlet("/AddUserTypeAction")
public class AddUserTypeAction extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddUserTypeAction() {
        super();
    }


   /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");

        String usertype=request.getParameter("usertype");

        LawFirmDelegate lawFirmDelegate=new LawFirmDelegate();
        try {
            lawFirmDelegate.addUserType(usertype);
        } catch (DatabaseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        RequestDispatcher requestDispatcher=request.getRequestDispatcher("GetUserTypeAction");
        requestDispatcher.forward(request, response);
    }
}

An

  • Show us the code you have tried. –  Jul 16 '14 at 13:40
  • please tell me how to attach files? –  Jul 16 '14 at 13:44
  • Edit your question. Indent every code line with four spaces. This can be done by selecting the code with and hitting the `{ }` button. –  Jul 16 '14 at 13:46
  • hi, i have copied the code. can anybody check it and give me solution please.. –  Jul 30 '14 at 09:31

1 Answers1

0

I am not sure How you are keeping the data in your page. If you are having the data in JSON format or Javascript object, you can use the below script to identify the user entered value is exists in your list or not

Here the lstObjects is the collection of values from database.

var txtBoxValue = //Get the value from textbox.
if ($.inArray(txtBoxValue, lstObjects) == -1)
{
   //The value is not in the list. Its a new value.
}

This is one way of checking, otherwise use $.grep for grep you can refer the below links

Grep vs Filter in jQuery?

jquery grep on json object array

I Hope this will help you., :)

Community
  • 1
  • 1
Stalin
  • 21
  • 1
  • hi stalin, i have copied the code, can you check it and help me please. –  Jul 30 '14 at 09:40