1

In my project, I have html select file and html text file. I want to show some results that is getting to access database in html text file when html select file option onchange event. To do all of this I wrote this servlet codes which is taking data from access database and send results to jsp page.

package org.solr;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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

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

    /**
     * @see HttpServlet#HttpServlet()
     */
    public getValues() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String salesOrder=request.getParameter("salesOrder");
        System.out.println(salesOrder);
        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            String db_path="C:\\Users\\ftk1187\\Desktop\\ad\\Test1.accdb";
            Connection con=DriverManager.getConnection("jdbc:ucanaccess://"+db_path+";jackcessOpener=CryptCodecOpener","","12345tec");
            Statement state=con.createStatement();
            ResultSet rs=state.executeQuery("SELECT * FROM tblSO_all WHERE strSO='"+salesOrder+"'");
            while(rs.next())
            {
                String indDate=rs.getString("strDate");
                System.out.println(indDate);
                request.setAttribute("indDate", indDate);
                request.getRequestDispatcher("upload.jsp").forward(request, response);
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

    }
}

In jsp side, I wrote this javascript code

<script>
function strSO(){
    var salesOrder=$("#sOrder option:selected").val();
    $.ajax({
        type:'get',
        url:'getValues',
        data:'salesOrder='+salesOrder,
        dataType:'text/plain',
        success:function(){
            console.log("success");
        }
    document.getElementById("indDate").value=<%=request.getAttribute("indDate")%>
    }); 
    }
</script>

The servlet running correctly and I'm seeing the results in eclipse's console with sysout. But the result not shown in html text file. So where is the problem?

UPDATE

Here are my eclipse's console and my browser's console. Eclipse's console

web browser's console

UPDATE

After Swati's answers, the solution of the problem is like this

String salesOrder=request.getParameter("salesOrder");
        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            String db_path="C:\\Users\\ftk1187\\Desktop\\ad\\Test1.accdb";
            Connection con=DriverManager.getConnection("jdbc:ucanaccess://"+db_path+";jackcessOpener=CryptCodecOpener","","12345tec");
            Statement state=con.createStatement();
            ResultSet rs=state.executeQuery("SELECT * FROM tblSO_all WHERE strSO='"+salesOrder+"'");
            while(rs.next())
            {
                String indDate=rs.getString("strDate");
                System.out.println(indDate);
                response.getWriter().write(indDate);
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

And the script like this

<script>
function salesOrder(){
    var salesOrder=$("#sOrder option:selected").val();
    $.ajax({
        type:'get',
        url:'getValues',
        data:'salesOrder='+salesOrder,
        success:function(data){
            console.log("success");
            document.getElementById("induction").value=data;
        }
    }); 
}
</script>
demir5334
  • 215
  • 7
  • 17
  • Did you look at the Browser console and identify whether the request is sent to the correct URL? Does the console log `success` as you have printed? – Sanjay Bharathi Mar 03 '20 at 13:58
  • you are doing an ajax call and don't do anything with the result. Why do you expect it to show up anywhere? And what do you mean with "in html text file"? – f1sh Mar 03 '20 at 14:01
  • @f1sh I want to change html input text's dynamically and I want to get datas in my access database. – demir5334 Mar 03 '20 at 14:04
  • @demir5334 where do you change the text? shouldn't the line `document.getElementById("indDate").value` be inside the success callback function? – f1sh Mar 03 '20 at 14:06
  • @f1sh I tried it inside and outside of success callback but it doesn't work. – demir5334 Mar 03 '20 at 14:10
  • General problem, I see: You `forward` your request for each "result row". I'd expect, that you 1. set (a list of) request parameters. 2. `write` something into `response.getOutpustream()`. (alternativeley: 1. "apply values to a template" & 2. render this.) – xerx593 Mar 03 '20 at 14:13
  • 1
    next problem: How can you/one `getElementById()` from a `text/plain`?? (so please "return xml/html/dom" or "handle text".) next problem: The `document.getElementById` (besides that incorrect) is misplaced, it should go into a (success) handler. – xerx593 Mar 03 '20 at 14:15

1 Answers1

0

Whatever you will print i.e : System.out.println(indDate); this will return back as a response in ajax call.Also ,you cannot forward your page from servlet as you are using ajax here you need to sent back something and from there you can forward.So,remove these request.getRequestDispatcher("upload.jsp").forward(request, response); lines from servlet .Also your ajax call should look like below :

function strSO(){
    var salesOrder=$("#sOrder option:selected").val();
    $.ajax({
        type:'get',
        url:'getValues',
        data:'salesOrder='+salesOrder,
        success:function(data){
            console.log("success");
       document.getElementById("indDate").value=data;//indDate will be asign to textbox
    //you can write redirect code  here
        }

    }); 
    }
Swati
  • 28,069
  • 4
  • 21
  • 41
  • Still the result that I get from my servlet, not coming in my html textbox. :( I'll add a picture from my eclipse's console and my web browser's console. – demir5334 Mar 04 '20 at 14:02
  • `alert(data);` inside success function of ajax and check what does it gives,Also, you have error in your js code ..i.e : `expected ';'` check where is this line in your code. – Swati Mar 04 '20 at 15:44
  • When I'm doing `alert(data)`, it's not show anything. It comes **null** :( – demir5334 Mar 05 '20 at 08:55
  • 1
    In your servlet write `response.getWriter().write("Success Data"+indDate);` and check in your ajax method if data comes or not – Swati Mar 05 '20 at 09:07
  • I have 1 more question. If I want to pass an arraylist, how can I do it? – demir5334 Mar 05 '20 at 15:33
  • check [this](https://stackoverflow.com/a/4113258/10606400) answer it will help you to achieve that as well. – Swati Mar 05 '20 at 15:37